articleDBServer.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import time
  6. from applications.wxSpider import ArticleManager
  7. from applications.functions import show_desc_to_sta
  8. class ArticleSpider(object):
  9. """
  10. input: ghId, AccountName
  11. """
  12. def __init__(self, params, mysql_client):
  13. self.endTime = None
  14. self.startTime = None
  15. self.ghId = None
  16. self.params = params
  17. self.mysql_client = mysql_client
  18. self.tools = ArticleManager()
  19. def checkParams(self):
  20. """
  21. 校验参数
  22. :return:
  23. """
  24. try:
  25. self.ghId = self.params['ghId']
  26. return None
  27. except Exception as e:
  28. return {
  29. "error": "Params Error",
  30. "msg": str(e),
  31. "params": self.params
  32. }
  33. async def checkAccount(self):
  34. """
  35. 判断账号是否是新账号, 内部账号还是外部账号
  36. :return:
  37. """
  38. sql = f"""
  39. select accountName, updateTime
  40. from official_articles_v2
  41. where ghId = '{self.ghId}'
  42. order by updateTime DESC;
  43. """
  44. result = await self.mysql_client.async_select(sql)
  45. if result:
  46. account_name, update_time = result[0]
  47. return {
  48. "account_name": account_name,
  49. "update_time": update_time,
  50. "account_type": "history"
  51. }
  52. else:
  53. return {
  54. "account_name": "",
  55. "update_time": int(time.time()) - 30 * 24 * 60 * 60,
  56. "account_type": "new"
  57. }
  58. async def process_msg_list(self, gh_id, account_name, msg_list):
  59. """
  60. 把消息数据更新到数据库中
  61. :param account_name:
  62. :param gh_id:
  63. :param msg_list:
  64. :return:
  65. """
  66. for info in msg_list:
  67. baseInfo = info.get("BaseInfo", {})
  68. appMsgId = info.get("AppMsg", {}).get("BaseInfo", {}).get("AppMsgId", None)
  69. createTime = info.get("AppMsg", {}).get("BaseInfo", {}).get("CreateTime", None)
  70. updateTime = info.get("AppMsg", {}).get("BaseInfo", {}).get("UpdateTime", None)
  71. # if int(time.time()) - int(updateTime) <= 20 * 60 * 60:
  72. # continue
  73. Type = info.get("AppMsg", {}).get("BaseInfo", {}).get("Type", None)
  74. detail_article_list = info.get("AppMsg", {}).get("DetailInfo", [])
  75. if detail_article_list:
  76. for article in detail_article_list:
  77. title = article.get("Title", None)
  78. Digest = article.get("Digest", None)
  79. ItemIndex = article.get("ItemIndex", None)
  80. ContentUrl = article.get("ContentUrl", None)
  81. SourceUrl = article.get("SourceUrl", None)
  82. CoverImgUrl = article.get("CoverImgUrl", None)
  83. CoverImgUrl_1_1 = article.get("CoverImgUrl_1_1", None)
  84. CoverImgUrl_235_1 = article.get("CoverImgUrl_235_1", None)
  85. ItemShowType = article.get("ItemShowType", None)
  86. IsOriginal = article.get("IsOriginal", None)
  87. ShowDesc = article.get("ShowDesc", None)
  88. show_stat = show_desc_to_sta(ShowDesc)
  89. ori_content = article.get("ori_content", None)
  90. show_view_count = show_stat.get("show_view_count", 0)
  91. show_like_count = show_stat.get("show_like_count", 0)
  92. show_zs_count = show_stat.get("show_zs_count", 0)
  93. show_pay_count = show_stat.get("show_pay_count", 0)
  94. wx_sn = ContentUrl.split("&sn=")[1].split("&")[0] if ContentUrl else None
  95. info_tuple = (
  96. gh_id,
  97. account_name,
  98. appMsgId,
  99. title,
  100. Type,
  101. createTime,
  102. updateTime,
  103. Digest,
  104. ItemIndex,
  105. ContentUrl,
  106. SourceUrl,
  107. CoverImgUrl,
  108. CoverImgUrl_1_1,
  109. CoverImgUrl_235_1,
  110. ItemShowType,
  111. IsOriginal,
  112. ShowDesc,
  113. ori_content,
  114. show_view_count,
  115. show_like_count,
  116. show_zs_count,
  117. show_pay_count,
  118. wx_sn,
  119. json.dumps(baseInfo, ensure_ascii=False)
  120. )
  121. try:
  122. insert_sql = f"""
  123. INSERT INTO official_articles_v2
  124. (ghId, accountName, appMsgId, title, Type, createTime, updateTime, Digest, ItemIndex, ContentUrl, SourceUrl, CoverImgUrl, CoverImgUrl_1_1, CoverImgUrl_255_1, ItemShowType, IsOriginal, ShowDesc, ori_content, show_view_count, show_like_count, show_zs_count, show_pay_count, wx_sn, baseInfo)
  125. values
  126. (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
  127. """
  128. await self.mysql_client.async_insert(sql=insert_sql, params=info_tuple)
  129. print("插入成功")
  130. except Exception as e:
  131. try:
  132. update_sql = f"""
  133. UPDATE official_articles_v2
  134. SET show_view_count = %s, show_like_count=%s
  135. WHERE wx_sn = %s;
  136. """
  137. await self.mysql_client.async_insert(sql=update_sql, params=(show_view_count, show_like_count, wx_sn))
  138. print("更新成功")
  139. except Exception as e:
  140. print("失败-{}".format(e))
  141. continue
  142. async def getAccountArticleList(self, gh_id, account_name, last_update_time, cursor=None):
  143. """
  144. 输入ghid获取账号的文章list
  145. :return:
  146. """
  147. response = self.tools.update_msg_list(ghId=gh_id, index=cursor)
  148. msg_list = response.get("data", {}).get("data")
  149. if msg_list:
  150. # print(msg_list)
  151. print("获取msg_list成功")
  152. last_article_in_this_msg = msg_list[-1]
  153. last_time_stamp_in_this_msg = last_article_in_this_msg['AppMsg']['BaseInfo']['UpdateTime']
  154. last_url = last_article_in_this_msg['AppMsg']['DetailInfo'][0]['ContentUrl']
  155. resdata = await self.tools.get_account_by_url(last_url)
  156. check_name = resdata['data'].get('data', {}).get('account_name')
  157. check_id = resdata['data'].get('data', {}).get('wx_gh')
  158. print(check_name, check_id, last_url)
  159. if check_id == gh_id:
  160. print("校验成功")
  161. await self.process_msg_list(gh_id, check_name, msg_list)
  162. if last_time_stamp_in_this_msg > last_update_time:
  163. next_cursor = response['data']['next_cursor']
  164. return await self.getAccountArticleList(
  165. gh_id=gh_id,
  166. account_name=check_name,
  167. last_update_time=last_update_time,
  168. cursor=next_cursor
  169. )
  170. else:
  171. print("校验失败")
  172. async def deal(self):
  173. """
  174. deal function
  175. :return:
  176. """
  177. if self.checkParams():
  178. return self.checkParams()
  179. else:
  180. account_info = await self.checkAccount()
  181. account_name = account_info['account_name']
  182. update_time = account_info['update_time']
  183. print("开始执行")
  184. await self.getAccountArticleList(
  185. gh_id=self.ghId,
  186. account_name=account_name,
  187. last_update_time=update_time
  188. )
  189. return {"message": "successful"}