1
0

refactor: clean up variable names

This commit is contained in:
2025-08-04 12:31:10 +09:00
parent 6987c26b11
commit 69a5fd2e97
3 changed files with 30 additions and 30 deletions

View File

@@ -20,10 +20,10 @@ class Attachment:
@dataclass @dataclass
class Post: class Post:
id: int id: int
boardId: str board_id: str
boardPath: BoardPath board_path: BoardPath
authorId: Optional[str] = None author_id: Optional[str] = None
authorName: Optional[str] = None author_name: Optional[str] = None
category: Optional[str] = None category: Optional[str] = None
title: Optional[str] = None title: Optional[str] = None
body: Optional[Tag] = None body: Optional[Tag] = None

View File

@@ -2,7 +2,7 @@ import asyncio
from aiohttp import ClientRequest, ClientResponse, ClientHandlerType from aiohttp import ClientRequest, ClientResponse, ClientHandlerType
class SemaphoreMiddleware(asyncio.Semaphore): class Semaphore(asyncio.Semaphore):
async def __call__(self, req: ClientRequest, handler: ClientHandlerType) -> ClientResponse: async def __call__(self, req: ClientRequest, handler: ClientHandlerType) -> ClientResponse:
async with self: async with self:
return await handler(req) return await handler(req)

View File

@@ -37,10 +37,10 @@ class Scraper(ClientSession):
async def list( async def list(
self, self,
boardId: str, board_id: str,
boardPath: BoardPath = 'board', board_path: BoardPath = 'board',
page: int = 1, page: int = 1,
categoryId: int = 0, category_id: int = 0,
only_recommended: bool = False, only_recommended: bool = False,
only_notice: bool = False, only_notice: bool = False,
search_type: Optional[SearchType] = None, search_type: Optional[SearchType] = None,
@@ -50,20 +50,20 @@ class Scraper(ClientSession):
""" """
특정 게시판으로부터 특정 조건에 맞는 게시글 목록을 가져옵니다 특정 게시판으로부터 특정 조건에 맞는 게시글 목록을 가져옵니다
:param boardId: 게시판 아이디 :param board_id: 게시판 아이디
:param boardPath: 게시판 경로(종류) :param board_path: 게시판 경로(종류)
:param page: 페이지 번호 :param page: 페이지 번호
:param categoryId: 말머리 아이디 :param category_id: 말머리 아이디
:param only_recommended: 개념글 게시글만 조회할지? :param only_recommended: 개념글 게시글만 조회할지?
:param only_notice: 공지 게시글만 조회할지? :param only_notice: 공지 게시글만 조회할지?
:param search_type: 검색 종류 :param search_type: 검색 종류
:param search_position: 검색 지점 :param search_position: 검색 지점
:param search_value: 검색어 :param search_value: 검색어
""" """
url = f'https://m.dcinside.com/{boardPath}/{boardId}' url = f'https://m.dcinside.com/{board_path}/{board_id}'
params = { params = {
'page': page, 'page': page,
'headid': categoryId, 'headid': category_id,
'recommend': only_recommended and '1' or '0', 'recommend': only_recommended and '1' or '0',
'notice': only_notice and '1' or '0', 'notice': only_notice and '1' or '0',
's_type': search_type or '', 's_type': search_type or '',
@@ -78,8 +78,8 @@ class Scraper(ClientSession):
return [ return [
Post( Post(
id=int(re.findall(r'/\d+', tag.select_one('a[href]:first-child')['href'])[0][1:]), id=int(re.findall(r'/\d+', tag.select_one('a[href]:first-child')['href'])[0][1:]),
boardId=boardId, board_id=board_id,
boardPath=boardPath board_path=board_path
) )
for tag in document.select('.gall-detail-lnktb') for tag in document.select('.gall-detail-lnktb')
] ]
@@ -90,35 +90,35 @@ class Scraper(ClientSession):
:param post: 조회할 게시글 인스턴스 :param post: 조회할 게시글 인스턴스
""" """
async with self.get(f'https://m.dcinside.com/{post.boardPath}/{post.boardId}/{post.id}') as response: async with self.get(f'https://m.dcinside.com/{post.board_path}/{post.board_id}/{post.id}') as response:
html = await response.text() html = await response.text()
document = BeautifulSoup(html, 'lxml') document = BeautifulSoup(html, 'lxml')
# 상단 제목 요소는 `li`로 나누어져있고 무슨 지랄을 해도 정확히 2개임 # 상단 제목 요소는 `li`로 나누어져있고 무슨 지랄을 해도 정확히 2개임
# 만약 아니라면 어처피 파싱 무결성 전체가 깨질테니 예외 처리는 나도 몰?루 # 만약 아니라면 어처피 파싱 무결성 전체가 깨질테니 예외 처리는 나도 몰?루
authorTag, timestampTag, *_ = document.select('.gallview-tit-box .ginfo2 > li') author_tag, timestamp_tag, *_ = document.select('.gallview-tit-box .ginfo2 > li')
authorAnchorTag = authorTag.select_one('a') author_anchor_tag = author_tag.select_one('a')
# 작성일 파싱 # 작성일 파싱
post.created_at = ( post.created_at = (
datetime datetime
.strptime(timestampTag.get_text(strip=True), '%Y.%m.%d %H:%M') .strptime(timestamp_tag.get_text(strip=True), '%Y.%m.%d %H:%M')
.replace(tzinfo=ZoneInfo('Asia/Seoul')) .replace(tzinfo=ZoneInfo('Asia/Seoul'))
) )
# 작성자 정보 파싱 # 작성자 정보 파싱
if authorAnchorTag: if author_anchor_tag:
# 작성자 요소에 앵커 태그가 있다면 갤로그가 존재하는 상태임 # 작성자 요소에 앵커 태그가 있다면 갤로그가 존재하는 상태임
post.authorId = re.findall(r'\/\w+$', authorAnchorTag['href'])[0][1:] post.author_id = re.findall(r'\/\w+$', author_anchor_tag['href'])[0][1:]
post.authorName = authorAnchorTag.get_text(strip=True) post.author_name = author_anchor_tag.get_text(strip=True)
else: else:
authorParts = authorTag.get_text(strip=True).split('(') author_parts = author_tag.get_text(strip=True).split('(')
post.authorId = authorParts.pop()[:-1].strip() # 123.123) -> 123.123 post.author_id = author_parts.pop()[:-1].strip() # 123.123) -> 123.123
post.authorName = authorParts.pop().strip() post.author_name = author_parts.pop().strip()
# 모바일 웹에서 말머리와 제목은 `\n`으로 분리되어있음 # 모바일 웹에서 말머리와 제목은 `\n`으로 분리되어있음
titleTexts = ( title_texts = (
document document
.select_one('.gallview-tit-box .tit') .select_one('.gallview-tit-box .tit')
.get_text(strip=True) .get_text(strip=True)
@@ -126,10 +126,10 @@ class Scraper(ClientSession):
) )
# 제목과 말머리 파싱 # 제목과 말머리 파싱
post.title = titleTexts.pop().strip() post.title = title_texts.pop().strip()
if titleTexts: if title_texts:
post.category = titleTexts.pop()[1:~1].strip() # [XX] -> XX post.category = title_texts.pop()[1:~1].strip() # [XX] -> XX
# 본문 파싱 # 본문 파싱
post.body = document.select_one('.thum-txtin') post.body = document.select_one('.thum-txtin')
@@ -138,7 +138,7 @@ class Scraper(ClientSession):
for tag in post.body.select('script, style'): for tag in post.body.select('script, style'):
tag.extract() tag.extract()
print(f'{post.boardId}/{post.id}: {post.title}') print(f'{post.board_id}/{post.id}: {post.title}')
async def fetch_voice(self, id: str): async def fetch_voice(self, id: str):
""" """