shipinhao.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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://47.236.68.175:8889/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://47.236.68.175:8889/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. if data_lsit == None:
  72. return list
  73. for obj in data_lsit:
  74. objectId = obj['id']
  75. status = sqlCollect.is_used(task_mark, objectId, mark, "视频号")
  76. old_title = obj['objectDesc']['description']
  77. url_p = obj['objectDesc']['media'][0]['Url']
  78. url_token = obj['objectDesc']['media'][0]['urlToken']
  79. video_url = f"{url_p}{url_token}"
  80. decode_key = obj['objectDesc']['media'][0]['decodeKey']
  81. cover = obj['objectDesc']['media'][0]['coverUrl']
  82. share_cnt = int(obj['forwardCount']) # 分享
  83. like_cnt = int(obj['likeCount']) # 点赞
  84. duration = int(obj['objectDesc']['media'][0]['VideoPlayLen'])
  85. # duration = int(duration_ms) / 1000
  86. 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}"
  87. AliyunLogger.logging(channel_id, name, url_id, objectId, "扫描到一条视频", "2001", log_data)
  88. Common.logger("sph").info(
  89. f"扫描:{task_mark},用户主页id:{url_id},视频id{objectId} ,分享:{share_cnt},点赞:{like_cnt}")
  90. if status:
  91. AliyunLogger.logging(channel_id, name, url_id, objectId, "该视频已改造过", "2002", log_data)
  92. continue
  93. video_percent = '%.2f' % (share_cnt / like_cnt)
  94. special = float(0.25)
  95. if like_cnt >= 30000 or like_cnt >= 50000 or (share_cnt >= 300 and float(video_percent) >= special):
  96. if int(duration) < 30 or int(duration) > 720:
  97. Common.logger("sph").info(
  98. f"任务:{task_mark},用户主页id:{url_id},视频id{objectId} ,分享:{share_cnt},点赞:{like_cnt} ,时长:{duration} ")
  99. AliyunLogger.logging(channel_id, name, url, objectId, "不符合规则:时长不符合规则大于720秒/小于30秒",
  100. "2003", log_data)
  101. continue
  102. all_data = {"video_id": objectId, "cover": cover, "video_url": video_url, "rule": video_percent, "old_title": old_title, "decode_key": decode_key}
  103. list.append(all_data)
  104. AliyunLogger.logging(channel_id, name, url_id, objectId, "符合规则等待改造", "2004", log_data)
  105. if len(list) == int(number):
  106. Common.logger(mark).info(f"获取视频号视频总数:{len(list)}\n")
  107. return list
  108. else:
  109. AliyunLogger.logging(channel_id, name, url_id, objectId, "不符合规则:点赞小于30000/50000 或 分享/点赞小于0.25和分享小于300", "2003", log_data)
  110. Common.logger("sph").info(
  111. f"不符合规则:{task_mark},用户主页id:{url_id},视频id{objectId} ,分享:{share_cnt},点赞:{like_cnt}")
  112. continue
  113. return list
  114. except Exception as e:
  115. print(e)
  116. return list
  117. if __name__ == '__main__':
  118. SPH.get_sph_url('1',"人民日报",'10','2',"视频号",'视频号品类账号')