fix(video.py): return self instead of video_meta_file

fix(main.py): print video object instead of video url
feat(uploader.py): add retry mechanism to handle token expiration and refresh token if necessary. Also, pass youtube object to upload_thumbnail function.
This commit is contained in:
Paillat
2023-06-25 18:16:19 +02:00
parent 9d11b67ebd
commit f0dff73ce7
3 changed files with 25 additions and 13 deletions

View File

@@ -62,4 +62,4 @@ class Video:
with open(os.path.join( self.path, "video.yaml"), "w") as f: with open(os.path.join( self.path, "video.yaml"), "w") as f:
yaml.dump(video_meta_file, f) yaml.dump(video_meta_file, f)
f.close() f.close()
return video_meta_file return self

View File

@@ -64,7 +64,7 @@ async def main():
video = await channel.generate_video(idea) video = await channel.generate_video(idea)
printm("Done!") printm("Done!")
printm("Here is the video:") printm("Here is the video:")
printm(video.url) printm(video)
if __name__ == "__main__": if __name__ == "__main__":
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(main()) loop.run_until_complete(main())

View File

@@ -38,7 +38,7 @@ VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
# Authorize the request and store authorization credentials. # Authorize the request and store authorization credentials.
async def get_authenticated_service(credentialsPath=""): async def get_authenticated_service(credentialsPath="", force_refresh=False):
CLIENT_SECRETS_FILE = "" CLIENT_SECRETS_FILE = ""
try: try:
CLIENT_SECRETS_FILE=os.path.join(credentialsPath, "client_secret.json") CLIENT_SECRETS_FILE=os.path.join(credentialsPath, "client_secret.json")
@@ -52,7 +52,7 @@ async def get_authenticated_service(credentialsPath=""):
break break
if CLIENT_SECRETS_FILE == "": if CLIENT_SECRETS_FILE == "":
raise FileNotFoundError("No client_secret.json file found in the specified path !") raise FileNotFoundError("No client_secret.json file found in the specified path !")
if os.path.exists(f'{credentialsPath}/credentials.json'): if os.path.exists(f'{credentialsPath}/credentials.json') and not force_refresh:
with open(f'{credentialsPath}/credentials.json') as json_file: with open(f'{credentialsPath}/credentials.json') as json_file:
data = json.load(json_file) data = json.load(json_file)
credentials = google.oauth2.credentials.Credentials( credentials = google.oauth2.credentials.Credentials(
@@ -146,16 +146,28 @@ async def upload_video(path, title, description, category, keywords, privacyStat
'keywords': keywords, 'keywords': keywords,
'privacyStatus': privacyStatus 'privacyStatus': privacyStatus
} }
youtube = await get_authenticated_service(credentials_path) refresh = False
try: while True:
videoid = await initialize_upload(youtube, options) try:
await upload_thumbnail(videoid, path + "/miniature.png", credentials_path) youtube = await get_authenticated_service(credentials_path, force_refresh=refresh)
return videoid videoid = await initialize_upload(youtube, options)
except HttpError as e: await upload_thumbnail(videoid, path + "/miniature.png", credentials_path, youtube)
print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)) return videoid
except HttpError as e:
print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content))
#escape the loop
break
except:
#refresh the token
if not refresh:
refresh = True
else:
#escape the loop
break
async def upload_thumbnail(video_id, file, credentials_path=""):
youtube = await get_authenticated_service(credentials_path)
async def upload_thumbnail(video_id, file, credentials_path="", youtube=None):
youtube.thumbnails().set( # type: ignore youtube.thumbnails().set( # type: ignore
videoId=video_id, videoId=video_id,
media_body=file media_body=file