dy.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import random
  2. import time
  3. import requests
  4. import json
  5. from utils.aliyun_log import AliyunLogger
  6. from utils.feishu_form import Material
  7. from utils.sql_help import sqlCollect
  8. class DY:
  9. @classmethod
  10. def get_dy_list(cls, task, fs_channel_name):
  11. url = "http://8.217.192.46:8889/crawler/dou_yin/blogger"
  12. list = []
  13. next_cursor = ''
  14. if not task["channel_url"] or not task["channel_url"].strip():
  15. return []
  16. if "历史" in task['channel']:
  17. sort_type = "最热"
  18. else:
  19. sort_type = "最新"
  20. for i in range(5):
  21. try:
  22. payload = json.dumps({
  23. "account_id": task["channel_url"],
  24. "source": "app",
  25. "sort_type": sort_type,
  26. "cursor": next_cursor
  27. })
  28. headers = {
  29. 'Content-Type': 'application/json'
  30. }
  31. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  32. time.sleep(random.randint(1, 5))
  33. response = response.json()
  34. code = response['code']
  35. if code != 0:
  36. return list
  37. data_list = response['data']
  38. next_cursor = str(data_list['next_cursor'])
  39. data = data_list['data']
  40. for i in range(len(data)):
  41. video_id = data[i].get('aweme_id') # 文章id
  42. day_count = Material.get_count_restrict(task["channel"])
  43. if day_count:
  44. status = sqlCollect.is_used_days(video_id, task['channel'], day_count)
  45. else:
  46. status = sqlCollect.is_used(video_id, task['channel'])
  47. video_url = data[i].get('video', {}).get('play_addr', {}).get('url_list', [None])[0]
  48. # 视频链接
  49. digg_count = int(data[i].get('statistics').get('digg_count')) # 点赞
  50. share_count = int(data[i].get('statistics').get('share_count')) # 转发
  51. duration = data[i].get('duration')
  52. duration = duration / 1000
  53. old_title = data[i].get('desc', "").strip().replace("\n", "") \
  54. .replace("/", "").replace("\\", "").replace("\r", "") \
  55. .replace(":", "").replace("*", "").replace("?", "") \
  56. .replace("?", "").replace('"', "").replace("<", "") \
  57. .replace(">", "").replace("|", "").replace(" ", "") \
  58. .replace("&NBSP", "").replace(".", "。").replace(" ", "") \
  59. .replace("'", "").replace("#", "").replace("Merge", "")
  60. log_data = f"user:{task['channel_url']},,video_id:{video_id},,video_url:{video_url},,original_title:{old_title},,share_count:{share_count},,digg_count:{digg_count},,duration:{duration}"
  61. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], video_id, "扫描到一条视频", "2001", log_data)
  62. if status:
  63. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], video_id, "该视频已改造过", "2002", log_data)
  64. continue
  65. video_percent = '%.2f' % (int(share_count) / int(digg_count))
  66. special = float(0.15)
  67. if int(share_count) < 200:
  68. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], video_id, "不符合规则:分享小于200", "2003", log_data)
  69. continue
  70. if float(video_percent) < special:
  71. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], video_id, "不符合规则:分享/点赞小于0.15", "2003", log_data)
  72. continue
  73. if int(duration) < 30 or int(duration) > 720:
  74. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], video_id, "不符合规则:时长不符合规则大于720秒/小于30秒", "2003", log_data)
  75. continue
  76. cover_url = data[i].get('video').get('cover').get('url_list')[0] # 视频封面
  77. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url, "rule": video_percent,
  78. "old_title": old_title}
  79. list.append(all_data)
  80. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], video_id, "符合规则等待改造", "2004", log_data)
  81. if len(list) == int(task['count']):
  82. return list
  83. if next_cursor == False:
  84. return list
  85. except Exception as exc:
  86. return list
  87. return list
  88. return list
  89. @classmethod
  90. def get_video(cls, video_id):
  91. url = "http://8.217.192.46:8889/crawler/dou_yin/detail"
  92. for i in range(3):
  93. payload = json.dumps({
  94. "content_id": str(video_id)
  95. })
  96. headers = {
  97. 'Content-Type': 'application/json'
  98. }
  99. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  100. response = response.json()
  101. code = response["code"]
  102. if code == 10000:
  103. time.sleep(60)
  104. data = response["data"]["data"]
  105. video_url = data["video_url_list"][0]["video_url"]
  106. image_url = data["image_url_list"][0]["image_url"]
  107. return video_url, image_url
  108. return None, None