I want to upload a video with Youtube API and because I have a big file I want to use a Resumable Uploads type of request but I do not know how to implement it in my code quickly my code is here
google.py
import pickleimport osfrom google_auth_oauthlib.flow import Flow, InstalledAppFlowfrom googleapiclient.discovery import buildfrom googleapiclient.http import MediaFileUpload, MediaIoBaseDownloadfrom google.auth.transport.requests import Requestimport datetimedef Create_Service(client_secret_file, api_name, api_version, *scopes): print(client_secret_file, api_name, api_version, scopes, sep='-') CLIENT_SECRET_FILE = client_secret_file API_SERVICE_NAME = api_name API_VERSION = api_version SCOPES = [scope for scope in scopes[0]] print(SCOPES) cred = None pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle' # print(pickle_file) if os.path.exists(pickle_file): with open(pickle_file, 'rb') as token: cred = pickle.load(token) if not cred or not cred.valid: if cred and cred.expired and cred.refresh_token: cred.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES) cred = flow.run_local_server() with open(pickle_file, 'wb') as token: pickle.dump(cred, token) try: service = build(API_SERVICE_NAME, API_VERSION, credentials=cred) print(API_SERVICE_NAME, 'service created successfully') return service except Exception as e: print('Unable to connect.') print(e) return Nonedef convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0): dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() +'Z' return dt
upload_video.py
import datetimefrom Google import Create_Servicefrom googleapiclient.http import MediaFileUploadCLIENT_SECRET_FILE = 'youtube_client.json'API_NAME = 'youtube'API_VERSION = 'v3'SCOPES = ['https://www.googleapis.com/auth/youtube.upload']service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)upload_date_time = datetime.datetime(2022, 12, 25, 12, 30, 0).isoformat() +'.000Z'request_body = {'snippet': {'categoryI': 10,'title': 'best music on the youtube | happy mood mix | AMP','description': "test",'tags': ['Travel', 'video test', 'Travel Tips'] },'status': {'privacyStatus': 'private','publishAt': upload_date_time,'selfDeclaredMadeForKids': False, },'notifySubscribers': False}mediaFile = MediaFileUpload('output2.mp4')response_upload = service.videos().insert( part='snippet,status', body=request_body, media_body=mediaFile).execute()service.thumbnails().set( videoId=response_upload.get('id'), media_body=MediaFileUpload('thumbnail.png')).execute()
I want to implement a resumable function to upload_video.py
def resumable_upload(request): response = None error = None retry = 0 while response is None: try: print('Uploading file...') status, response = request.next_chunk() if response is not None: if 'id' in response: print('Video id "%s" was successfully uploaded.' % response['id']) else: exit('The upload failed with an unexpected response: %s' % response) except HttpError as e: if e.resp.status in RETRIABLE_STATUS_CODES: error = 'A retriable HTTP error %d occurred:\n%s' % (e.resp.status, e.content) else: raise except RETRIABLE_EXCEPTIONS, e: error = 'A retriable error occurred: %s' % e if error is not None: print(error) retry += 1 if retry > MAX_RETRIES: exit('No longer attempting to retry.') max_sleep = 2 ** retry sleep_seconds = random.random() * max_sleep print('Sleeping %f seconds and then retrying...' % sleep_seconds) time.sleep(sleep_seconds)
I found a lot of questions there but always was some error there so I am starting from zero I think that I should change just 1 line of code but I do not know which one.