import requests import json from common import Common, AliyunLogger from common.sql_help import sqlCollect class DyKeyword: @classmethod def get_key_word(cls, keyword, task_mark, mark, channel_id, name, task): combo = task['combo'] content_type = combo[0] publish_time = combo[1] duration = combo[2] share_count_rule = 0 special = 0 short_duration_rule = 0 url = "http://47.236.68.175:8889/crawler/dou_yin/keyword" list = [] payload = json.dumps({ "keyword": keyword, "content_type": "视频", "sort_type": content_type, "publish_time": publish_time, "duration": duration, "cursor": "" }) headers = { 'Content-Type': 'application/json' } if " 不限" == publish_time: share_count_rule = 200 special = 0.15 short_duration_rule = 30 elif "一天内" == publish_time: share_count_rule = 0 special = 0.10 short_duration_rule = 25 elif "一周内" == publish_time: share_count_rule = 100 special = 0.15 short_duration_rule = 25 elif "半年内" == publish_time: share_count_rule = 200 special = 0.15 short_duration_rule = 25 try: response = requests.request("POST", url, headers=headers, data=payload) response = response.json() code = response['code'] if code != 0: Common.logger("dy-key-word").info(f"抖音搜索词数据获取失败,接口为/dou_yin/keyword\n") return list data = response['data']['data'] for i in range(len(data)): video_id = data[i].get('aweme_id') # 文章id status = sqlCollect.is_used(task_mark, video_id, mark, channel_id) 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' # 视频链接 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", "") digg_count = int(data[i].get('statistics').get('digg_count')) # 点赞 share_count = int(data[i].get('statistics').get('share_count')) # 转发 duration = data[i].get('duration') duration = duration / 1000 log_data = f"user:{keyword},,video_id:{video_id},,video_url:{video_url},,original_title:{old_title},,share_count:{share_count},,digg_count:{digg_count},,duration:{duration}" AliyunLogger.logging(channel_id, name, keyword, video_id, "扫描到一条视频", "2001", log_data) Common.logger("dy-key-word").info( f"扫描:{task_mark},搜索词:{keyword},视频id{video_id} ,分享:{share_count},点赞{digg_count}") if status: AliyunLogger.logging(channel_id, name, keyword, video_id, "该视频已改造过", "2001", log_data) continue video_percent = '%.2f' % (int(share_count) / int(digg_count)) if int(share_count) < share_count_rule: AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:分享小于{share_count_rule}", "2003", log_data) Common.logger("dy-key-word").info( f"不符合规则:{task_mark},用户主页id:{keyword},视频id{video_id} ,分享:{share_count},点赞{digg_count} ,时长:{int(duration)} ") continue if float(video_percent) < special: AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:分享/点赞小于{special}", "2003", log_data) Common.logger("dy-key-word").info( f"不符合规则:{task_mark},用户主页id:{keyword},视频id{video_id} ,分享:{share_count},点赞{digg_count} ,时长:{int(duration)} ") continue if int(duration) < short_duration_rule or int(duration) > 720: AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:时长不符合规则大于720秒/小于{short_duration_rule}秒", "2003", log_data) Common.logger("dy-key-word").info( f"不符合规则:{task_mark},用户主页id:{keyword},视频id{video_id} ,分享:{share_count},点赞{digg_count} ,时长:{int(duration)} ") continue AliyunLogger.logging(channel_id, name, keyword, video_id, "符合规则等待改造", "2004", log_data) 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) return list except Exception as exc: Common.logger("dy-key-word").info(f"抖音搜索词{keyword}获取失败{exc}\n") return list if __name__ == '__main__': DyKeyword.get_key_word('keyword', 'sort_type', 'publish_time', 'duration', 'task_mark', 'mark')