Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: portal crawling bugs #450

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions apps/core/management/scripts/portal_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ def _save_portal_image(html, session):

for tr in trs:
if len(list(tr.children)) == 3:
html = tr.find("td").prettify()
html = str(tr.find("td"))
break

if html is None:
for tr in trs:
if len(list(tr.children)) == 2:
html = tr.find("td").prettify()
html = str(tr.find("td"))
break

html = _save_portal_image(html, session)
Expand Down Expand Up @@ -208,11 +208,21 @@ def _get_board_today(page_num):
linklist = []
links = soup.select("table > tbody > tr > td > a")
dates = soup.select("table > tbody > tr > td:nth-child(5)")
total = soup.select("div > ul > li > em")[0].get_text()

if links:
log.info("------- portal login success!")
else:
log.info("------- portal login failed!")
if not links:
log.error("------- portal login failed!")
raise RuntimeError("portal login failed!")

if int(total) < 10_000:
"""
If the total number of response articles is small,
all responses are public. (LOGIN FAILED)
"""
log.error("------- portal login cookie failed!")
raise RuntimeError(f"portal login cookie {COOKIES} failed!")

log.info("------- portal login success!")

today_date = str(day).replace("-", ".")
for link, date in zip(links, dates):
Expand Down Expand Up @@ -316,7 +326,14 @@ def _get_board_today(page_num):
last_portal_article_in_db.save()
new_articles.pop()

created_articles = Article.objects.bulk_create(new_articles)
# @NOTE
# MySQL's bulk_create method does not return IDs. However, PortalViewCount requires the IDs of the created articles.
# Therefore, insert one article at a time and retrieve their IDs.
# Reference: https://docs.djangoproject.com/en/5.0/ref/models/querysets/#bulk-create
created_articles = []
for new_article in new_articles:
new_article.save()
created_articles.append(new_article)

new_portal_view_counts = []

Expand Down
Loading