Skip to content

Commit

Permalink
Merge pull request #18 from tsoding/16-credentials-auth
Browse files Browse the repository at this point in the history
Implement authorization and credential storage
  • Loading branch information
rexim committed Jun 10, 2017
2 parents a422f4e + 9c6b821 commit 392244b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
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

0 comments on commit 392244b

Please sign in to comment.