ks_ls.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import random
  2. import time
  3. import requests
  4. import json
  5. from common import Feishu, AliyunLogger
  6. from common.sql_help import sqlCollect
  7. class KSLS:
  8. @classmethod
  9. def get_ksls_list(cls, task_mark, url_id, number, mark, channel_id, name):
  10. # 快手app
  11. url = "http://8.217.192.46:8889/crawler/kuai_shou/blogger"
  12. next_cursor = ""
  13. try:
  14. for i in range(5):
  15. payload = json.dumps({
  16. "account_id": url_id,
  17. "sort_type": "最热",
  18. "cursor": next_cursor
  19. })
  20. headers = {
  21. 'Content-Type': 'application/json'
  22. }
  23. time.sleep(random.randint(1, 5))
  24. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  25. response = response.json()
  26. list = []
  27. data_all_list = response["data"]
  28. if data_all_list == None or len(data_all_list) == 0:
  29. try:
  30. if int(response["cdoe"]) == 27006:
  31. Feishu.finish_bot("kuai_shou/blogger接口"+response["msg"],
  32. "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb", "【快手 Token 使用提示 】")
  33. except Exception as exc:
  34. return list
  35. has_more = data_all_list["has_more"]
  36. next_cursor = str(data_all_list["next_cursor"])
  37. data_list = data_all_list["data"]
  38. for data in data_list:
  39. photo_id = data["photo_id"]
  40. status = sqlCollect.is_used(task_mark, photo_id, mark, "快手历史")
  41. view_count = data["view_count"]
  42. share_count = data["share_count"]
  43. old_title = data["caption"] # 标题
  44. video_percent = '%.4f' % (int(share_count) / (view_count))
  45. duration = data["duration"]
  46. duration = int(duration)/1000
  47. special = float(0.0005)
  48. log_data = f"user:{url_id},,video_id:{photo_id},,video_url:'',original_title:{old_title},,share_count:{share_count},,view_count:{view_count},,duration:{duration}"
  49. AliyunLogger.logging(channel_id, name, url_id, photo_id, "扫描到一条视频", "2001", log_data)
  50. # if status:
  51. # AliyunLogger.logging(channel_id, name, url_id, photo_id, "该视频已改造过", "2002", log_data)
  52. # continue
  53. if float(video_percent) < special:
  54. AliyunLogger.logging(channel_id, name, url_id, photo_id, "不符合规则:分享/浏览小于0.0005", "2003", log_data)
  55. continue
  56. if int(share_count) < 100:
  57. AliyunLogger.logging(channel_id, name, url_id, photo_id, "不符合规则:分享小于100", "2003", log_data)
  58. continue
  59. if int(duration) < 30 or (duration) > 720:
  60. AliyunLogger.logging(channel_id, name, url_id, photo_id, "不符合规则:时长不符合规则大于720秒/小于30秒", "2003", log_data)
  61. continue
  62. video_url, image_url = cls.get_video(photo_id)
  63. if video_url:
  64. log_data = f"user:{url_id},,video_id:{photo_id},,video_url:{video_url},,original_title:{old_title},,share_count:{share_count},,view_count:{view_count},,duration:{duration}"
  65. all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
  66. "rule": video_percent,
  67. "old_title": old_title}
  68. list.append(all_data)
  69. AliyunLogger.logging(channel_id, name, url_id, photo_id, "符合规则等待改造", "2004", log_data)
  70. if len(list) == int(number):
  71. return list
  72. else:
  73. AliyunLogger.logging(channel_id, name, url_id, photo_id, "无法获取到视频链接", "2003", log_data)
  74. continue
  75. if has_more == False:
  76. return list
  77. return list
  78. except Exception as exc:
  79. return list
  80. @classmethod
  81. def get_video(cls, video_id):
  82. url = "http://8.217.192.46:8889/crawler/kuai_shou/detail"
  83. payload = json.dumps({
  84. "content_id": str(video_id)
  85. })
  86. headers = {
  87. 'Content-Type': 'application/json'
  88. }
  89. time.sleep(random.uniform(1, 10))
  90. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  91. response = response.json()
  92. data = response["data"]["data"]
  93. video_url = data["video_url_list"][0]["video_url"]
  94. image_url = data["image_url_list"][0]["image_url"]
  95. return video_url, image_url
  96. if __name__ == '__main__':
  97. # Feishu.finish_bot('测试',
  98. # "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb", "【 Token 使用提示 】")
  99. # DYLS.get_video("7314923922602954022")
  100. KSLS.get_ksls_list("1","3xzicxg2nandemc",1,"1",'','')