ks_keyword.py 6.4 KB

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