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 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) api_service_name = "youtube" api_version = "v3" # 获取apiKey apiKey = "AIzaSyARaW3mqO9szQiHgWZR4el0HWvdyheSHBc" # 获取对象 youtube = googleapiclient.discovery.build( api_service_name, api_version, developerKey=apiKey, http=http ) return youtube def updateVideoViewCount(startTime, endTime): list = VideoService.getVideosByTime(startTime, endTime) videoCount = 0 videosRequest = "" youtube = getYoutube() for video in list: videoCount = videoCount + 1 Logger.info(video.videoId) videosRequest = videosRequest + "," + video.videoId if videoCount == 30 or videoCount == len(list): request = youtube.videos().list(part="statistics", id=videosRequest) response = request.execute() for item in response['items']: Logger.info(item) ViewCountService.createOrUpdateOne( item['id'], 1, item['statistics']['viewCount']) 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("startTime:%s, endTime:%s" % (startTime, endTime)) updateVideoViewCount(startTime, endTime) # zero_today = zero_today.strftime("%y-%m-%dT%H:%S:%MZ") # print(zero_today)