gzh_spider.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from __future__ import annotations
  2. import re
  3. import json
  4. import requests
  5. from fake_useragent import FakeUserAgent
  6. from tenacity import retry
  7. from applications.api import log
  8. from applications.utils import request_retry
  9. from applications.utils import AsyncHttpClient
  10. retry_desc = request_retry(retry_times=3, min_retry_delay=2, max_retry_delay=30)
  11. # url from aigc
  12. base_url = "http://crawler-cn.aiddit.com/crawler/wei_xin"
  13. headers = {"Content-Type": "application/json"}
  14. @retry(**retry_desc)
  15. async def get_article_detail(
  16. article_link: str, is_count: bool = False, is_cache: bool = True
  17. ) -> dict | None:
  18. """
  19. get official article detail
  20. """
  21. target_url = f"{base_url}/detail"
  22. payload = json.dumps(
  23. {
  24. "content_link": article_link,
  25. "is_count": is_count,
  26. "is_ad": False,
  27. "is_cache": is_cache,
  28. }
  29. )
  30. try:
  31. async with AsyncHttpClient(timeout=10) as http_client:
  32. response = await http_client.post(target_url, headers=headers, data=payload)
  33. except Exception as e:
  34. log(
  35. task="get_article_detail",
  36. function="get_article_detail",
  37. message=f"API请求失败: {e}",
  38. data={"link": article_link},
  39. )
  40. return None
  41. return response
  42. @retry(**retry_desc)
  43. async def get_article_list_from_account(account_id: str, index=None) -> dict | None:
  44. target_url = f"{base_url}/blogger"
  45. payload = json.dumps({"account_id": account_id, "cursor": index})
  46. try:
  47. async with AsyncHttpClient(timeout=120) as http_client:
  48. response = await http_client.post(target_url, headers=headers, data=payload)
  49. except Exception as e:
  50. log(
  51. task="get_article_list_from_account",
  52. function="get_article_list_from_account",
  53. message=f"API请求失败: {e}",
  54. data={"account_id": account_id, "index": index},
  55. )
  56. return None
  57. return response
  58. @retry(**retry_desc)
  59. def get_source_account_from_article(article_link) -> dict | None:
  60. """
  61. get account info from official article
  62. :param article_link:
  63. :return:
  64. """
  65. try:
  66. response = requests.get(
  67. url=article_link,
  68. headers={"User-Agent": FakeUserAgent().random},
  69. timeout=120,
  70. )
  71. response.raise_for_status()
  72. html_text = response.text
  73. regex_nickname = r"hit_nickname:\s*'([^']+)'"
  74. regex_username = r"hit_username:\s*'([^']+)'"
  75. nickname = re.search(regex_nickname, html_text)
  76. username = re.search(regex_username, html_text)
  77. # 输出提取的结果
  78. if nickname and username:
  79. return {"name": nickname.group(1), "gh_id": username.group(1)}
  80. else:
  81. return {}
  82. except requests.exceptions.RequestException as e:
  83. log(
  84. task="get_source_account_from_article",
  85. function="get_source_account_from_article",
  86. message=f"API请求失败: {e}",
  87. data={"link": article_link},
  88. )
  89. except json.JSONDecodeError as e:
  90. log(
  91. task="get_source_account_from_article",
  92. function="get_source_account_from_article",
  93. message=f"响应解析失败: {e}",
  94. data={"link": article_link},
  95. )
  96. return None
  97. @retry(**retry_desc)
  98. async def weixin_search(keyword: str, page="1") -> dict | None:
  99. url = "{}/keyword".format(base_url)
  100. payload = json.dumps({"keyword": keyword, "cursor": page})
  101. try:
  102. async with AsyncHttpClient(timeout=120) as http_client:
  103. response = await http_client.post(url=url, headers=headers, data=payload)
  104. except Exception as e:
  105. log(
  106. task="weixin_search",
  107. function="weixin_search",
  108. message=f"API请求失败: {e}",
  109. data={"keyword": keyword, "page": page},
  110. )
  111. return None
  112. return response