douyin_video.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. # @Time: 2023/12/22
  3. import datetime
  4. import os
  5. import random
  6. import re
  7. import sys
  8. import requests
  9. import json
  10. import urllib3
  11. sys.path.append(os.getcwd())
  12. from requests.adapters import HTTPAdapter
  13. from single_video.douyin.douyin_help import DouYinHelper
  14. from common.url_manage import urlManage
  15. from common import Oss, Common
  16. from common.pq_utility import PQ
  17. from common.userAgent import get_random_user_agent
  18. class douyinVideo():
  19. # 获取抖音 标题+视频链接
  20. @classmethod
  21. def get_videoList(cls, vx_message, channel):
  22. data_link = vx_message[1]
  23. content_id = ''
  24. for i in range(3):
  25. content_id = urlManage.url_manage(data_link, channel)
  26. if content_id:
  27. break
  28. if content_id == '':
  29. return "无法获取到视频ID"
  30. Common.logger().info(f'渠道:{channel},视频ID:{content_id}')
  31. for i in range(3):
  32. try:
  33. url = f'https://www.douyin.com/aweme/v1/web/aweme/detail/'
  34. headers = {
  35. 'Accept': 'application/json, text/plain, */*',
  36. 'Accept-Language': 'zh-CN,zh;q=0.9',
  37. 'Cache-Control': 'no-cache',
  38. 'Cookie' : DouYinHelper.get_cookie(),
  39. 'Pragma': 'no-cache',
  40. 'Referer': f'https://www.douyin.com/video/{content_id}',
  41. 'User-Agent': get_random_user_agent()
  42. }
  43. query = DouYinHelper.get_full_query(ua=headers['User-Agent'], extra_data={'aweme_id': content_id})
  44. urllib3.disable_warnings()
  45. s = requests.session()
  46. s.mount('http://', HTTPAdapter(max_retries=3))
  47. s.mount('https://', HTTPAdapter(max_retries=3))
  48. response = requests.request(method='GET', url=url, headers=headers, params=query)
  49. status_code = response.status_code
  50. if status_code != 200:
  51. return "cookie过期"
  52. body = response.content.decode()
  53. obj = json.loads(body).get('aweme_detail', {})
  54. if len(obj) == 0:
  55. return "cookie过期"
  56. if obj.get('desc'):
  57. title = obj.get('desc').split('\n')[0].strip()
  58. title = f'{title[:20]}...' if len(title) > 20 else title
  59. title = title if title else ' '
  60. else:
  61. title = ' '
  62. if obj.get('video', {}).get('bit_rate', []):
  63. video_uri = obj.get('video', {}).get('play_addr', {}).get('uri')
  64. ratio = f'{obj.get("video", {}).get("height")}p'
  65. video_url = f'https://www.iesdouyin.com/aweme/v1/play/?video_id={video_uri}&ratio={ratio}&line=0'
  66. # 随机生成视频oss_id
  67. video_id = urlManage.random_id()
  68. oss_object_key = Oss.video_url_upload_oss(video_url, video_id)
  69. oss_object_key = oss_object_key.get("oss_object_key")
  70. Common.logger().info(f'准备发送站内参数:{oss_object_key},{title},{vx_message[3]}')
  71. piaoquantv = PQ.insert_piaoquantv(oss_object_key, title, vx_message[3])
  72. if piaoquantv == False:
  73. return '视频发送到站内失败'
  74. else:
  75. return "无法获取到视频链接"
  76. except Exception as e:
  77. Common.logger().info(f'报错信息:{e}')
  78. if __name__ == '__main__':
  79. douyinVideo.get_videoList()