dy_keyword.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import requests
  2. import json
  3. from common import Common, 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://47.236.68.175:8889/crawler/dou_yin/keyword"
  16. list = []
  17. payload = json.dumps({
  18. "keyword": keyword,
  19. "content_type": "视频",
  20. "sort_type": content_type,
  21. "publish_time": publish_time,
  22. "duration": duration,
  23. "cursor": ""
  24. })
  25. headers = {
  26. 'Content-Type': 'application/json'
  27. }
  28. if " 不限" == publish_time:
  29. share_count_rule = 200
  30. special = 0.15
  31. short_duration_rule = 30
  32. elif "一天内" == publish_time:
  33. share_count_rule = 0
  34. special = 0.10
  35. short_duration_rule = 25
  36. elif "一周内" == publish_time:
  37. share_count_rule = 100
  38. special = 0.15
  39. short_duration_rule = 25
  40. elif "半年内" == publish_time:
  41. share_count_rule = 200
  42. special = 0.15
  43. short_duration_rule = 25
  44. try:
  45. response = requests.request("POST", url, headers=headers, data=payload)
  46. response = response.json()
  47. code = response['code']
  48. if code != 0:
  49. Common.logger("dy-key-word").info(f"抖音搜索词数据获取失败,接口为/dou_yin/keyword\n")
  50. return list
  51. data = response['data']['data']
  52. for i in range(len(data)):
  53. video_id = data[i].get('aweme_id') # 文章id
  54. status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
  55. video_uri = data[i].get('video', {}).get('play_addr', {}).get('uri')
  56. ratio = f'{data[i].get("video", {}).get("height")}p'
  57. video_url = f'https://www.iesdouyin.com/aweme/v1/play/?video_id={video_uri}&ratio={ratio}&line=0' # 视频链接
  58. old_title = data[i].get('desc', "").strip().replace("\n", "") \
  59. .replace("/", "").replace("\\", "").replace("\r", "") \
  60. .replace(":", "").replace("*", "").replace("?", "") \
  61. .replace("?", "").replace('"', "").replace("<", "") \
  62. .replace(">", "").replace("|", "").replace(" ", "") \
  63. .replace("&NBSP", "").replace(".", "。").replace(" ", "") \
  64. .replace("'", "").replace("#", "").replace("Merge", "")
  65. digg_count = int(data[i].get('statistics').get('digg_count')) # 点赞
  66. share_count = int(data[i].get('statistics').get('share_count')) # 转发
  67. duration = data[i].get('duration')
  68. duration = duration / 1000
  69. 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}"
  70. AliyunLogger.logging(channel_id, name, keyword, video_id, "扫描到一条视频", "2001", log_data)
  71. Common.logger("dy-key-word").info(
  72. f"扫描:{task_mark},搜索词:{keyword},视频id{video_id} ,分享:{share_count},点赞{digg_count}")
  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. Common.logger("dy-key-word").info(
  80. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{video_id} ,分享:{share_count},点赞{digg_count} ,时长:{int(duration)} ")
  81. continue
  82. if float(video_percent) < special:
  83. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:分享/点赞小于{special}", "2003", log_data)
  84. Common.logger("dy-key-word").info(
  85. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{video_id} ,分享:{share_count},点赞{digg_count} ,时长:{int(duration)} ")
  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. Common.logger("dy-key-word").info(
  90. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{video_id} ,分享:{share_count},点赞{digg_count} ,时长:{int(duration)} ")
  91. continue
  92. cover_url = data[i].get('video').get('cover').get('url_list')[0] # 视频封面
  93. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url, "rule": video_percent,
  94. "old_title": old_title}
  95. list.append(all_data)
  96. AliyunLogger.logging(channel_id, name, keyword, video_id, "符合规则等待改造", "2004", log_data)
  97. return list
  98. except Exception as exc:
  99. Common.logger("dy-key-word").info(f"抖音搜索词{keyword}获取失败{exc}\n")
  100. return list
  101. if __name__ == '__main__':
  102. DyKeyword.get_key_word('keyword', 'sort_type', 'publish_time', 'duration', 'task_mark', 'mark')