我有这段代码可以提取有关我的YouTube频道的一些指标,并根据它们创建一个熊猫数据框。
import os
import google.oauth2.credentials
import google_auth_oauthlib.flow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow
import json
SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly']
API_SERVICE_NAME = 'youtubeAnalytics'
API_VERSION = 'v2'
CLIENT_SECRETS_FILE = 'client_secrets.json'
def get_service():
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
credentials = flow.run_console()
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
def execute_api_request(client_library_function, **kwargs):
response = client_library_function(
**kwargs
).execute()
with open('data.json', 'w') as fp:
json.dump(response, fp)
if __name__ == '__main__':
# Disable OAuthlib's HTTPs verification when running locally.
# *DO NOT* leave this option enabled when running in production.
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
youtubeAnalytics = get_service()
execute_api_request(
youtubeAnalytics.reports().query,
ids='channel==MINE',
startDate='2014-01-01',
endDate='2019-02-26',
metrics='averageViewDuration,views,likes,dislikes,subscribersGained,subscribersLost',
dimensions='day',
sort='day',
filters = 'country==US'
)
## Now, convert the json to dataframe
import json
import pandas as pd
with open('data.json') as json_data:
d = json.load(json_data)
colnames = [d['columnHeaders'][i]['name'] for i in range(0,len(d['columnHeaders']))]
Results = pd.DataFrame(d['rows'],columns = colnames)
Results.to_csv("Youtube_data.csv")
运行此代码,将打开一个窗口,要求我登录youtube,然后向我提供授权代码。输入此授权码即可完成上述python程序的运行。但是,每次运行此程序时,都应重复此授权过程。
反正有没有绕过这种重复授权以使该过程可以自动化吗?
答案 0 :(得分:0)
您需要使用oauth2client.file.Storage类来存储和检索凭据对象,如(严重)在此说明:https://developers.google.com/api-client-library/python/guide/aaa_oauth
您将需要使用以下内容来修改class background(object):
def __init__(self, x, y, vel, index):
self.x = x
self.y = y
self.index = index
self.vel = vel
def draw(self, win):
win.blit(bg[self.index], (self.x, self.y))
if self.x < -800:
self.x = 800
def bgUpade(self):
if not player_one.isDead:
bg_one.x -= bg_one.vel
bg_two.x -= bg_two.vel
函数:
get_service
希望这会有所帮助