sph_crawling_data.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import json
  2. import random
  3. import time
  4. import requests
  5. from common import Material, Oss, Feishu
  6. from common.sql_help import sqlCollect
  7. from data_channel.data_help import dataHelp
  8. from data_channel.shipinhao import SPH
  9. class SphHistory:
  10. """获取视频号所有内容"""
  11. @classmethod
  12. def sph_data_info(cls):
  13. user_list = cls.get_sph_user()
  14. if user_list == None:
  15. return
  16. for user in user_list:
  17. account_id = SPH.get_account_id(user)
  18. if account_id == False:
  19. print(f"{account_id}:没有获取到视频account_id,无法抓取数据")
  20. continue
  21. url = "http://61.48.133.26:30001/FinderGetUpMasterNextPage"
  22. last_buffer = ""
  23. try:
  24. count = 1
  25. while True:
  26. headers = {
  27. 'Content-Type': 'application/json'
  28. }
  29. payload = json.dumps({
  30. "username": account_id,
  31. "last_buffer": last_buffer
  32. })
  33. response = requests.request("POST", url, headers=headers, data=payload)
  34. time.sleep(random.randint(1, 5))
  35. count += 1
  36. if response.text == "" or response.text == None:
  37. break
  38. res_json = response.json()
  39. try:
  40. if len(res_json["DownloadAddress"]) == 0 or res_json["DownloadAddress"] == "" or res_json["DownloadAddress"] == None:
  41. break
  42. except:
  43. pass
  44. if "objectId" not in response.text or response.status_code != 200:
  45. break
  46. if len(res_json["UpMasterHomePage"]) == 0:
  47. break
  48. if not res_json["UpMasterHomePage"]:
  49. break
  50. last_buffer = res_json.get('last_buffer')
  51. try:
  52. for obj in res_json["UpMasterHomePage"]:
  53. objectId = obj['objectId']
  54. object_id = sqlCollect.sph_data_info_v_id(objectId, "视频号")
  55. if object_id:
  56. continue
  57. objectNonceId = obj['objectNonceId']
  58. url1 = "http://61.48.133.26:30001/GetFinderDownloadAddress"
  59. payload = json.dumps({
  60. "objectId": objectId,
  61. "objectNonceId": objectNonceId
  62. })
  63. headers = {
  64. 'Content-Type': 'text/plain'
  65. }
  66. response = requests.request("POST", url1, headers=headers, data=payload)
  67. time.sleep(random.randint(0, 1))
  68. video_obj = response.json()
  69. video_url = video_obj.get('DownloadAddress')
  70. duration = video_obj.get('play_len')
  71. # duration = dataHelp.video_duration(video_url)
  72. cover = video_obj.get('thumb_url')
  73. if len(video_url) == 0:
  74. continue
  75. v_id = f"sph/{objectId}"
  76. try:
  77. oss_video_key = Oss.channel_upload_oss(video_url, v_id) # 视频发送OSS
  78. oss_video_key = oss_video_key.get("oss_object_key")
  79. oss_cover_key = Oss.channel_upload_oss(cover, f"sph/{objectId}.jpg") # 视频发送OSS
  80. oss_cover_key = oss_cover_key.get("oss_object_key")
  81. create_time = obj['createtime'] # 发布时间
  82. except:
  83. continue
  84. share_cnt = int(obj['forward_count']) # 分享
  85. like_cnt = int(obj['like_count']) # 点赞
  86. video_title = video_obj.get('title').split("\n")[0].split("#")[0]
  87. user_name = obj['username'] # 用户名标示
  88. nick_name = obj['nickname'] # 用户名
  89. comment_count = obj['comment_count'] # 评论数
  90. fav_count = obj['fav_count'] # 大拇指点赞数
  91. sqlCollect.sph_data_info('视频号', objectId, video_url, cover, video_title, str(share_cnt), str(like_cnt), oss_video_key, oss_cover_key, nick_name, user_name, comment_count, fav_count, create_time,duration)
  92. except Exception as e:
  93. continue
  94. sqlCollect.update_sph_channel_user_status(user)
  95. count = sqlCollect.sph_data_info_count(user, "视频号")
  96. text = (
  97. f"**{user}抓取完成:共抓了{count[0]}条数据**\n"
  98. )
  99. Feishu.finish_bot(text,
  100. "https://open.feishu.cn/open-apis/bot/v2/hook/029fa989-9847-4574-8e1b-5c396e665f16",
  101. "【 视频号历史数据抓取通知 】")
  102. except Exception as e:
  103. Feishu.finish_bot(e,
  104. "https://open.feishu.cn/open-apis/bot/v2/hook/029fa989-9847-4574-8e1b-5c396e665f16",
  105. "【 视频号抓取异常通知 】")
  106. continue
  107. @classmethod
  108. def get_sph_user(cls):
  109. data = sqlCollect.sph_channel_user_list()
  110. if data == None:
  111. user_list = Material.get_sph_user()
  112. if user_list:
  113. for user in user_list:
  114. sqlCollect.insert_sph_channel_user("视频号", user)
  115. else:
  116. return None
  117. result_list = [item for sublist in data for item in sublist]
  118. return result_list
  119. if __name__ == '__main__':
  120. SphHistory.sph_data_info()
  121. # count = sqlCollect.sph_data_info_count("郑蓝旗", "视频号")
  122. # print(count)