diff --git a/classes/video.py b/classes/video.py index 03cd19e..193541d 100644 --- a/classes/video.py +++ b/classes/video.py @@ -62,4 +62,4 @@ class Video: with open(os.path.join( self.path, "video.yaml"), "w") as f: yaml.dump(video_meta_file, f) f.close() - return video_meta_file \ No newline at end of file + return self \ No newline at end of file diff --git a/main.py b/main.py index 062bed7..f11967e 100644 --- a/main.py +++ b/main.py @@ -64,7 +64,7 @@ async def main(): video = await channel.generate_video(idea) printm("Done!") printm("Here is the video:") - printm(video.url) + printm(video) if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main()) diff --git a/utils/uploader.py b/utils/uploader.py index c934c15..53ecc1d 100644 --- a/utils/uploader.py +++ b/utils/uploader.py @@ -38,7 +38,7 @@ VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted') # 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 = "" try: CLIENT_SECRETS_FILE=os.path.join(credentialsPath, "client_secret.json") @@ -52,7 +52,7 @@ async def get_authenticated_service(credentialsPath=""): break if CLIENT_SECRETS_FILE == "": 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: data = json.load(json_file) credentials = google.oauth2.credentials.Credentials( @@ -146,16 +146,28 @@ async def upload_video(path, title, description, category, keywords, privacyStat 'keywords': keywords, 'privacyStatus': privacyStatus } - youtube = await get_authenticated_service(credentials_path) - try: - videoid = await initialize_upload(youtube, options) - await upload_thumbnail(videoid, path + "/miniature.png", credentials_path) - return videoid - except HttpError as e: - print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)) + refresh = False + while True: + try: + youtube = await get_authenticated_service(credentials_path, force_refresh=refresh) + videoid = await initialize_upload(youtube, options) + await upload_thumbnail(videoid, path + "/miniature.png", credentials_path, youtube) + 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 videoId=video_id, media_body=file