# -*- coding: utf-8 -*- # @Time: 2024/01/18 import datetime import os import random import sys import time from datetime import datetime import requests import json import urllib3 sys.path.append(os.getcwd()) from common.aliyun_oss_uploading import Oss from common.common import Common from common.material import Material from common.feishu import Feishu from common.db import MysqlHelper from requests.adapters import HTTPAdapter class ZhanNeiAuthor(): """ oss视频地址 存入数据库 """ @classmethod def insert_videoUrl(cls, video_id, account_id, oss_object_key, mark): current_time = datetime.now() formatted_time = current_time.strftime("%Y-%m-%d %H:%M") insert_sql = f"""INSERT INTO agc_video_url (video_id, account_id, oss_object_key, time, status, mark) values ("{video_id}", "{account_id}", "{oss_object_key}", "{formatted_time}", 1, "{mark}")""" MysqlHelper.update_values( sql=insert_sql, env="prod", machine="", ) """ 获取快手用户主页id """ @classmethod def get_zhannei_videoUserId(cls, mark): select_user_sql = f"""select user_id, channel from agc_channel_data where mark = '{mark}' and channel = '站内' ORDER BY id DESC;""" user_list = MysqlHelper.get_values(select_user_sql, "prod") return user_list """ 查询该video_id是否在数据库存在 """ @classmethod def select_videoUrl_id(cls, video_id, mark): select_user_sql = f"""select video_id from agc_video_url where video_id='{video_id}' and mark='{mark}' ;""" user_list = MysqlHelper.get_values(select_user_sql, "prod") if user_list: return True else: return False """站内读取数据 将数据存储到oss上""" @classmethod def get_zhannei_videoList(cls, data): try: mark = data['mark'] mark_name = data['mark_name'] feishu_id = data['feishu_id'] channel_id = data['channel'] channel = "站内" user_list = Material.insert_user(feishu_id, channel_id, mark, channel) if len(user_list) == 0: return for account_id in user_list: id = cls.select_videoUrl_id(account_id, mark) if id: Common.logger("zhannei").info( f"重复视频不在抓取该用户,用户主页id:{account_id}") continue time.sleep(5) Common.logger("zhannei").info(f"用户主页ID:{account_id}") cookie = Material.get_houtai_cookie() url = f"https://admin.piaoquantv.com/manager/video/detail/{account_id}" payload = {} headers = { 'authority': 'admin.piaoquantv.com', '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://admin.piaoquantv.com/cms/post-detail/{account_id}/detail', 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"macOS"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' } response = requests.request("GET", url, headers=headers, data=payload) data = response.json() code = data["code"] if code != 0: if "-" in mark: mark1 = mark.split("-")[0] Common.logger("video").info( f"未登录,请更换cookie,{data}") Feishu.bot('recommend', '管理后台', '管理后台cookie失效,请及时更换~', mark1, mark_name) return video_url = data["content"]["transedVideoPath"] channel_name = mark+'/zhannei' oss_object_key = Oss.video_sync_upload_oss(video_url, account_id, account_id, channel_name) status = oss_object_key.get("status") # 发送 oss oss_object_key = oss_object_key.get("oss_object_key") Common.logger("zhannei").info(f"站内视频链接oss发送成功,oss地址:{oss_object_key}") if status == 200: cls.insert_videoUrl(account_id, account_id, oss_object_key, mark) Common.logger("zhannei").info(f"视频地址插入数据库成功,视频id:{account_id},用户主页id:{account_id},视频储存地址:{oss_object_key}") except Exception as e: Common.logger("zhannei").warning(f"抓取异常:{e}\n") return