Skip to content

Commit

Permalink
Spacing changed.
Browse files Browse the repository at this point in the history
Added 308 redirect change to fix #293
  • Loading branch information
kootsoop committed May 7, 2020
1 parent 7373aa9 commit 6a30b55
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
16 changes: 10 additions & 6 deletions youtube_upload/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"""Wrapper for Google OAuth2 API."""
import sys
import json

import googleapiclient.discovery
import httplib2
import oauth2client
import httplib2

from youtube_upload import lib
from youtube_upload.auth import console
from youtube_upload.auth import browser
from youtube_upload.auth import console
from oauth2client import client
from oauth2client import file

YOUTUBE_UPLOAD_SCOPE = ["https://www.googleapis.com/auth/youtube.upload", "https://www.googleapis.com/auth/youtube"]


def _get_credentials_interactively(flow, storage, get_code_callback):
"""Return the credentials asking the user."""
flow.redirect_uri = oauth2client.client.OOB_CALLBACK_URN
Expand All @@ -23,6 +23,7 @@ def _get_credentials_interactively(flow, storage, get_code_callback):
credential.set_store(storage)
return credential


def _get_credentials(flow, storage, get_code_callback):
"""Return the user credentials. If not found, run the interactive flow."""
existing_credentials = storage.get()
Expand All @@ -31,12 +32,15 @@ def _get_credentials(flow, storage, get_code_callback):
else:
return _get_credentials_interactively(flow, storage, get_code_callback)


def get_resource(client_secrets_file, credentials_file, get_code_callback):
"""Authenticate and return a googleapiclient.discovery.Resource object."""
get_flow = oauth2client.client.flow_from_clientsecrets
flow = get_flow(client_secrets_file, scope=YOUTUBE_UPLOAD_SCOPE)
storage = oauth2client.file.Storage(credentials_file)
credentials = _get_credentials(flow, storage, get_code_callback)
if credentials:
http = credentials.authorize(httplib2.Http())
httplib = httplib2.Http()
httplib.redirect_codes = httplib.redirect_codes - {308}
http = credentials.authorize(httplib)
return googleapiclient.discovery.build("youtube", "v3", http=http)
14 changes: 8 additions & 6 deletions youtube_upload/upload_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
googleapiclient.errors.HttpError,
]


def _upload_to_request(request, progress_callback):
"""Upload a video to a Youtube request. Return video ID."""
while 1:
Expand All @@ -31,13 +32,14 @@ def _upload_to_request(request, progress_callback):
else:
raise KeyError("Expected field 'id' not found in response")

def upload(resource, path, body, chunksize=4*1024*1024,
progress_callback=None, max_retries=10):

def upload(resource, path, body, chunksize=4 * 1024 * 1024,
progress_callback=None, max_retries=10):
"""Upload video to Youtube. Return video ID."""
body_keys = ",".join(body.keys())
media = apiclient.http.MediaFileUpload(path, chunksize=chunksize,
resumable=True, mimetype="application/octet-stream")
media = apiclient.http.MediaFileUpload(path, chunksize=chunksize,
resumable=True, mimetype="application/octet-stream")
request = resource.videos().insert(part=body_keys, body=body, media_body=media)
upload_fun = lambda: _upload_to_request(request, progress_callback)
return lib.retriable_exceptions(upload_fun,
RETRIABLE_EXCEPTIONS, max_retries=max_retries)
return lib.retriable_exceptions(upload_fun,
RETRIABLE_EXCEPTIONS, max_retries=max_retries)

0 comments on commit 6a30b55

Please sign in to comment.