douyin.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import json
  2. import random
  3. import time
  4. import requests
  5. import urllib3
  6. from requests.adapters import HTTPAdapter
  7. from common import Material, Common, Feishu, AliyunLogger
  8. from common.sql_help import sqlCollect
  9. from data_channel.data_help import dataHelp
  10. from data_channel.douyin_help import DouYinHelper
  11. class DY:
  12. @classmethod
  13. def get_dy_url(cls, task_mark, url_id, number, mark, feishu_id, cookie_sheet, channel_id, name):
  14. list = []
  15. next_cursor = 0
  16. try:
  17. for i in range(6):
  18. cookie = Material.get_cookie_data(feishu_id, cookie_sheet, channel_id)
  19. time.sleep(random.randint(1, 5))
  20. url = 'https://www.douyin.com/aweme/v1/web/aweme/post/'
  21. headers = {
  22. 'Accept': 'application/json, text/plain, */*',
  23. 'Accept-Language': 'zh-CN,zh;q=0.9',
  24. 'Cache-Control': 'no-cache',
  25. 'Cookie': cookie,
  26. 'Pragma': 'no-cache',
  27. 'Referer': f'https://www.douyin.com/user/{url_id}',
  28. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
  29. 'Chrome/118.0.0.0 Safari/537.36',
  30. }
  31. query = DouYinHelper.get_full_query(ua=headers['User-Agent'], extra_data={
  32. 'sec_user_id': url_id,
  33. 'max_cursor': next_cursor,
  34. 'locate_query': 'false',
  35. 'show_live_replay_strategy': '1',
  36. 'need_time_list': '1',
  37. 'time_list_query': '0',
  38. 'whale_cut_token': '',
  39. 'cut_version': '1',
  40. 'count': '18',
  41. 'publish_video_strategy_type': '2',
  42. })
  43. urllib3.disable_warnings()
  44. s = requests.session()
  45. s.mount('http://', HTTPAdapter(max_retries=3))
  46. s.mount('https://', HTTPAdapter(max_retries=3))
  47. response = requests.request(method='GET', url=url, headers=headers, params=query)
  48. body = response.content.decode()
  49. obj = json.loads(body)
  50. has_more = True if obj.get('has_more', 0) == 1 else False
  51. next_cursor = str(obj.get('max_cursor')) if has_more else None
  52. data = obj.get('aweme_list', [])
  53. if data == [] and len(data) == 0:
  54. if name == '抖音品类账号' or name == '抖音品类账号-1':
  55. Feishu.bot("wangxueke", '机器自动改造消息通知', f'抖音-{name}cookie过期,请及时更换', '王雪珂')
  56. Feishu.bot("xinxin", '机器自动改造消息通知', f'抖音-{name}cookie过期,请及时更换', '信欣')
  57. Feishu.bot("liuzhaoheng", '机器自动改造消息通知', f'抖音-{name}cookie过期,请及时更换', '刘兆恒')
  58. else:
  59. Feishu.bot(mark, '机器自动改造消息通知', f'抖音-{name}cookie过期,请及时更换', name)
  60. return list
  61. response.close()
  62. for i in range(len(data)):
  63. entity_type = data[i].get('media_type')
  64. if entity_type == 4:
  65. # is_top = data[i].get('is_top') # 是否置顶
  66. video_id = data[i].get('aweme_id') # 文章id
  67. status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
  68. video_uri = data[i].get('video', {}).get('play_addr', {}).get('uri')
  69. ratio = f'{data[i].get("video", {}).get("height")}p'
  70. video_url = f'https://www.iesdouyin.com/aweme/v1/play/?video_id={video_uri}&ratio={ratio}&line=0' # 视频链接
  71. digg_count = int(data[i].get('statistics').get('digg_count')) # 点赞
  72. # comment_count = int(data[i].get('statistics').get('comment_count')) # 评论
  73. share_count = int(data[i].get('statistics').get('share_count')) # 转发
  74. old_title = data[i].get('desc', "").strip().replace("\n", "") \
  75. .replace("/", "").replace("\\", "").replace("\r", "") \
  76. .replace(":", "").replace("*", "").replace("?", "") \
  77. .replace("?", "").replace('"', "").replace("<", "") \
  78. .replace(">", "").replace("|", "").replace(" ", "") \
  79. .replace("&NBSP", "").replace(".", "。").replace(" ", "") \
  80. .replace("'", "").replace("#", "").replace("Merge", "")
  81. duration = dataHelp.video_duration(video_url)
  82. Common.logger("dy").info(
  83. f"扫描:{task_mark},用户主页id:{url_id},视频id{video_id} ,分享:{share_count},点赞{digg_count}")
  84. 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}"
  85. AliyunLogger.logging(channel_id, name, url_id, video_id, "扫描到一条视频", "2001", log_data)
  86. if status:
  87. AliyunLogger.logging(channel_id, name, url_id, video_id, "该视频已改造过", "2002", log_data)
  88. continue
  89. if share_count < 200:
  90. AliyunLogger.logging(channel_id, name, url_id, video_id, "不符合规则:分享小于200", "2003", log_data)
  91. Common.logger("dy").info(
  92. f"不符合规则:{task_mark},用户主页id:{url_id},视频id{video_id} ,分享:{share_count},点赞{digg_count}")
  93. continue
  94. video_percent = '%.2f' % (share_count / digg_count)
  95. special = float(0.15)
  96. if float(video_percent) < special:
  97. AliyunLogger.logging(channel_id, name, url_id, video_id, "不符合规则:分享/点赞小于0.15", "2003", log_data)
  98. Common.logger("dy").info(
  99. f"不符合规则:{task_mark},用户主页id:{url_id},视频id{video_id} ,分享:{share_count},点赞{digg_count} ")
  100. continue
  101. if int(duration) < 30 or int(duration) > 720:
  102. AliyunLogger.logging(channel_id, name, url_id, video_id, "不符合规则:时长不符合规则大于720秒/小于30秒", "2003", log_data)
  103. Common.logger("dy").info(
  104. f"不符合规则:{task_mark},用户主页id:{url_id},视频id{video_id} ,分享:{share_count},点赞{digg_count} ,时长:{duration} ")
  105. continue
  106. cover_url = data[i].get('video').get('cover').get('url_list')[0] # 视频封面
  107. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url,
  108. "rule": video_percent, "old_title": old_title}
  109. list.append(all_data)
  110. AliyunLogger.logging(channel_id, name, url_id, video_id, "符合规则等待改造", "2004", log_data)
  111. if len(list) == int(number):
  112. Common.logger(mark).info(f"获取抖音视频总数:{len(list)}\n")
  113. return list
  114. return list
  115. except Exception as exc:
  116. # Feishu.bot("liuzhaoheng", '机器自动改造消息通知', f'抖音-{name}cookie过期,请及时更换', '刘兆恒')
  117. # Feishu.bot("wangxueke", '机器自动改造消息通知', f'抖音-{name}cookie过期,请及时更换', '王雪珂')
  118. # Feishu.bot("xinxin", '机器自动改造消息通知', f'抖音-{name}cookie过期,请及时更换', '信欣')
  119. Common.logger("dy").info(f"抖音历史数据获取失败:{exc}\n")
  120. return list