dy_keyword.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import requests
  2. import json
  3. from common import AliyunLogger, Material
  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 not keyword.strip():
  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. day_count = Material.get_count_restrict(channel_id)
  56. if day_count:
  57. status = sqlCollect.is_used_days(task_mark, video_id, mark, channel_id, day_count)
  58. else:
  59. status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
  60. video_uri = data[i].get('video', {}).get('play_addr', {}).get('uri')
  61. ratio = f'{data[i].get("video", {}).get("height")}p'
  62. # video_url = f'https://www.iesdouyin.com/aweme/v1/play/?video_id={video_uri}&ratio={ratio}&line=0' # 视频链接
  63. video_url = data[i].get('video', {}).get('play_addr', {}).get('url_list', [None])[0]
  64. old_title = data[i].get('desc', "").strip().replace("\n", "") \
  65. .replace("/", "").replace("\\", "").replace("\r", "") \
  66. .replace(":", "").replace("*", "").replace("?", "") \
  67. .replace("?", "").replace('"', "").replace("<", "") \
  68. .replace(">", "").replace("|", "").replace(" ", "") \
  69. .replace("&NBSP", "").replace(".", "。").replace(" ", "") \
  70. .replace("'", "").replace("#", "").replace("Merge", "")
  71. digg_count = int(data[i].get('statistics').get('digg_count')) # 点赞
  72. share_count = int(data[i].get('statistics').get('share_count')) # 转发
  73. duration = data[i].get('duration')
  74. duration = duration / 1000
  75. 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}"
  76. AliyunLogger.logging(channel_id, name, keyword, video_id, "扫描到一条视频", "2001", log_data)
  77. if status:
  78. AliyunLogger.logging(channel_id, name, keyword, video_id, "该视频已改造过", "2002", log_data)
  79. continue
  80. video_percent = '%.2f' % (int(share_count) / int(digg_count))
  81. if int(share_count) < share_count_rule:
  82. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:分享小于{share_count_rule}", "2003", log_data)
  83. continue
  84. if float(video_percent) < special:
  85. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:分享/点赞小于{special}", "2003", log_data)
  86. continue
  87. if int(duration) < short_duration_rule or int(duration) > 720:
  88. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:时长不符合规则大于720秒/小于{short_duration_rule}秒", "2003", log_data)
  89. continue
  90. cover_url = data[i].get('video').get('cover').get('url_list')[0] # 视频封面
  91. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url, "rule": video_percent,
  92. "old_title": old_title}
  93. list.append(all_data)
  94. AliyunLogger.logging(channel_id, name, keyword, video_id, "符合规则等待改造", "2004", log_data)
  95. return list
  96. except Exception as exc:
  97. return list
  98. if __name__ == '__main__':
  99. DyKeyword.get_key_word('keyword', 'sort_type', 'publish_time', 'duration', 'task_mark', 'mark')