ks.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import random
  2. import time
  3. import requests
  4. import json
  5. from common import Feishu, AliyunLogger, Material
  6. from common.sql_help import sqlCollect
  7. class KSX:
  8. @classmethod
  9. def get_ks_list(cls, task_mark, url_id, number, mark, channel_id, name):
  10. # 快手app
  11. list = []
  12. url = "http://8.217.192.46:8889/crawler/kuai_shou/blogger"
  13. next_cursor = ""
  14. try:
  15. if not url_id or not url_id.strip():
  16. return list
  17. for i in range(5):
  18. payload = json.dumps({
  19. "account_id": url_id,
  20. "sort_type": "最新",
  21. "cursor": next_cursor
  22. })
  23. headers = {
  24. 'Content-Type': 'application/json'
  25. }
  26. time.sleep(random.randint(1, 5))
  27. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  28. response = response.json()
  29. data_all_list = response["data"]
  30. if data_all_list == None or len(data_all_list) == 0:
  31. try:
  32. if int(response["cdoe"]) == 27006:
  33. Feishu.finish_bot("kuai_shou/blogger接口"+response["msg"],
  34. "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb", "【快手 Token 使用提示 】")
  35. except Exception as exc:
  36. return list
  37. has_more = data_all_list["has_more"]
  38. next_cursor = str(data_all_list["next_cursor"])
  39. data_list = data_all_list["data"]
  40. for data in data_list:
  41. photo_id = data["photo_id"]
  42. day_count = Material.get_count_restrict(channel_id)
  43. if day_count:
  44. status = sqlCollect.is_used_days(photo_id, mark, channel_id, day_count)
  45. else:
  46. status = sqlCollect.is_used(photo_id, mark, channel_id)
  47. view_count = data["view_count"]
  48. share_count = data["share_count"]
  49. old_title = data["caption"] # 标题
  50. video_percent = '%.4f' % (int(share_count) / (view_count))
  51. duration = data["duration"]
  52. duration = int(duration)/1000
  53. special = float(0.0001)
  54. 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}"
  55. AliyunLogger.logging(channel_id, name, url_id, photo_id, "扫描到一条视频", "2001", log_data)
  56. if status:
  57. AliyunLogger.logging(channel_id, name, url_id, photo_id, "该视频已改造过", "2002", log_data)
  58. continue
  59. if float(video_percent) < special:
  60. AliyunLogger.logging(channel_id, name, url_id, photo_id, "不符合规则:分享/浏览小于0.0001", "2003", log_data)
  61. continue
  62. if int(share_count) < 500:
  63. AliyunLogger.logging(channel_id, name, url_id, photo_id, "不符合规则:分享小于500", "2003", log_data)
  64. continue
  65. if int(duration) < 30 or (duration) > 720:
  66. AliyunLogger.logging(channel_id, name, url_id, photo_id, "不符合规则:时长不符合规则大于720秒/小于30秒", "2003", log_data)
  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. all_data = {"video_id": photo_id, "cover": image_url, "video_url": video_url,
  72. "rule": video_percent,
  73. "old_title": old_title}
  74. list.append(all_data)
  75. AliyunLogger.logging(channel_id, name, url_id, photo_id, "符合规则等待改造", "2004", log_data)
  76. if len(list) == int(number):
  77. return list
  78. else:
  79. AliyunLogger.logging(channel_id, name, url_id, photo_id, "无法获取到视频链接", "2003", log_data)
  80. continue
  81. if has_more == False:
  82. return list
  83. return list
  84. except Exception as exc:
  85. return list
  86. @classmethod
  87. def get_video(cls, video_id):
  88. url = "http://8.217.192.46:8889/crawler/kuai_shou/detail"
  89. payload = json.dumps({
  90. "content_id": str(video_id)
  91. })
  92. headers = {
  93. 'Content-Type': 'application/json'
  94. }
  95. time.sleep(random.uniform(1, 10))
  96. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  97. response = response.json()
  98. data = response["data"]["data"]
  99. video_url = data["video_url_list"][0]["video_url"]
  100. image_url = data["image_url_list"][0]["image_url"]
  101. return video_url, image_url
  102. if __name__ == '__main__':
  103. # Feishu.finish_bot('测试',
  104. # "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb", "【 Token 使用提示 】")
  105. # DYLS.get_video("7314923922602954022")
  106. KSX.get_ks_list("1","3xzicxg2nandemc",1,"1",'','')