ks_ls.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import random
  2. import time
  3. import requests
  4. import json
  5. from common import Common, Feishu
  6. from common.sql_help import sqlCollect
  7. class KSLS:
  8. @classmethod
  9. def get_ksls_list(cls, task_mark, url_id, number, mark):
  10. # 快手app
  11. url = "http://8.217.190.241:8888/crawler/kuai_shou/blogger"
  12. next_cursor = ""
  13. for i in range(50):
  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(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. if status == False:
  42. continue
  43. status = sqlCollect.is_used(task_mark, photo_id, mark, "快手历史")
  44. if status == False:
  45. continue
  46. view_count = data["view_count"]
  47. share_count = data["share_count"]
  48. old_title = data["caption"] # 标题
  49. video_percent = '%.4f' % (int(share_count) / (view_count))
  50. duration = data["duration"]
  51. duration = int(duration)/1000
  52. special = float(0.0005)
  53. if float(video_percent) < special or int(share_count) < 100 or int(duration) < 30 or (duration) > 720:
  54. Common.logger("ks-ls").info(
  55. f"不符合规则:{task_mark},用户主页id:{url_id},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
  56. continue
  57. video_url, image_url = cls.get_video(photo_id)
  58. if video_url:
  59. all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
  60. "rule": video_percent,
  61. "old_title": old_title}
  62. list.append(all_data)
  63. if len(list) == int(number):
  64. Common.logger("ks-ls").info(f"获取快手历史视频总数:{len(list)}\n")
  65. return list
  66. if has_more == False:
  67. return list
  68. except Exception as exc:
  69. Common.logger("ks-ls").info(f"快手历史数据获取失败:{exc}\n")
  70. return list
  71. @classmethod
  72. def get_video(cls, video_id):
  73. url = "http://8.217.190.241:8888/crawler/kuai_shou/detail"
  74. payload = json.dumps({
  75. "content_id": str(video_id)
  76. })
  77. headers = {
  78. 'Content-Type': 'application/json'
  79. }
  80. response = requests.request("POST", url, headers=headers, data=payload)
  81. response = response.json()
  82. data = response["data"]["data"]
  83. video_url = data["video_url_list"][0]["video_url"]
  84. image_url = data["image_url_list"][0]["image_url"]
  85. return video_url, image_url
  86. if __name__ == '__main__':
  87. Feishu.finish_bot('测试',
  88. "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb", "【 Token 使用提示 】")
  89. # DYLS.get_video("7314923922602954022")
  90. KSLS.get_ksls_list("1","3xzicxg2nandemc",1,"1")