ks_ls.py 4.2 KB

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