ks_ls.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import random
  2. import time
  3. import requests
  4. import json
  5. from common import Common, 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://47.236.68.175:8889/crawler/kuai_shou/blogger"
  12. next_cursor = ""
  13. try:
  14. for i in range(20):
  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)
  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. Common.logger("ks-ls").info(
  56. f"不符合规则:{task_mark},用户主页id:{url_id},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
  57. continue
  58. if int(share_count) < 100:
  59. AliyunLogger.logging(channel_id, name, url_id, photo_id, "不符合规则:分享小于100", "2003", log_data)
  60. Common.logger("ks-ls").info(
  61. f"不符合规则:{task_mark},用户主页id:{url_id},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
  62. continue
  63. if int(duration) < 30 or (duration) > 720:
  64. AliyunLogger.logging(channel_id, name, url_id, photo_id, "不符合规则:时长不符合规则大于720秒/小于30秒", "2003", log_data)
  65. Common.logger("ks-ls").info(
  66. f"不符合规则:{task_mark},用户主页id:{url_id},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
  67. continue
  68. video_url, image_url = cls.get_video(photo_id)
  69. if video_url:
  70. 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}"
  71. all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
  72. "rule": video_percent,
  73. "old_title": old_title}
  74. list.append(all_data)
  75. AliyunLogger.logging(channel_id, name, url_id, photo_id, "符合规则等待改造", "2004", log_data)
  76. if len(list) == int(number):
  77. Common.logger("ks-ls").info(f"获取快手历史视频总数:{len(list)}\n")
  78. return list
  79. else:
  80. AliyunLogger.logging(channel_id, name, url_id, photo_id, "无法获取到视频链接", "2003", log_data)
  81. continue
  82. if has_more == False:
  83. return list
  84. return list
  85. except Exception as exc:
  86. Common.logger("ks-ls").info(f"快手历史数据获取失败:{exc}\n")
  87. return list
  88. @classmethod
  89. def get_video(cls, video_id):
  90. url = "http://47.236.68.175:8889/crawler/kuai_shou/detail"
  91. payload = json.dumps({
  92. "content_id": str(video_id)
  93. })
  94. headers = {
  95. 'Content-Type': 'application/json'
  96. }
  97. response = requests.request("POST", url, headers=headers, data=payload)
  98. response = response.json()
  99. data = response["data"]["data"]
  100. video_url = data["video_url_list"][0]["video_url"]
  101. image_url = data["image_url_list"][0]["image_url"]
  102. return video_url, image_url
  103. if __name__ == '__main__':
  104. # Feishu.finish_bot('测试',
  105. # "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb", "【 Token 使用提示 】")
  106. # DYLS.get_video("7314923922602954022")
  107. KSLS.get_ksls_list("1","3xzicxg2nandemc",1,"1")