ks_keyword.py 5.8 KB

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