douyin.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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
  8. from common.sql_help import sqlCollect
  9. from data_channel.douyin_help import DouYinHelper
  10. class DY:
  11. @classmethod
  12. def get_dy_url(cls, task_mark, url_id, number, mark, feishu_id, cookie_sheet, channel_id, name):
  13. list = []
  14. next_cursor = 0
  15. for i in range(3):
  16. cookie = Material.get_cookie_data(feishu_id, cookie_sheet, channel_id)
  17. time.sleep(random.randint(5, 10))
  18. url = 'https://www.douyin.com/aweme/v1/web/aweme/post/'
  19. headers = {
  20. 'Accept': 'application/json, text/plain, */*',
  21. 'Accept-Language': 'zh-CN,zh;q=0.9',
  22. 'Cache-Control': 'no-cache',
  23. 'Cookie': cookie,
  24. 'Pragma': 'no-cache',
  25. 'Referer': f'https://www.douyin.com/user/{url_id}',
  26. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
  27. 'Chrome/118.0.0.0 Safari/537.36',
  28. }
  29. query = DouYinHelper.get_full_query(ua=headers['User-Agent'], extra_data={
  30. 'sec_user_id': url_id,
  31. 'max_cursor': next_cursor,
  32. 'locate_query': 'false',
  33. 'show_live_replay_strategy': '1',
  34. 'need_time_list': '1',
  35. 'time_list_query': '0',
  36. 'whale_cut_token': '',
  37. 'cut_version': '1',
  38. 'count': '18',
  39. 'publish_video_strategy_type': '2',
  40. })
  41. urllib3.disable_warnings()
  42. s = requests.session()
  43. s.mount('http://', HTTPAdapter(max_retries=3))
  44. s.mount('https://', HTTPAdapter(max_retries=3))
  45. response = requests.request(method='GET', url=url, headers=headers, params=query)
  46. body = response.content.decode()
  47. obj = json.loads(body)
  48. has_more = True if obj.get('has_more', 0) == 1 else False
  49. next_cursor = str(obj.get('max_cursor')) if has_more else None
  50. data = obj.get('aweme_list', [])
  51. if data == [] and len(data) == 0:
  52. return list
  53. if not data[0].get('search_impr').get('entity_type'):
  54. Feishu.bot(mark, '机器自动改造消息通知', f'今日任务为空,请关注', name)
  55. time.sleep(900)
  56. continue
  57. response.close()
  58. for i in range(len(data)):
  59. entity_type = data[i].get('search_impr').get('entity_type')
  60. if entity_type == 'GENERAL':
  61. # is_top = data[i].get('is_top') # 是否置顶
  62. video_id = data[i].get('aweme_id') # 文章id
  63. status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
  64. if status:
  65. video_uri = data[i].get('video', {}).get('play_addr', {}).get('uri')
  66. ratio = f'{data[i].get("video", {}).get("height")}p'
  67. video_url = f'https://www.iesdouyin.com/aweme/v1/play/?video_id={video_uri}&ratio={ratio}&line=0' # 视频链接
  68. cover_url = data[i].get('video').get('cover').get('url_list')[0] # 视频封面
  69. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url}
  70. list.append(all_data)
  71. if len(list) == int(number):
  72. Common.logger("log").info(f"获取抖音视频总数:{len(list)}\n")
  73. return list
  74. return list