ks_ls.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import random
  2. import time
  3. import requests
  4. import json
  5. from common import Common
  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": "3xk3dc57x4vpkg6",
  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. has_more = data_all_list["has_more"]
  27. next_cursor = str(data_all_list["next_cursor"])
  28. try:
  29. data_list = data_all_list["data"]
  30. for data in data_list:
  31. photo_id = data["photo_id"]
  32. status = sqlCollect.is_used(task_mark, photo_id, mark, "快手")
  33. if status == False:
  34. continue
  35. status = sqlCollect.is_used(task_mark, photo_id, mark, "快手历史")
  36. if status == False:
  37. continue
  38. view_count = data["view_count"]
  39. share_count = data["share_count"]
  40. old_title = data["caption"] # 标题
  41. video_percent = '%.4f' % (int(share_count) / (view_count))
  42. duration = data["duration"]
  43. duration = int(duration)/1000
  44. special = float(0.0005)
  45. if float(video_percent) < special or int(share_count) < 100 or int(duration) < 30 or (duration) > 720:
  46. Common.logger("ks-ls").info(
  47. f"不符合规则:{task_mark},用户主页id:{url_id},视频id{photo_id} ,分享:{share_count},浏览{view_count} ,时长:{int(duration)} ")
  48. continue
  49. video_url, image_url = cls.get_video(photo_id)
  50. if video_url:
  51. all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
  52. "rule": video_percent,
  53. "old_title": old_title}
  54. list.append(all_data)
  55. if len(list) == int(number):
  56. Common.logger("ks-ls").info(f"获取快手历史视频总数:{len(list)}\n")
  57. return list
  58. if has_more == False:
  59. return list
  60. except Exception as exc:
  61. Common.logger("ks-ls").info(f"抖音历史数据获取失败:{exc}\n")
  62. return list
  63. @classmethod
  64. def get_video(cls, video_id):
  65. url = "http://8.217.190.241:8888/crawler/kuai_shou/detail"
  66. payload = json.dumps({
  67. "content_id": str(video_id)
  68. })
  69. headers = {
  70. 'Content-Type': 'application/json'
  71. }
  72. response = requests.request("POST", url, headers=headers, data=payload)
  73. response = response.json()
  74. data = response["data"]["data"]
  75. video_url = data["video_url_list"][0]["video_url"]
  76. image_url = data["image_url_list"][0]["image_url"]
  77. return video_url, image_url
  78. if __name__ == '__main__':
  79. # DYLS.get_video("7314923922602954022")
  80. KSLS.get_ksls_list("1","3xk3dc57x4vpkg6",1,"1")