# -*- coding: utf-8 -*- # @Author: wangkun # @Time: 2023/3/13 import json import os import random import shutil import sys import time import requests import urllib3 sys.path.append(os.getcwd()) from common.common import Common from common.scheduling_db import MysqlHelper from common.publish import Publish from common.feishu import Feishu from common.public import filter_word proxies = {"http": None, "https": None} class XiaoniangaoFollow: platform = "小年糕" # 小程序个人主页视频列表翻页参数 next_t = None # 基础门槛规则 @staticmethod def download_rule(video_dict): """ 下载视频的基本规则 :param video_dict: 视频信息,字典格式 :return: 满足规则,返回 True;反之,返回 False """ # 视频时长 if int(float(video_dict['duration'])) >= 40: # 宽或高 if int(video_dict['video_width']) >= 0 or int(video_dict['video_height']) >= 0: # 播放量 if int(video_dict['play_cnt']) >= 500: # 分享量 if int(video_dict['share_cnt']) >= 0: return True else: return False else: return False else: return False else: return False # 从云文档获取关注用户列表 @classmethod def get_users(cls): # try: # while True: # follow_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "oNpThi") # if follow_sheet is None: # time.sleep(1) # continue # if len(follow_sheet) == 1: # Common.logger(log_type, crawler).info("暂无定向爬取账号") # else: # user_list = [] # for i in range(1, len(follow_sheet)): # profile_id = follow_sheet[i][0] # profile_mid = follow_sheet[i][1] # user_name = follow_sheet[i][2] # user_dict = { # "profile_id": str(profile_id), # "profile_mid": str(profile_mid), # "user_name": user_name, # } # user_list.append(user_dict) # return user_list # except Exception as e: # Common.logger(log_type, crawler).error("从云文档获取关注用户列表异常:{}", e) user_list = [{'profile_id': '43549964', 'profile_mid': '116311065', 'user_name': '青峰明月老人'}, {'profile_id': '43486474', 'profile_mid': '1113556014', 'user_name': '好人一生平安'}, {'profile_id': '43590330', 'profile_mid': '192501730', 'user_name': '风轻云淡'}, {'profile_id': '43559741', 'profile_mid': '90261331', 'user_name': '歌一曲'}, {'profile_id': '43785654', 'profile_mid': '104703232', 'user_name': '涧欣'}, {'profile_id': '44211463', 'profile_mid': '1149063512', 'user_name': '美食分享'}, {'profile_id': '46661040', 'profile_mid': '293457861', 'user_name': '阿妞'}, {'profile_id': '46731019', 'profile_mid': '1059483000', 'user_name': '早睡早起锻炼身体'}, {'profile_id': '46644244', 'profile_mid': '609255292', 'user_name': '雨太漫长'}, {'profile_id': '46553912', 'profile_mid': '1160417287', 'user_name': '葛锡琴'}, {'profile_id': '46802824', 'profile_mid': '1157645694', 'user_name': '留白'}, {'profile_id': '46795509', 'profile_mid': '1161593368', 'user_name': '绒花漫舞'}, {'profile_id': '46837469', 'profile_mid': '1160417275', 'user_name': '苏超灵'}, {'profile_id': '46830553', 'profile_mid': '1160417336', 'user_name': '山里红'}, {'profile_id': '50541284', 'profile_mid': '1210725734', 'user_name': '语琴'}, {'profile_id': '50541428', 'profile_mid': '1222976096', 'user_name': '血性娘子'}, {'profile_id': '50364411', 'profile_mid': '1192924729', 'user_name': '好运莲莲'}, {'profile_id': '50499182', 'profile_mid': '329676836', 'user_name': '花好月圆'}, {'profile_id': '50449938', 'profile_mid': '341183833', 'user_name': '花开富贵'}, {'profile_id': '50199770', 'profile_mid': '1166029432', 'user_name': '兰花草1'}, {'profile_id': '50299362', 'profile_mid': '1013092427', 'user_name': '感悟人生'}, {'profile_id': '50539176', 'profile_mid': '1203033756', 'user_name': '风之别鹤'}, {'profile_id': '50541092', 'profile_mid': '1237693278', 'user_name': '莲心'}, {'profile_id': '50523015', 'profile_mid': '260159327', 'user_name': '微笑😊123'}, {'profile_id': '50216970', 'profile_mid': '1219153129', 'user_name': '秋夕'}, {'profile_id': '50286181', 'profile_mid': '1113733998', 'user_name': '上善若水'}, {'profile_id': '50505135', 'profile_mid': '185546', 'user_name': '秋水揽星河'}, {'profile_id': '50555007', 'profile_mid': '1113739184', 'user_name': '淡墨青衫'}, {'profile_id': '50500343', 'profile_mid': '1072539904', 'user_name': '微笑向明天'}, {'profile_id': '50628735', 'profile_mid': '1142824266', 'user_name': '清爽'}, {'profile_id': '50580367', 'profile_mid': '26055443', 'user_name': '玉儿Syy'}, {'profile_id': '50585024', 'profile_mid': '1105334217', 'user_name': '老树开花🌸'}, {'profile_id': '50380683', 'profile_mid': '63682047', 'user_name': '海王岛浩哥'}, {'profile_id': '46386724', 'profile_mid': '271535337', 'user_name': '雪梅'}, {'profile_id': '50636398', 'profile_mid': '237071458', 'user_name': '看花人'}, {'profile_id': '50624991', 'profile_mid': '1202244309', 'user_name': '无关风月'}, {'profile_id': '50561528', 'profile_mid': '1134790664', 'user_name': '老有所乐'}, {'profile_id': '50612235', 'profile_mid': '1162974507', 'user_name': '花素千昔'}, {'profile_id': '50933310', 'profile_mid': '1237661365', 'user_name': '淡淡浅香'}, {'profile_id': '50492961', 'profile_mid': '1160417339', 'user_name': '胡淑华'}, {'profile_id': '49965729', 'profile_mid': '1160417117', 'user_name': '百姓生活'}, {'profile_id': '50928693', 'profile_mid': '1163097085', 'user_name': '多福'}, {'profile_id': '50819153', 'profile_mid': '224365299', 'user_name': '欢天喜地'}, {'profile_id': '51510752', 'profile_mid': '1160417314', 'user_name': '郝建兵'}, {'profile_id': '51470002', 'profile_mid': '631317964', 'user_name': '九牛二虎'}, {'profile_id': '51466430', 'profile_mid': '177806025', 'user_name': '越来越好'}, {'profile_id': '51480157', 'profile_mid': '1000526700', 'user_name': '光伯伯'}, {'profile_id': '51396620', 'profile_mid': '1160417243', 'user_name': '亚萍'}, {'profile_id': '51538137', 'profile_mid': '154002316', 'user_name': 'hua jean-pierre 許遵訓'}, {'profile_id': '51533676', 'profile_mid': '197908577', 'user_name': '萍水相逢'}, {'profile_id': '51488254', 'profile_mid': '153685063', 'user_name': '英勇'}, {'profile_id': '51425929', 'profile_mid': '1161593426', 'user_name': '百年字号'}, {'profile_id': '51514370', 'profile_mid': '1198361106', 'user_name': '品味人生'}, {'profile_id': '51405010', 'profile_mid': '1161593390', 'user_name': '低调生活'}, {'profile_id': '51535995', 'profile_mid': '690938173', 'user_name': '世界风'}, {'profile_id': '51468900', 'profile_mid': '1242534599', 'user_name': '人生如茶'}, {'profile_id': '51489772', 'profile_mid': '269038284', 'user_name': '韵律'}, {'profile_id': '51377241', 'profile_mid': '194287386', 'user_name': '霹雳雷天发'}, {'profile_id': '51538527', 'profile_mid': '1064479786', 'user_name': '鑫'}, {'profile_id': '51365161', 'profile_mid': '1161593351', 'user_name': '黄金三十分'}, {'profile_id': '51527297', 'profile_mid': '1163011474', 'user_name': '指日东升'}, {'profile_id': '51356555', 'profile_mid': '76036942', 'user_name': '利剑'}, {'profile_id': '51382702', 'profile_mid': '1160417349', 'user_name': '丽丽'}, {'profile_id': '51536925', 'profile_mid': '1160417133', 'user_name': '福海'}, {'profile_id': '51525148', 'profile_mid': '1160417333', 'user_name': '君子兰'}, {'profile_id': '51530896', 'profile_mid': '1162996019', 'user_name': '你好,朋友'}, {'profile_id': '51532418', 'profile_mid': '1162998153', 'user_name': '旭日东升'}, {'profile_id': '51329950', 'profile_mid': '285062559', 'user_name': '阿静'}, {'profile_id': '51512535', 'profile_mid': '114981390', 'user_name': '泽南'}, {'profile_id': '51480066', 'profile_mid': '1160417334', 'user_name': '克平'}, {'profile_id': '51520908', 'profile_mid': '1161593408', 'user_name': '永生'}, {'profile_id': '51496944', 'profile_mid': '1160417145', 'user_name': '一生快乐'}, {'profile_id': '51511833', 'profile_mid': '1105154277', 'user_name': '老周.物流全国.'}, {'profile_id': '51515937', 'profile_mid': '1178673137', 'user_name': '风起风落'}, {'profile_id': '51539903', 'profile_mid': '1175023459', 'user_name': '花开福贵 &吉祥'}, {'profile_id': '51394812', 'profile_mid': '1160417173', 'user_name': '尔东'}, {'profile_id': '51512298', 'profile_mid': '22171304', 'user_name': '张杰真'}, {'profile_id': '51472642', 'profile_mid': '175855074', 'user_name': '张顺淑'}, {'profile_id': '51548416', 'profile_mid': '1116847111', 'user_name': '沁水百合'}, {'profile_id': '51456595', 'profile_mid': '1075862992', 'user_name': '永保青春'}, {'profile_id': '51502433', 'profile_mid': '1162980250', 'user_name': '午夜想念'}, {'profile_id': '51495781', 'profile_mid': '1162961480', 'user_name': '快乐每一天'}, {'profile_id': '51527180', 'profile_mid': '1161593427', 'user_name': '彩云追月'}, {'profile_id': '51537082', 'profile_mid': '1163005063', 'user_name': '峥嵘岁月'}, {'profile_id': '51510904', 'profile_mid': '64295407', 'user_name': '文文'}, {'profile_id': '51497047', 'profile_mid': '59349938', 'user_name': '老马识途'}, {'profile_id': '51544604', 'profile_mid': '1160417260', 'user_name': '星'}, {'profile_id': '51411909', 'profile_mid': '801020924', 'user_name': '相思鸟'}, {'profile_id': '51525860', 'profile_mid': '1160417222', 'user_name': '老来得福'}, {'profile_id': '51540902', 'profile_mid': '1163003217', 'user_name': '银发中年'}, {'profile_id': '51526760', 'profile_mid': '295640510', 'user_name': '阿中'}, {'profile_id': '51529247', 'profile_mid': '674650199', 'user_name': '张恩培'}, {'profile_id': '51540032', 'profile_mid': '18103420', 'user_name': '吴希孟'}, {'profile_id': '51491818', 'profile_mid': '1162991035', 'user_name': '幸福每一天'}, {'profile_id': '51539843', 'profile_mid': '210721352', 'user_name': '掉地人参果'}, {'profile_id': '51521237', 'profile_mid': '1160417237', 'user_name': '山野村夫'}, {'profile_id': '51531438', 'profile_mid': '103235841', 'user_name': '开心'}, {'profile_id': '51528239', 'profile_mid': '1160417288', 'user_name': '戴玉亭'}, {'profile_id': '51542817', 'profile_mid': '126121769', 'user_name': '芳芳'}, {'profile_id': '51541108', 'profile_mid': '287637208', 'user_name': '心无旁骛'}, {'profile_id': '51462517', 'profile_mid': '247008734', 'user_name': '华'}, {'profile_id': '51543209', 'profile_mid': '1214482340', 'user_name': '丽'}, {'profile_id': '51509548', 'profile_mid': '1162977406', 'user_name': '绚丽多彩'}, {'profile_id': '51536733', 'profile_mid': '1161593366', 'user_name': '夕阳红'}, {'profile_id': '51519407', 'profile_mid': '1122750580', 'user_name': '幸福满堂'}, {'profile_id': '51459413', 'profile_mid': '348958961', 'user_name': '段平清'}, {'profile_id': '51487820', 'profile_mid': '605290310', 'user_name': '青青'}, {'profile_id': '51469080', 'profile_mid': '131475097', 'user_name': '手宝'}, {'profile_id': '51426171', 'profile_mid': '196597807', 'user_name': '妙'}, {'profile_id': '51470293', 'profile_mid': '78386273', 'user_name': '芳草'}, {'profile_id': '51505305', 'profile_mid': '172977522', 'user_name': '伟星'}, {'profile_id': '51516061', 'profile_mid': '913804992', 'user_name': '荷塘月色'}, {'profile_id': '51541167', 'profile_mid': '1100776170', 'user_name': '闪电⚡️'}, {'profile_id': '51391450', 'profile_mid': '982518039', 'user_name': '吕梁'}, {'profile_id': '51529786', 'profile_mid': '168833501', 'user_name': ':shen. ser'}, {'profile_id': '51416422', 'profile_mid': '376557509', 'user_name': '于孝震'}, {'profile_id': '51342944', 'profile_mid': '556796156', 'user_name': '☀吉祥☀'}, {'profile_id': '51536144', 'profile_mid': '1162956349', 'user_name': '凤舞九天'}, {'profile_id': '51533164', 'profile_mid': '153787914', 'user_name': '红梅'}, {'profile_id': '51450974', 'profile_mid': '20396528', 'user_name': '学友'}, {'profile_id': '51512622', 'profile_mid': '35883962', 'user_name': '珎珎'}, {'profile_id': '51525737', 'profile_mid': '69546032', 'user_name': '欣然'}, {'profile_id': '51414093', 'profile_mid': '1154645646', 'user_name': '老中医'}, {'profile_id': '51372351', 'profile_mid': '154481426', 'user_name': '寻花'}, {'profile_id': '51511869', 'profile_mid': '1162958849', 'user_name': '保持微笑'}, {'profile_id': '51367397', 'profile_mid': '173682337', 'user_name': '莲'}, {'profile_id': '51494361', 'profile_mid': '1161593403', 'user_name': '红叶'}, {'profile_id': '51352260', 'profile_mid': '31040287', 'user_name': '52部队'}, {'profile_id': '51430672', 'profile_mid': '69886497', 'user_name': '张俊宽'}, {'profile_id': '51440076', 'profile_mid': '1186145367', 'user_name': '金光'}, {'profile_id': '51431733', 'profile_mid': '112830331', 'user_name': '幸福安康'}, {'profile_id': '51511957', 'profile_mid': '1214482340', 'user_name': '丽'}, {'profile_id': '51466969', 'profile_mid': '131962346', 'user_name': '丹桂'}, {'profile_id': '51515739', 'profile_mid': '1162950925', 'user_name': '烈火'}, {'profile_id': '51425141', 'profile_mid': '207104529', 'user_name': '雪梅'}, {'profile_id': '51465625', 'profile_mid': '913804992', 'user_name': '荷塘月色'}, {'profile_id': '51470424', 'profile_mid': '690938173', 'user_name': '世界风'}, {'profile_id': '51502065', 'profile_mid': '1105154277', 'user_name': '老周.物流全国.'}, {'profile_id': '51505082', 'profile_mid': '674650199', 'user_name': '张恩培'}, {'profile_id': '51508736', 'profile_mid': '30955754', 'user_name': '硕'}, {'profile_id': '51363432', 'profile_mid': '1160417336', 'user_name': '山里红'}, {'profile_id': '51472849', 'profile_mid': '1160417336', 'user_name': '山里红'}, {'profile_id': '51531002', 'profile_mid': '1149063512', 'user_name': '美食分享'}, {'profile_id': '51369043', 'profile_mid': '1162996019', 'user_name': '你好,朋友'}, {'profile_id': '51488704', 'profile_mid': '1160417201', 'user_name': '伴迪伴你'}, {'profile_id': '51514738', 'profile_mid': '1160417222', 'user_name': '老来得福'}, {'profile_id': '51526888', 'profile_mid': '1160417349', 'user_name': '丽丽'}, {'profile_id': '51513568', 'profile_mid': '1163005063', 'user_name': '峥嵘岁月'}, {'profile_id': '51514871', 'profile_mid': '1161593379', 'user_name': '心无界'}, {'profile_id': '51518737', 'profile_mid': '1160417333', 'user_name': '君子兰'}, {'profile_id': '51525297', 'profile_mid': '1160417115', 'user_name': '绚丽多彩'}, {'profile_id': '51383706', 'profile_mid': '1162953017', 'user_name': '荷塘月色'}, {'profile_id': '51528569', 'profile_mid': '1162977406', 'user_name': '绚丽多彩'}, {'profile_id': '51437102', 'profile_mid': '1162958849', 'user_name': '保持微笑'}, {'profile_id': '51536771', 'profile_mid': '1160417243', 'user_name': '亚萍'}, {'profile_id': '51516969', 'profile_mid': '1161593390', 'user_name': '低调生活'}, {'profile_id': '51535885', 'profile_mid': '1160417243', 'user_name': '亚萍'}, {'profile_id': '51517906', 'profile_mid': '1160417268', 'user_name': '宏蕴'}, {'profile_id': '51530809', 'profile_mid': '1163003217', 'user_name': '银发中年'}, {'profile_id': '51458395', 'profile_mid': '1160417288', 'user_name': '戴玉亭'}, {'profile_id': '51517169', 'profile_mid': '1163601308', 'user_name': '今日祝福'}, {'profile_id': '51515458', 'profile_mid': '1162953017', 'user_name': '荷塘月色'}, {'profile_id': '51498134', 'profile_mid': '1160417262', 'user_name': '快乐每一天'}, {'profile_id': '51436822', 'profile_mid': '1162996019', 'user_name': '你好,朋友'}, {'profile_id': '51466833', 'profile_mid': '1160417349', 'user_name': '丽丽'}, {'profile_id': '51514420', 'profile_mid': '1242534599', 'user_name': '人生如茶'}, {'profile_id': '51329643', 'profile_mid': '1160417133', 'user_name': '福海'}, {'profile_id': '51384888', 'profile_mid': '1160417349', 'user_name': '丽丽'}, {'profile_id': '51382334', 'profile_mid': '1177277385', 'user_name': '丝花'}] return user_list # 获取个人主页视频 @classmethod def get_videoList(cls, log_type, crawler, strategy, p_mid, oss_endpoint, env): try: while True: url = "https://api.xiaoniangao.cn/profile/list_album" headers = { # "X-Mid": str(cls.follow_x_mid), "X-Mid": '1fb47aa7a860d9', # "X-Token-Id": str(cls.follow_x_token_id), "X-Token-Id": '9f2cb91f9952c107ecb73642083e1dec-1145266232', "content-type": "application/json", # "uuid": str(cls.follow_uid), "uuid": 'f40c2e7c-3cfb-4804-b513-608c0280268c', "Accept-Encoding": "gzip,compress,br,deflate", "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)" " AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 " "MicroMessenger/8.0.20(0x18001435) NetType/WIFI Language/zh_CN", # "Referer": str(cls.follow_referer) "Referer": 'https://servicewechat.com/wxd7911e4c177690e4/654/page-frame.html' } json_text = { "visited_mid": str(p_mid), "start_t": cls.next_t, "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!690x385r/crop/690x385/interlace/1/format/jpg", "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!120x120r/crop/120x120/interlace/1/format/jpg", "limit": 20, # "token": str(cls.follow_token), "token": '54e4c603f7bf3dc009c86b49ed91be36', # "uid": str(cls.follow_uid), "uid": 'f40c2e7c-3cfb-4804-b513-608c0280268c', "proj": "ma", "wx_ver": "8.0.23", "code_ver": "3.68.0", "log_common_params": { "e": [{ "data": { "page": "profilePage", "topic": "public" } }], "ext": { "brand": "iPhone", "device": "iPhone 11", "os": "iOS 14.7.1", "weixinver": "8.0.23", "srcver": "2.24.7", "net": "wifi", "scene": "1089" }, "pj": "1", "pf": "2", "session_id": "7468cf52-00ea-432e-8505-6ea3ad7ec164" } } urllib3.disable_warnings() r = requests.post(url=url, headers=headers, json=json_text, proxies=proxies, verify=False) if 'data' not in r.text or r.status_code != 200: Common.logger(log_type, crawler).info(f"get_videoList:{r.text}\n") cls.next_t = None return elif 'list' not in r.json()['data']: Common.logger(log_type, crawler).info(f"get_videoList:{r.json()}\n") cls.next_t = None return elif len(r.json()['data']['list']) == 0: Common.logger(log_type, crawler).info(f"没有更多数据啦~\n") cls.next_t = None return else: cls.next_t = r.json()["data"]["next_t"] feeds = r.json()["data"]["list"] for i in range(len(feeds)): # 标题,表情随机加在片头、片尾,或替代句子中间的标点符号 char_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "BhlbST") expression_list = [] char_list = [] for q in range(len(char_sheet)): if char_sheet[q][0] is not None: expression_list.append(char_sheet[q][0]) if char_sheet[q][1] is not None: char_list.append(char_sheet[q][1]) befor_video_title = feeds[i]["title"].strip().replace("\n", "") \ .replace("/", "").replace("\r", "").replace("#", "") \ .replace(".", "。").replace("\\", "").replace("&NBSP", "") \ .replace(":", "").replace("*", "").replace("?", "") \ .replace("?", "").replace('"', "").replace("<", "") \ .replace(">", "").replace("|", "").replace(" ", "") expression = random.choice(expression_list) expression_title_list = [expression + befor_video_title, befor_video_title + expression] # 标题,表情随机加在片头 title_list1 = random.choice(expression_title_list) # 标题,表情随机加在片尾 title_list2 = befor_video_title + random.choice(char_list) # # 替代句子中间的标点符号 # title_list3 = befor_video_title.replace( # ",", random.choice(expression_list)).replace(",", random.choice(expression_list)) title_list4 = [title_list1, title_list2] video_title = random.choice(title_list4) # 用户名 user_name = feeds[i]["album_user"]["nick"].strip().replace("\n", "") \ .replace("/", "").replace("快手", "").replace(" ", "") \ .replace(" ", "").replace("&NBSP", "").replace("\r", "") # 视频 ID if "vid" in feeds[i]: video_id = feeds[i]["vid"] else: video_id = 0 # 播放量 if "play_pv" in feeds[i]: video_play_cnt = feeds[i]["play_pv"] else: video_play_cnt = 0 # 点赞 if "total" in feeds[i]["favor"]: video_like_cnt = feeds[i]["favor"]["total"] else: video_like_cnt = 0 # 评论数 if "comment_count" in feeds[i]: video_comment_cnt = feeds[i]["comment_count"] else: video_comment_cnt = 0 # 分享 if "share" in feeds[i]: video_share_cnt = feeds[i]["share"] else: video_share_cnt = 0 # 时长 if "du" in feeds[i]: video_duration = int(feeds[i]["du"] / 1000) else: video_duration = 0 # 发布时间 if "t" in feeds[i]: publish_time_stamp = int(feeds[i]["t"] / 1000) else: publish_time_stamp = 0 publish_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(publish_time_stamp)) # 宽和高 if "w" in feeds[i] or "h" in feeds[i]: video_width = feeds[i]["w"] video_height = feeds[i]["h"] else: video_width = 0 video_height = 0 # 头像 if "hurl" in feeds[i]["album_user"]: head_url = feeds[i]["album_user"]["hurl"] else: head_url = 0 # 用户 ID if "id" in feeds[i]: profile_id = feeds[i]["id"] else: profile_id = 0 # 用户 mid if "mid" in feeds[i]: profile_mid = feeds[i]["mid"] else: profile_mid = 0 # 封面 if "url" in feeds[i]: cover_url = feeds[i]["url"] else: cover_url = 0 # 视频播放地址 if "v_url" in feeds[i]: video_url = feeds[i]["v_url"] else: video_url = 0 # 过滤无效视频 if video_id == 0 \ or video_title == 0 \ or publish_time_stamp == 0 \ or video_duration == 0 \ or video_url == 0: Common.logger(log_type, crawler).info("无效视频\n") elif int(time.time()) - publish_time_stamp > 3600 * 24 * 3: Common.logger(log_type, crawler).info(f"发布时间超过3天:{publish_time_str}\n") cls.next_t = None return else: video_dict = { "video_id": video_id, "video_title": video_title, "duration": video_duration, "play_cnt": video_play_cnt, "like_cnt": video_like_cnt, "comment_cnt": video_comment_cnt, "share_cnt": video_share_cnt, "user_name": user_name, "publish_time_stamp": publish_time_stamp, "publish_time_str": publish_time_str, "video_width": video_width, "video_height": video_height, "avatar_url": head_url, "profile_id": profile_id, "profile_mid": profile_mid, "cover_url": cover_url, "video_url": video_url, "session": f"xiaoniangao-follow-{int(time.time())}" } for k, v in video_dict.items(): Common.logger(log_type, crawler).info(f"{k}:{v}") cls.download_publish(log_type=log_type, crawler=crawler, strategy=strategy, video_dict=video_dict, oss_endpoint=oss_endpoint, env=env) except Exception as error: Common.logger(log_type, crawler).error(f"获取个人主页视频异常:{error}\n") @classmethod def repeat_video(cls, log_type, crawler, video_id, env): sql = f""" select * from crawler_video where platform="小年糕" and out_video_id="{video_id}"; """ repeat_video = MysqlHelper.get_values(log_type, crawler, sql, env) return len(repeat_video) # 下载/上传 @classmethod def download_publish(cls, log_type, crawler, strategy, video_dict, oss_endpoint, env): try: if cls.download_rule(video_dict) is False: Common.logger(log_type, crawler).info("不满足基础门槛\n") elif cls.repeat_video(log_type, crawler, video_dict['video_id'], env) != 0: Common.logger(log_type, crawler).info('视频已下载\n') elif any(str(word) if str(word) in video_dict['video_title'] else False for word in filter_word(log_type, crawler, "小年糕", env)) is True: Common.logger(log_type, crawler).info("视频已中过滤词\n") else: # 下载封面 Common.download_method(log_type=log_type, crawler=crawler, text="cover", title=video_dict["video_title"], url=video_dict["cover_url"]) # 下载视频 Common.download_method(log_type=log_type, crawler=crawler, text="video", title=video_dict["video_title"], url=video_dict["video_url"]) # 保存视频信息至 "./videos/{download_video_title}/info.txt" Common.save_video_info(log_type=log_type, crawler=crawler, video_dict=video_dict) # 上传视频 Common.logger(log_type, crawler).info("开始上传视频...") our_video_id = Publish.upload_and_publish(log_type=log_type, crawler=crawler, strategy=strategy, our_uid="follow", env=env, oss_endpoint=oss_endpoint) if env == "dev": our_video_link = f"https://testadmin.piaoquantv.com/cms/post-detail/{our_video_id}/info" else: our_video_link = f"https://admin.piaoquantv.com/cms/post-detail/{our_video_id}/info" Common.logger(log_type, crawler).info("视频上传完成") if our_video_id is None: # 删除视频文件夹 shutil.rmtree(f"./{crawler}/videos/{video_dict['video_title']}") return # 视频信息保存数据库 rule_dict = { "duration": {"min": 40, "max": 100000000}, "play_cnt": {"min": 500} } insert_sql = f""" insert into crawler_video(video_id, out_user_id, platform, strategy, out_video_id, video_title, cover_url, video_url, duration, publish_time, play_cnt, crawler_rule, width, height) values({our_video_id}, "{video_dict['profile_id']}", "{cls.platform}", "定向爬虫策略", "{video_dict['video_id']}", "{video_dict['video_title']}", "{video_dict['cover_url']}", "{video_dict['video_url']}", {int(video_dict['duration'])}, "{video_dict['publish_time_str']}", {int(video_dict['play_cnt'])}, '{json.dumps(rule_dict)}', {int(video_dict['video_width'])}, {int(video_dict['video_height'])}) """ Common.logger(log_type, crawler).info(f"insert_sql:{insert_sql}") MysqlHelper.update_values(log_type, crawler, insert_sql, env) Common.logger(log_type, crawler).info('视频信息插入数据库成功!') # 视频写入飞书 Feishu.insert_columns(log_type, crawler, "Wu0CeL", "ROWS", 1, 2) # 视频ID工作表,首行写入数据 upload_time = int(time.time()) values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(upload_time)), "用户主页", str(video_dict['video_id']), str(video_dict['video_title']), our_video_link, video_dict['play_cnt'], video_dict['comment_cnt'], video_dict['like_cnt'], video_dict['share_cnt'], video_dict['duration'], f"{video_dict['video_width']}*{video_dict['video_height']}", str(video_dict['publish_time_str']), str(video_dict['user_name']), str(video_dict['profile_id']), str(video_dict['profile_mid']), str(video_dict['avatar_url']), str(video_dict['cover_url']), str(video_dict['video_url'])]] time.sleep(1) Feishu.update_values(log_type, crawler, "Wu0CeL", "F2:Z2", values) Common.logger(log_type, crawler).info('视频信息写入飞书成功\n') except Exception as e: Common.logger(log_type, crawler).error("下载/上传异常:{}", e) # 获取所有关注列表的用户视频 @classmethod def get_follow_videos(cls, log_type, crawler, strategy, oss_endpoint, env): try: # 已关注的用户列表 mids user_list = cls.get_users() for user in user_list: user_name = user['user_name'] profile_mid = user['profile_mid'] Common.logger(log_type, crawler).info(f"获取 {user_name} 主页视频") cls.get_videoList(log_type=log_type, crawler=crawler, strategy=strategy, p_mid=profile_mid, oss_endpoint=oss_endpoint, env=env) cls.next_t = None time.sleep(1) except Exception as e: Common.logger(log_type, crawler).error(f"get_follow_videos:{e}\n") if __name__ == "__main__": # print(XiaoniangaoFollow.repeat_video("follow", "xiaoniangao", "4919087666", "prod", "aliyun")) # print(XiaoniangaoFollow.repeat_video("follow", "xiaoniangao", "4919087666", "dev")) print(XiaoniangaoFollow.get_users()) pass