Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement authorization and credential storage #18

Merged
merged 4 commits into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions commons/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
from os.path import expanduser


# TODO(#19): configuration scheme
#
# Maintain actual configuration scheme and verify it on any
# configuration loading
def home_tsoding_config():
return read_yaml_file(expanduser("~/.tsoding.yaml"))
5 changes: 5 additions & 0 deletions commons/functional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def arrow(x, *fs):
result = x
for f in fs:
result = f(result)
return result
36 changes: 28 additions & 8 deletions youtube/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,44 @@
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow

from functional import arrow

# TODO(bc02b696-9ad8-4572-8faa-58caed05f612): authorization and credential storage
#
# Develop a mechanism for storing and retrieving credentials for
# ytservice_from_credentials() and authorizing the Tsoding Tools to
# get those credentials in the first place.
#
# For more info see https://developers.google.com/api-client-library/python/guide/aaa_oauth

# TODO(#20): document how to retrieve the client secrets file
def flow_from_config(tsoding_config):
return flow_from_clientsecrets(
tsoding_config['youtube']['client_secrets_file'],
scope="https://www.googleapis.com/auth/youtube"
)


def credentials_from_flow(flow):
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()

if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage)

return credentials


def ytservice_from_credentials(credentials):
"""Constructs YouTube Service"""
"""Constructs YouTube Service from Credentials"""
return build("youtube",
"v3",
http=credentials.authorize(httplib2.Http()))


def ytservice_from_config(tsoding_config):
"""Constructs YouTube Service from Tsoding Config"""
return arrow(tsoding_config,
flow_from_config,
credentials_from_flow,
ytservice_from_credentials)


# TODO(f2430622-3245-41e3-9761-35b65f40e9fa): implement unlist_playlist_videos()
def unlist_playlist_videos(ytservice, playlist):
"""Unlists all of the videos in a specific playlist using YouTube API.
Expand Down