import argparse import random import time import Contant from LoggerUtils import Logger, initLogger import Orm from VideoService import VideoService from ChannelService import ChannelService from VideoCountService import ViewCountService from func_timeout import func_set_timeout import func_timeout import requests import httplib2 import googleapiclient.discovery import googleapiclient.errors import datetime apiIndex = 0 apiKeys = [ "AIzaSyDjPkCgDQ9Tv_xcChjY2E6GpJ6IzngnD5I", "AIzaSyAxIycOdQYGB5kWhwe3B-kJAYRo7wOnp8o", "AIzaSyCsYUC5vN0pB6y9xsCj0B1ehAoqOJ3WMf0" ] def getYoutube(): proxy_info = httplib2.ProxyInfo( proxy_type=httplib2.socks.PROXY_TYPE_HTTP, proxy_host="127.0.0.1", proxy_port=7890) # http = httplib2.Http(timeout=10, proxy_info=proxy_info, # disable_ssl_certificate_validation=False) http = httplib2.Http(timeout=10, disable_ssl_certificate_validation=False) # http = httplib2.Http(timeout=10, disable_ssl_certificate_validation=False) api_service_name = "youtube" api_version = "v3" # 获取apiKey apiKey = "AIzaSyARaW3mqO9szQiHgWZR4el0HWvdyheSHBc" # 获取对象 youtube = googleapiclient.discovery.build( api_service_name, api_version, developerKey=Contant.apiKeys[Contant.apiIndex], http=http ) return youtube def updateVideoViewCount(day, startTime, endTime): list = VideoService.getVideosByTime(startTime, endTime) Logger.info(len(list)) videoCount = 0 totalCount = 0 videosRequest = "" youtube = getYoutube() for video in list: videoCount = videoCount + 1 totalCount = totalCount + 1 Logger.info(video.videoId) videosRequest = videosRequest + "," + video.videoId if videoCount == 50 or videoCount == len(list) or totalCount == len(list): request = youtube.videos().list(part="statistics", id=videosRequest) if Contant.apiIndex < (len(Contant.apiKeys) - 1): Contant.apiIndex = Contant.apiIndex + 1 else: Contant.apiIndex = 0 response = request.execute() for item in response['items']: try: Logger.info(item) ViewCountService.createOrUpdateOne( item['id'], day, item['statistics']['viewCount']) except Exception as e: Logger.error("存储失败{}".format(item)) videosRequest = "" videoCount = 0 # python ./view_count_main.py --db="../db/youtube_prod.db" --logDir="./logs" --start="2024-01-03T00:00:00Z" --end="2024-01-04T00:00:00Z" if __name__ == "__main__": parser = argparse.ArgumentParser(description='') parser.add_argument('--db', type=str, default='') parser.add_argument('--logDir', type=str, default='') args = parser.parse_args() Contant.db = args.db Contant.logDir = args.logDir initLogger() Orm.ormInit() # 查询30天内的所有视屏 now = datetime.datetime.now() zero_today = now.replace(hour=0, minute=0, second=0, microsecond=0) end_today = now.replace(hour=23, minute=59, second=59, microsecond=0) for i in range(1, 31): startTime = zero_today+datetime.timedelta(days=-i) endTime = end_today+datetime.timedelta(days=-i) startTime = startTime.strftime("%Y-%m-%dT%H:%S:%MZ") endTime = endTime.strftime("%Y-%m-%dT%H:%S:%MZ") Logger.info("day:%d, startTime:%s, endTime:%s" % (i, startTime, endTime)) updateVideoViewCount(i, startTime, endTime) # zero_today = zero_today.strftime("%y-%m-%dT%H:%S:%MZ") # print(zero_today)