ks_keyword.py 6.6 KB

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