Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
NanmiCoder committed Jan 3, 2024
1 parent 38d6f10 commit 80ecc53
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 23 deletions.
24 changes: 2 additions & 22 deletions media_platform/bilibili/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,32 +147,12 @@ async def get_comments(self, video_id: str, semaphore: asyncio.Semaphore):
async with semaphore:
try:
utils.logger.info(f"[BilibiliCrawler.get_comments] begin get video_id: {video_id} comments ...")
# Read keyword and quantity from config
keywords = config.COMMENT_KEYWORDS
max_comments = config.MAX_COMMENTS_PER_POST

# Download comments
all_comments = await self.bili_client.get_video_all_comments(
await self.bili_client.get_video_all_comments(
video_id=video_id,
crawl_interval=random.random(),
callback=bilibili.batch_update_bilibili_video_comments
)

# Filter comments by keyword
if keywords:
filtered_comments = [
comment for comment in all_comments if
any(keyword in comment["content"]["message"] for keyword in keywords)
]
else:
filtered_comments = all_comments

# Limit the number of comments
if max_comments > 0:
filtered_comments = filtered_comments[:max_comments]

# Update bilibili video comments
await bilibili.batch_update_bilibili_video_comments(video_id, filtered_comments)

except DataFetchError as ex:
utils.logger.error(f"[BilibiliCrawler.get_comments] get video_id: {video_id} comment error: {ex}")
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion models/bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def update_bilibili_video_comment(video_id: str, comment_item: Dict):
local_db_item = {
"comment_id": comment_id,
"create_time": comment_item.get("ctime"),
"video_id": video_id,
"video_id": str(video_id),
"content": content.get("message"),
"user_id": user_info.get("mid"),
"nickname": user_info.get("uname"),
Expand Down

0 comments on commit 80ecc53

Please sign in to comment.