import json import random import time import requests import urllib3 from requests.adapters import HTTPAdapter from common import Material, Common, Feishu from common.sql_help import sqlCollect from data_channel.data_help import dataHelp from data_channel.douyin_help import DouYinHelper class DY: @classmethod def get_dy_url(cls, task_mark, url_id, number, mark, feishu_id, cookie_sheet, channel_id, name): list = [] next_cursor = 0 for i in range(3): cookie = Material.get_cookie_data(feishu_id, cookie_sheet, channel_id) time.sleep(random.randint(1, 5)) url = 'https://www.douyin.com/aweme/v1/web/aweme/post/' headers = { 'Accept': 'application/json, text/plain, */*', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Cache-Control': 'no-cache', 'Cookie': cookie, 'Pragma': 'no-cache', 'Referer': f'https://www.douyin.com/user/{url_id}', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/118.0.0.0 Safari/537.36', } query = DouYinHelper.get_full_query(ua=headers['User-Agent'], extra_data={ 'sec_user_id': url_id, 'max_cursor': next_cursor, 'locate_query': 'false', 'show_live_replay_strategy': '1', 'need_time_list': '1', 'time_list_query': '0', 'whale_cut_token': '', 'cut_version': '1', 'count': '18', 'publish_video_strategy_type': '2', }) 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) body = response.content.decode() obj = json.loads(body) has_more = True if obj.get('has_more', 0) == 1 else False next_cursor = str(obj.get('max_cursor')) if has_more else None data = obj.get('aweme_list', []) if data == [] and len(data) == 0: Feishu.bot(mark, '机器自动改造消息通知', f'抖音cookie过期,请及时更换', name) return list response.close() for i in range(len(data)): entity_type = data[i].get('media_type') if entity_type == 4: # is_top = data[i].get('is_top') # 是否置顶 video_id = data[i].get('aweme_id') # 文章id status = sqlCollect.is_used(video_id, mark, channel_id) if status: video_uri = data[i].get('video', {}).get('play_addr', {}).get('uri') ratio = f'{data[i].get("video", {}).get("height")}p' video_url = f'https://www.iesdouyin.com/aweme/v1/play/?video_id={video_uri}&ratio={ratio}&line=0' # 视频链接 digg_count = int(data[i].get('statistics').get('digg_count')) # 点赞 # comment_count = int(data[i].get('statistics').get('comment_count')) # 评论 share_count = int(data[i].get('statistics').get('share_count')) # 转发 old_title = data[i].get('desc', "").strip().replace("\n", "") \ .replace("/", "").replace("\\", "").replace("\r", "") \ .replace(":", "").replace("*", "").replace("?", "") \ .replace("?", "").replace('"', "").replace("<", "") \ .replace(">", "").replace("|", "").replace(" ", "") \ .replace("&NBSP", "").replace(".", "。").replace(" ", "") \ .replace("'", "").replace("#", "").replace("Merge", "") Common.logger("dy").info( f"扫描:{task_mark},用户主页id:{url_id},视频id{video_id} ,分享:{share_count},点赞{digg_count}") if share_count < 500: Common.logger("dy").info( f"不符合规则:{task_mark},用户主页id:{url_id},视频id{video_id} ,分享:{share_count},点赞{digg_count}") continue video_percent = '%.2f' % (share_count / digg_count) special = float(0.25) if float(video_percent) < special: Common.logger("dy").info( f"不符合规则:{task_mark},用户主页id:{url_id},视频id{video_id} ,分享:{share_count},点赞{digg_count} ") continue duration = dataHelp.video_duration(video_url) if int(duration) < 30 or int(duration) > 720: Common.logger("dy").info( f"不符合规则:{task_mark},用户主页id:{url_id},视频id{video_id} ,分享:{share_count},点赞{digg_count} ,时长:{duration} ") continue cover_url = data[i].get('video').get('cover').get('url_list')[0] # 视频封面 all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url, "rule": video_percent, "old_title": old_title} list.append(all_data) if len(list) == int(number): Common.logger(mark).info(f"获取抖音视频总数:{len(list)}\n") return list return list