shipinhao.py 5.8 KB

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