ks_ls.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. for i in range(20):
  14. payload = json.dumps({
  15. "account_id": url_id,
  16. "sort_type": "最热",
  17. "cursor": next_cursor
  18. })
  19. headers = {
  20. 'Content-Type': 'application/json'
  21. }
  22. time.sleep(random.randint(1, 5))
  23. response = requests.request("POST", url, headers=headers, data=payload)
  24. response = response.json()
  25. list = []
  26. data_all_list = response["data"]
  27. if data_all_list == None or len(data_all_list) == 0:
  28. try:
  29. if int(response["cdoe"]) == 27006:
  30. Feishu.finish_bot("kuai_shou/blogger接口"+response["msg"],
  31. "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb", "【快手 Token 使用提示 】")
  32. except Exception as exc:
  33. return list
  34. has_more = data_all_list["has_more"]
  35. next_cursor = str(data_all_list["next_cursor"])
  36. try:
  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, "该视频已改造过", "2001", 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. AliyunLogger.logging(channel_id, name, url_id, photo_id, "符合规则等待改造", "2004", log_data)
  72. all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
  73. "rule": video_percent,
  74. "old_title": old_title}
  75. list.append(all_data)
  76. if len(list) == int(number):
  77. Common.logger("ks-ls").info(f"获取快手历史视频总数:{len(list)}\n")
  78. return list
  79. if has_more == False:
  80. return list
  81. except Exception as exc:
  82. Common.logger("ks-ls").info(f"快手历史数据获取失败:{exc}\n")
  83. return list
  84. @classmethod
  85. def get_video(cls, video_id):
  86. url = "http://47.236.68.175:8889/crawler/kuai_shou/detail"
  87. payload = json.dumps({
  88. "content_id": str(video_id)
  89. })
  90. headers = {
  91. 'Content-Type': 'application/json'
  92. }
  93. response = requests.request("POST", url, headers=headers, data=payload)
  94. response = response.json()
  95. data = response["data"]["data"]
  96. video_url = data["video_url_list"][0]["video_url"]
  97. image_url = data["image_url_list"][0]["image_url"]
  98. return video_url, image_url
  99. if __name__ == '__main__':
  100. # Feishu.finish_bot('测试',
  101. # "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb", "【 Token 使用提示 】")
  102. # DYLS.get_video("7314923922602954022")
  103. KSLS.get_ksls_list("1","3xzicxg2nandemc",1,"1")