shipinhao.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import json
  2. import random
  3. import time
  4. import requests
  5. from common import Common, AliyunLogger, Feishu
  6. from common.sql_help import sqlCollect
  7. from data_channel.data_help import dataHelp
  8. class SPH:
  9. @classmethod
  10. def find_target_user(cls, name, user_list):
  11. """
  12. 在搜索到到账号列表中找目标列表
  13. """
  14. for obj in user_list:
  15. if obj['contact']["nickname"] == name:
  16. return obj
  17. else:
  18. continue
  19. return False
  20. @classmethod
  21. def get_account_id(cls, account_name):
  22. channel = 'shipinhao'
  23. history_id = sqlCollect.get_history_id(channel, account_name)
  24. if history_id:
  25. return history_id
  26. else:
  27. url = "http://8.217.190.241:8888/crawler/wei_xin/shi_pin_hao/account_info"
  28. payload = json.dumps({
  29. "account_name": account_name
  30. })
  31. headers = {
  32. 'Content-Type': 'application/json'
  33. }
  34. response = requests.request("POST", url, headers=headers, data=payload)
  35. response = response.json()
  36. if response['code'] == 0:
  37. data = response['data']['data']
  38. channel_account_id = data['channel_account_id']
  39. if channel_account_id:
  40. sqlCollect.insert_history_id(account_name, channel_account_id, channel)
  41. return channel_account_id
  42. else:
  43. return False
  44. else:
  45. Feishu.finish_bot("shi_pin_hao/account_info接口获取失败",
  46. "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb",
  47. "【视频号接口异常提示 】")
  48. return False
  49. @classmethod
  50. def get_sph_url(cls, task_mark, url_id, number, mark, channel_id, name):
  51. account_id = cls.get_account_id(url_id)
  52. if account_id:
  53. url = "http://8.217.190.241:8888/crawler/wei_xin/shi_pin_hao/blogger"
  54. next_cursor = ""
  55. list = []
  56. # for i in range(10):
  57. headers = {
  58. 'Content-Type': 'application/json'
  59. }
  60. payload = json.dumps({
  61. "account_id": account_id,
  62. "cursor": next_cursor
  63. })
  64. try:
  65. response = requests.request("POST", url, headers=headers, data=payload)
  66. time.sleep(random.randint(1, 5))
  67. res_json = response.json()
  68. if res_json['code'] == 0:
  69. # next_cursor = res_json['data']['next_cursor']
  70. data_lsit = res_json['data']['data']
  71. for obj in data_lsit:
  72. objectId = obj['id']
  73. status = sqlCollect.is_used(task_mark, objectId, mark, "视频号")
  74. old_title = obj['object_desc']['description']
  75. url_p = obj['object_desc']['media'][0]['url']
  76. url_token = obj['object_desc']['media'][0]['url_token']
  77. video_url = f"{url_p}{url_token}"
  78. decode_key = obj['object_desc']['media'][0]['file_size']
  79. cover = obj['object_desc']['media'][0]['cover_url']
  80. share_cnt = int(obj['forward_count']) # 分享
  81. like_cnt = int(obj['like_count']) # 点赞
  82. duration_ms = obj['object_desc']['media'][0]['spec'][0]["duration_ms"]
  83. duration = int(duration_ms) / 1000
  84. log_data = f"user:{url_id},,video_id:{objectId},,video_url:{video_url},,original_title:{old_title},,share_count:{share_cnt},,like_count:{like_cnt},,duration:{duration}"
  85. AliyunLogger.logging(channel_id, name, url_id, objectId, "扫描到一条视频", "2001", log_data)
  86. Common.logger("sph").info(
  87. f"扫描:{task_mark},用户主页id:{url_id},视频id{objectId} ,分享:{share_cnt},点赞:{like_cnt}")
  88. if status:
  89. AliyunLogger.logging(channel_id, name, url_id, objectId, "该视频已改造过", "2001", log_data)
  90. continue
  91. video_percent = '%.2f' % (share_cnt / like_cnt)
  92. special = float(0.25)
  93. if like_cnt >= 30000 or like_cnt >= 50000 or (share_cnt >= 300 and float(video_percent) >= special):
  94. if int(duration) < 30 or int(duration) > 720:
  95. Common.logger("sph").info(
  96. f"任务:{task_mark},用户主页id:{url_id},视频id{objectId} ,分享:{share_cnt},点赞:{like_cnt} ,时长:{duration} ")
  97. AliyunLogger.logging(channel_id, name, url, objectId, "不符合规则:时长不符合规则大于720秒/小于30秒",
  98. "2003", log_data)
  99. continue
  100. AliyunLogger.logging(channel_id, name, url_id, objectId, "符合规则等待改造", "2004", log_data)
  101. all_data = {"video_id": objectId, "cover": cover, "video_url": video_url, "rule": video_percent, "old_title": old_title, "decode_key": decode_key}
  102. list.append(all_data)
  103. if len(list) == int(number):
  104. Common.logger(mark).info(f"获取视频号视频总数:{len(list)}\n")
  105. return list
  106. else:
  107. AliyunLogger.logging(channel_id, name, url_id, objectId, "不符合规则:点赞小于30000/50000 或 分享/点赞小于0.25和分享小于300", "2003", log_data)
  108. Common.logger("sph").info(
  109. f"不符合规则:{task_mark},用户主页id:{url_id},视频id{objectId} ,分享:{share_cnt},点赞:{like_cnt}")
  110. continue
  111. return list
  112. except Exception as e:
  113. print(e)
  114. return list
  115. if __name__ == '__main__':
  116. SPH.get_sph_url('1',"人民日报",'10','2',"视频号",'视频号品类账号')