ks.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import random
  2. import time
  3. import requests
  4. import json
  5. from utils.aliyun_log import AliyunLogger
  6. from utils.feishu_form import Material
  7. from utils.sql_help import sqlCollect
  8. class KS:
  9. @classmethod
  10. def get_ks_list(cls, task, fs_channel_name):
  11. # 快手app
  12. list = []
  13. url = "http://8.217.192.46:8889/crawler/kuai_shou/blogger"
  14. try:
  15. if not task["channel_url"] or not task["channel_url"].strip():
  16. return []
  17. if "历史" in task['channel']:
  18. sort_type = "最热"
  19. else:
  20. sort_type = "最新"
  21. next_cursor = ""
  22. for i in range(5):
  23. payload = json.dumps({
  24. "account_id": task["channel_url"],
  25. "sort_type": sort_type,
  26. "cursor": next_cursor
  27. })
  28. headers = {
  29. 'Content-Type': 'application/json'
  30. }
  31. time.sleep(random.randint(1, 5))
  32. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  33. response = response.json()
  34. data_all_list = response["data"]
  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. day_count = Material.get_count_restrict(task["channel"])
  41. if day_count:
  42. status = sqlCollect.is_used_days(photo_id, task['channel'], day_count)
  43. else:
  44. status = sqlCollect.is_used(photo_id, task['channel'])
  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.0001)
  52. log_data = f"user:{task['channel_url']},,video_id:{photo_id},,video_url:'',original_title:{old_title},,share_count:{share_count},,view_count:{view_count},,duration:{duration}"
  53. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], photo_id, "扫描到一条视频", "2001", log_data)
  54. if status:
  55. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], photo_id, "该视频已改造过", "2002", log_data)
  56. continue
  57. if float(video_percent) < special:
  58. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], photo_id, "不符合规则:分享/浏览小于0.0001", "2003", log_data)
  59. continue
  60. if int(share_count) < 500:
  61. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], photo_id, "不符合规则:分享小于500", "2003", log_data)
  62. continue
  63. if int(duration) < 30 or (duration) > 720:
  64. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], photo_id, "不符合规则:时长不符合规则大于720秒/小于30秒", "2003", log_data)
  65. continue
  66. video_url, image_url = cls.get_video(photo_id)
  67. if video_url:
  68. log_data = f"user:{task['channel']},,video_id:{photo_id},,video_url:{video_url},,original_title:{old_title},,share_count:{share_count},,view_count:{view_count},,duration:{duration}"
  69. all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
  70. "rule": video_percent,
  71. "old_title": old_title}
  72. list.append(all_data)
  73. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], photo_id, "符合规则等待改造", "2004", log_data)
  74. if len(list) == int(task['count']):
  75. return list
  76. else:
  77. AliyunLogger.logging(task['channel'], fs_channel_name, task['channel_url'], photo_id, "无法获取到视频链接", "2003", log_data)
  78. continue
  79. if has_more == False:
  80. return list
  81. return list
  82. except Exception as exc:
  83. return list
  84. @classmethod
  85. def get_video(cls, video_id):
  86. url = "http://8.217.192.46: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. time.sleep(random.uniform(1, 10))
  94. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  95. response = response.json()
  96. data = response["data"]["data"]
  97. video_url = data["video_url_list"][0]["video_url"]
  98. image_url = data["image_url_list"][0]["image_url"]
  99. return video_url, image_url