import json import random import time import requests from common import Common, AliyunLogger, Feishu from common.sql_help import sqlCollect from data_channel.data_help import dataHelp class SPH: @classmethod def find_target_user(cls, name, user_list): """ 在搜索到到账号列表中找目标列表 """ for obj in user_list: if obj['contact']["nickname"] == name: return obj else: continue return False @classmethod def get_account_id(cls, account_name): channel = 'shipinhao' history_id = sqlCollect.get_history_id(channel, account_name) if history_id: return history_id else: url = "http://47.236.68.175:8889/crawler/wei_xin/shi_pin_hao/account_info" payload = json.dumps({ "account_name": account_name }) headers = { 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) response = response.json() if response['code'] == 0: data = response['data']['data'] channel_account_id = data['channel_account_id'] if channel_account_id: sqlCollect.insert_history_id(account_name, channel_account_id, channel) return channel_account_id else: return False else: Feishu.finish_bot("shi_pin_hao/account_info接口获取失败", "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb", "【视频号接口异常提示 】") return False @classmethod def get_sph_url(cls, task_mark, url_id, number, mark, channel_id, name): account_id = cls.get_account_id(url_id) if account_id: url = "http://47.236.68.175:8889/crawler/wei_xin/shi_pin_hao/blogger" next_cursor = "" list = [] # for i in range(10): headers = { 'Content-Type': 'application/json' } payload = json.dumps({ "account_id": account_id, "cursor": next_cursor }) try: response = requests.request("POST", url, headers=headers, data=payload) time.sleep(random.randint(1, 5)) res_json = response.json() if res_json['code'] == 0: # next_cursor = res_json['data']['next_cursor'] data_lsit = res_json['data']['data'] if data_lsit == None: return list for obj in data_lsit: objectId = obj['id'] status = sqlCollect.is_used(task_mark, objectId, mark, "视频号") old_title = obj['objectDesc']['description'] url_p = obj['objectDesc']['media'][0]['Url'] url_token = obj['objectDesc']['media'][0]['urlToken'] video_url = f"{url_p}{url_token}" decode_key = obj['objectDesc']['media'][0]['decodeKey'] cover = obj['objectDesc']['media'][0]['coverUrl'] share_cnt = int(obj['forwardCount']) # 分享 like_cnt = int(obj['likeCount']) # 点赞 duration = int(obj['objectDesc']['media'][0]['VideoPlayLen']) # duration = int(duration_ms) / 1000 log_data = f"user:{url_id},,video_id:{objectId},,video_url:{video_url},,original_title:{old_title},,share_count:{share_cnt},,like_count:{like_cnt},,duration:{duration}" AliyunLogger.logging(channel_id, name, url_id, objectId, "扫描到一条视频", "2001", log_data) Common.logger("sph").info( f"扫描:{task_mark},用户主页id:{url_id},视频id{objectId} ,分享:{share_cnt},点赞:{like_cnt}") if status: AliyunLogger.logging(channel_id, name, url_id, objectId, "该视频已改造过", "2002", log_data) continue video_percent = '%.2f' % (share_cnt / like_cnt) special = float(0.25) if like_cnt >= 30000 or like_cnt >= 50000 or (share_cnt >= 300 and float(video_percent) >= special): if int(duration) < 30 or int(duration) > 720: Common.logger("sph").info( f"任务:{task_mark},用户主页id:{url_id},视频id{objectId} ,分享:{share_cnt},点赞:{like_cnt} ,时长:{duration} ") AliyunLogger.logging(channel_id, name, url, objectId, "不符合规则:时长不符合规则大于720秒/小于30秒", "2003", log_data) continue all_data = {"video_id": objectId, "cover": cover, "video_url": video_url, "rule": video_percent, "old_title": old_title, "decode_key": decode_key} list.append(all_data) AliyunLogger.logging(channel_id, name, url_id, objectId, "符合规则等待改造", "2004", log_data) if len(list) == int(number): Common.logger(mark).info(f"获取视频号视频总数:{len(list)}\n") return list else: AliyunLogger.logging(channel_id, name, url_id, objectId, "不符合规则:点赞小于30000/50000 或 分享/点赞小于0.25和分享小于300", "2003", log_data) Common.logger("sph").info( f"不符合规则:{task_mark},用户主页id:{url_id},视频id{objectId} ,分享:{share_cnt},点赞:{like_cnt}") continue return list except Exception as e: print(e) return list if __name__ == '__main__': SPH.get_sph_url('1',"人民日报",'10','2',"视频号",'视频号品类账号')