dy_keyword.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import requests
  2. import json
  3. from common import AliyunLogger
  4. from common.sql_help import sqlCollect
  5. class DyKeyword:
  6. @classmethod
  7. def get_key_word(cls, keyword, task_mark, mark, channel_id, name, task):
  8. combo = task['combo']
  9. content_type = combo[0]
  10. publish_time = combo[1]
  11. duration = combo[2]
  12. share_count_rule = 0
  13. special = 0
  14. short_duration_rule = 0
  15. url = "http://8.217.192.46:8889/crawler/dou_yin/keyword"
  16. list = []
  17. if not keyword or keyword == "":
  18. return list
  19. payload = json.dumps({
  20. "keyword": keyword,
  21. "content_type": "视频",
  22. "sort_type": content_type,
  23. "publish_time": publish_time,
  24. "duration": duration,
  25. "cursor": ""
  26. })
  27. headers = {
  28. 'Content-Type': 'application/json'
  29. }
  30. if " 不限" == publish_time:
  31. share_count_rule = 200
  32. special = 0.15
  33. short_duration_rule = 30
  34. elif "一天内" == publish_time:
  35. share_count_rule = 0
  36. special = 0.10
  37. short_duration_rule = 25
  38. elif "一周内" == publish_time:
  39. share_count_rule = 100
  40. special = 0.15
  41. short_duration_rule = 25
  42. elif "半年内" == publish_time:
  43. share_count_rule = 200
  44. special = 0.15
  45. short_duration_rule = 25
  46. try:
  47. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  48. response = response.json()
  49. code = response['code']
  50. if code != 0:
  51. return list
  52. data = response['data']['data']
  53. for i in range(len(data)):
  54. video_id = data[i].get('aweme_id') # 文章id
  55. status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
  56. video_uri = data[i].get('video', {}).get('play_addr', {}).get('uri')
  57. ratio = f'{data[i].get("video", {}).get("height")}p'
  58. # video_url = f'https://www.iesdouyin.com/aweme/v1/play/?video_id={video_uri}&ratio={ratio}&line=0' # 视频链接
  59. video_url = data[i].get('video', {}).get('play_addr', {}).get('url_list', [None])[0]
  60. old_title = data[i].get('desc', "").strip().replace("\n", "") \
  61. .replace("/", "").replace("\\", "").replace("\r", "") \
  62. .replace(":", "").replace("*", "").replace("?", "") \
  63. .replace("?", "").replace('"', "").replace("<", "") \
  64. .replace(">", "").replace("|", "").replace(" ", "") \
  65. .replace("&NBSP", "").replace(".", "。").replace(" ", "") \
  66. .replace("'", "").replace("#", "").replace("Merge", "")
  67. digg_count = int(data[i].get('statistics').get('digg_count')) # 点赞
  68. share_count = int(data[i].get('statistics').get('share_count')) # 转发
  69. duration = data[i].get('duration')
  70. duration = duration / 1000
  71. log_data = f"user:{keyword},,video_id:{video_id},,video_url:{video_url},,original_title:{old_title},,share_count:{share_count},,digg_count:{digg_count},,duration:{duration}"
  72. AliyunLogger.logging(channel_id, name, keyword, video_id, "扫描到一条视频", "2001", log_data)
  73. if status:
  74. AliyunLogger.logging(channel_id, name, keyword, video_id, "该视频已改造过", "2002", log_data)
  75. continue
  76. video_percent = '%.2f' % (int(share_count) / int(digg_count))
  77. if int(share_count) < share_count_rule:
  78. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:分享小于{share_count_rule}", "2003", log_data)
  79. continue
  80. if float(video_percent) < special:
  81. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:分享/点赞小于{special}", "2003", log_data)
  82. continue
  83. if int(duration) < short_duration_rule or int(duration) > 720:
  84. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:时长不符合规则大于720秒/小于{short_duration_rule}秒", "2003", log_data)
  85. continue
  86. cover_url = data[i].get('video').get('cover').get('url_list')[0] # 视频封面
  87. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url, "rule": video_percent,
  88. "old_title": old_title}
  89. list.append(all_data)
  90. AliyunLogger.logging(channel_id, name, keyword, video_id, "符合规则等待改造", "2004", log_data)
  91. return list
  92. except Exception as exc:
  93. return list
  94. if __name__ == '__main__':
  95. DyKeyword.get_key_word('keyword', 'sort_type', 'publish_time', 'duration', 'task_mark', 'mark')