dy_ls.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import random
  2. import time
  3. import requests
  4. import json
  5. from common import Feishu, AliyunLogger
  6. from common.sql_help import sqlCollect
  7. class DYLS:
  8. @classmethod
  9. def get_dy_zr_list(cls, task_mark, url_id, number, mark, channel_id, name):
  10. url = "http://8.217.192.46:8889/crawler/dou_yin/blogger"
  11. list = []
  12. next_cursor = ''
  13. for i in range(5):
  14. try:
  15. payload = json.dumps({
  16. "account_id": url_id,
  17. "source": "app",
  18. "sort": "最热",
  19. "cursor": next_cursor
  20. })
  21. headers = {
  22. 'Content-Type': 'application/json'
  23. }
  24. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  25. time.sleep(random.randint(1, 5))
  26. response = response.json()
  27. code = response['code']
  28. if code != 0:
  29. return list
  30. data_list = response['data']
  31. next_cursor = str(data_list['next_cursor'])
  32. data = data_list['data']
  33. for i in range(len(data)):
  34. video_id = data[i].get('aweme_id') # 文章id
  35. # status = sqlCollect.is_used(task_mark, video_id, mark, "抖音")
  36. # if status:
  37. status = sqlCollect.is_used(task_mark, video_id, mark, "抖音历史")
  38. video_uri = data[i].get('video', {}).get('play_addr', {}).get('uri')
  39. ratio = f'{data[i].get("video", {}).get("height")}p'
  40. # video_url = f'https://www.iesdouyin.com/aweme/v1/play/?video_id={video_uri}&ratio={ratio}&line=0'
  41. video_url = data[i].get('video', {}).get('play_addr', {}).get('url_list', [None])[0]
  42. # 视频链接
  43. digg_count = int(data[i].get('statistics').get('digg_count')) # 点赞
  44. share_count = int(data[i].get('statistics').get('share_count')) # 转发
  45. duration = data[i].get('duration')
  46. duration = duration / 1000
  47. old_title = data[i].get('desc', "").strip().replace("\n", "") \
  48. .replace("/", "").replace("\\", "").replace("\r", "") \
  49. .replace(":", "").replace("*", "").replace("?", "") \
  50. .replace("?", "").replace('"', "").replace("<", "") \
  51. .replace(">", "").replace("|", "").replace(" ", "") \
  52. .replace("&NBSP", "").replace(".", "。").replace(" ", "") \
  53. .replace("'", "").replace("#", "").replace("Merge", "")
  54. log_data = f"user:{url_id},,video_id:{video_id},,video_url:{video_url},,original_title:{old_title},,share_count:{share_count},,digg_count:{digg_count},,duration:{duration}"
  55. AliyunLogger.logging(channel_id, name, url_id, video_id, "扫描到一条视频", "2001", log_data)
  56. if status:
  57. AliyunLogger.logging(channel_id, name, url_id, video_id, "该视频已改造过", "2002", log_data)
  58. continue
  59. video_percent = '%.2f' % (int(share_count) / int(digg_count))
  60. special = float(0.25)
  61. if int(share_count) < 500:
  62. AliyunLogger.logging(channel_id, name, url_id, video_id, "不符合规则:分享小于500", "2003", log_data)
  63. continue
  64. if float(video_percent) < special:
  65. AliyunLogger.logging(channel_id, name, url_id, video_id, "不符合规则:分享/点赞小于0.25", "2003", log_data)
  66. continue
  67. if int(duration) < 30 or int(duration) > 720:
  68. AliyunLogger.logging(channel_id, name, url_id, video_id, "不符合规则:时长不符合规则大于720秒/小于30秒", "2003", log_data)
  69. continue
  70. cover_url = data[i].get('video').get('cover').get('url_list')[0] # 视频封面
  71. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url, "rule": video_percent,
  72. "old_title": old_title}
  73. list.append(all_data)
  74. AliyunLogger.logging(channel_id, name, url_id, video_id, "符合规则等待改造", "2004", log_data)
  75. if len(list) == int(number):
  76. return list
  77. if next_cursor == False:
  78. return list
  79. except Exception as exc:
  80. return list
  81. return list
  82. return list
  83. @classmethod
  84. def get_dyls_list(cls, task_mark, url_id, number, mark):
  85. next_cursor = ""
  86. for i in range(10):
  87. list = []
  88. try:
  89. # 抖查查
  90. url = "http://8.217.192.46:8889/crawler/dou_yin/blogger"
  91. payload = json.dumps({
  92. "account_id": url_id,
  93. "source": "抖查查",
  94. "cursor": next_cursor
  95. })
  96. headers = {
  97. 'Content-Type': 'application/json'
  98. }
  99. time.sleep(random.randint(1, 5))
  100. response = requests.request("POST", url, headers=headers, data=payload)
  101. response = response.json()
  102. data_all_list = response["data"]
  103. has_more = data_all_list["has_more"]
  104. next_cursor = str(data_all_list["next_cursor"])
  105. data_list = data_all_list["data"]
  106. for data in data_list:
  107. # comment_count = data["comment_count"]
  108. # download_count = data["download_count"]
  109. share_count = data["share_count"]
  110. good_count = data["good_count"]
  111. # collect_count = data["collect_count"]
  112. duration = data["duration"]
  113. video_id = data["video_id"]
  114. old_title = data["video_desc"]
  115. status = sqlCollect.is_used(video_id, mark, "抖音")
  116. if status:
  117. status = sqlCollect.is_used(video_id, mark, "抖音历史")
  118. if status == False:
  119. continue
  120. video_percent = '%.2f' % (int(share_count) / int(good_count))
  121. special = float(0.25)
  122. duration = duration / 1000
  123. if int(share_count) < 500 or float(video_percent) < special or int(duration) < 30 or int(duration) > 720:
  124. continue
  125. video_url, image_url = cls.get_video(video_id)
  126. if video_url:
  127. all_data = {"video_id": video_id, "cover": image_url, "video_url": video_url, "rule": video_percent,
  128. "old_title": old_title}
  129. list.append(all_data)
  130. if len(list) == int(number):
  131. return list
  132. else:
  133. Feishu.finish_bot("dou_yin/detail接口无法获取到视频链接",
  134. "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb",
  135. "【抖音异常提示 】")
  136. if has_more == False:
  137. return list
  138. except Exception as exc:
  139. return list
  140. @classmethod
  141. def get_video(cls, video_id):
  142. url = "http://8.217.192.46:8889/crawler/dou_yin/detail"
  143. for i in range(3):
  144. payload = json.dumps({
  145. "content_id": str(video_id)
  146. })
  147. headers = {
  148. 'Content-Type': 'application/json'
  149. }
  150. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  151. response = response.json()
  152. code = response["code"]
  153. if code == 10000:
  154. time.sleep(60)
  155. data = response["data"]["data"]
  156. video_url = data["video_url_list"][0]["video_url"]
  157. image_url = data["image_url_list"][0]["image_url"]
  158. return video_url, image_url
  159. return None, None
  160. if __name__ == '__main__':
  161. DYLS.get_dy_zr_list(1,2,1,3)
  162. # DYLS.get_dyls_list("1","MS4wLjABAAAA2QEvnEb7cQDAg6vZXq3j8_LlbO_DiturnV7VeybFKY4",1,"1")