12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- # -*- coding: utf-8 -*-
- # @Time: 2023/12/22
- import datetime
- import os
- import random
- import re
- import sys
- import requests
- import json
- import urllib3
- sys.path.append(os.getcwd())
- from requests.adapters import HTTPAdapter
- from single_video.douyin.douyin_help import DouYinHelper
- from common.url_manage import urlManage
- from common import Oss, Common
- from common.pq_utility import PQ
- from common.userAgent import get_random_user_agent
- class douyinVideo():
- # 获取抖音 标题+视频链接
- @classmethod
- def get_videoList(cls, vx_message, channel):
- data_link = vx_message[1]
- content_id = ''
- for i in range(3):
- content_id = urlManage.url_manage(data_link, channel)
- if content_id:
- break
- if content_id == '':
- return "无法获取到视频ID"
- Common.logger().info(f'渠道:{channel},视频ID:{content_id}')
- for i in range(3):
- try:
- url = f'https://www.douyin.com/aweme/v1/web/aweme/detail/'
- headers = {
- 'Accept': 'application/json, text/plain, */*',
- 'Accept-Language': 'zh-CN,zh;q=0.9',
- 'Cache-Control': 'no-cache',
- 'Cookie' : DouYinHelper.get_cookie(),
- 'Pragma': 'no-cache',
- 'Referer': f'https://www.douyin.com/video/{content_id}',
- 'User-Agent': get_random_user_agent()
- }
- query = DouYinHelper.get_full_query(ua=headers['User-Agent'], extra_data={'aweme_id': content_id})
- urllib3.disable_warnings()
- s = requests.session()
- s.mount('http://', HTTPAdapter(max_retries=3))
- s.mount('https://', HTTPAdapter(max_retries=3))
- response = requests.request(method='GET', url=url, headers=headers, params=query)
- status_code = response.status_code
- if status_code != 200:
- return "cookie过期"
- body = response.content.decode()
- obj = json.loads(body).get('aweme_detail', {})
- if len(obj) == 0:
- return "cookie过期"
- if obj.get('desc'):
- title = obj.get('desc').split('\n')[0].strip()
- title = f'{title[:20]}...' if len(title) > 20 else title
- title = title if title else ' '
- else:
- title = ' '
- if obj.get('video', {}).get('bit_rate', []):
- video_uri = obj.get('video', {}).get('play_addr', {}).get('uri')
- ratio = f'{obj.get("video", {}).get("height")}p'
- video_url = f'https://www.iesdouyin.com/aweme/v1/play/?video_id={video_uri}&ratio={ratio}&line=0'
- # 随机生成视频oss_id
- video_id = urlManage.random_id()
- oss_object_key = Oss.video_url_upload_oss(video_url, video_id)
- oss_object_key = oss_object_key.get("oss_object_key")
- Common.logger().info(f'准备发送站内参数:{oss_object_key},{title},{vx_message[3]}')
- piaoquantv = PQ.insert_piaoquantv(oss_object_key, title, vx_message[3])
- if piaoquantv == False:
- return '视频发送到站内失败'
- else:
- return "无法获取到视频链接"
- except Exception as e:
- Common.logger().info(f'报错信息:{e}')
- if __name__ == '__main__':
- douyinVideo.get_videoList()
|