ks_keyword.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import requests
  2. import json
  3. from common import Common, AliyunLogger, Feishu
  4. from common.sql_help import sqlCollect
  5. class KsKeyword:
  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.190.241:8888/crawler/kuai_shou/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 = 100
  30. special = 0.0005
  31. short_duration_rule = 25
  32. elif "近1日" == publish_time:
  33. share_count_rule = 0
  34. special = 0.0003
  35. short_duration_rule = 25
  36. elif "近7日" == publish_time:
  37. share_count_rule = 50
  38. special = 0.0005
  39. short_duration_rule = 25
  40. elif "近1月" == publish_time:
  41. share_count_rule = 100
  42. special = 0.0005
  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. if code == 27006 and response['msg'] == '快手内容已被删除或无法访问':
  50. Feishu.finish_bot(f"kuai_shou/keyword {response['msg']},cookie 过期需要更换",
  51. "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb",
  52. "【快手搜索接口使用提示】")
  53. Common.logger("ks-key-word").info(f"快手搜索词数据获取失败,{response['msg']}\n")
  54. return list
  55. Common.logger("ks-key-word").info(f"快手搜索词数据获取失败,接口为kuai_shou/keyword\n")
  56. return list
  57. data_list = response['data']['data']
  58. for data in data_list:
  59. photo_id = data["photo_id"]
  60. status = sqlCollect.is_used(task_mark, photo_id, mark, "快手历史")
  61. view_count = data["view_count"]
  62. share_count = data["share_count"]
  63. old_title = data["caption"] # 标题
  64. video_percent = '%.4f' % (int(share_count) / (view_count))
  65. duration = data["duration"]
  66. duration = int(duration) / 1000
  67. log_data = f"user:{keyword},,video_id:{photo_id},,video_url:'',original_title:{old_title},,share_count:{share_count},,view_count:{view_count},,duration:{duration}"
  68. AliyunLogger.logging(channel_id, name, keyword, photo_id, "扫描到一条视频", "2001", log_data)
  69. if status:
  70. AliyunLogger.logging(channel_id, name, keyword, photo_id, "该视频已改造过", "2001", log_data)
  71. continue
  72. if float(video_percent) < special:
  73. AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:分享/浏览{special}", "2003", log_data)
  74. Common.logger("ks-key-word").info(
  75. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
  76. continue
  77. if int(share_count) < share_count_rule:
  78. AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:分享小于{share_count_rule}", "2003", log_data)
  79. Common.logger("ks-key-word").info(
  80. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
  81. continue
  82. if int(duration) < short_duration_rule or (duration) > 600:
  83. AliyunLogger.logging(channel_id, name, keyword, photo_id, f"不符合规则:时长不符合规则大于600秒/小于{short_duration_rule}", "2003",
  84. log_data)
  85. Common.logger("ks-key-word").info(
  86. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
  87. continue
  88. video_url, image_url = cls.get_video(photo_id)
  89. if video_url:
  90. log_data = f"user:{keyword},,video_id:{photo_id},,video_url:{video_url},,original_title:{old_title},,share_count:{share_count},,view_count:{view_count},,duration:{duration}"
  91. AliyunLogger.logging(channel_id, name, keyword, photo_id, "符合规则等待改造", "2004", log_data)
  92. all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
  93. "rule": video_percent,
  94. "old_title": old_title}
  95. list.append(all_data)
  96. return list
  97. except Exception as exc:
  98. Common.logger("ks-key-word").info(f"快手搜索词{keyword}获取失败{exc}\n")
  99. return list
  100. @classmethod
  101. def get_video(cls, video_id):
  102. url = "http://8.217.190.241:8888/crawler/kuai_shou/detail"
  103. payload = json.dumps({
  104. "content_id": str(video_id)
  105. })
  106. headers = {
  107. 'Content-Type': 'application/json'
  108. }
  109. response = requests.request("POST", url, headers=headers, data=payload)
  110. response = response.json()
  111. data = response["data"]["data"]
  112. video_url = data["video_url_list"][0]["video_url"]
  113. image_url = data["image_url_list"][0]["image_url"]
  114. return video_url, image_url
  115. if __name__ == '__main__':
  116. keyword = '毛主席故居'
  117. task_mark = '1'
  118. mark = 'pl-gjc'
  119. channel_id = '快手搜索'
  120. name = '1'
  121. task = {'combo': ['最新发布', '近1日', '1分钟内']}
  122. KsKeyword.get_key_word(keyword, task_mark, mark, channel_id, name, task)