ks_xcx_keyword.py 5.7 KB

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