Advertisement
Guest User

G30rg du Cuxxl

a guest
Mar 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1.  
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5.  
  6. import google.oauth2.credentials
  7.  
  8. import google_auth_oauthlib.flow
  9. from googleapiclient.discovery import build
  10. from googleapiclient.errors import HttpError
  11. from google_auth_oauthlib.flow import InstalledAppFlow
  12.  
  13. # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
  14. # the OAuth 2.0 information for this application, including its client_id and
  15. # client_secret.
  16. CLIENT_SECRETS_FILE = "client_secret.json"
  17.  
  18. # This OAuth 2.0 access scope allows for full read/write access to the
  19. # authenticated user's account and requires requests to use an SSL connection.
  20. SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']
  21. API_SERVICE_NAME = 'youtube'
  22. API_VERSION = 'v3'
  23.  
  24. def get_authenticated_service():
  25.   flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
  26.   credentials = flow.run_console()
  27.   return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
  28.  
  29. def print_response(response):
  30.   print(response)
  31.  
  32. # Build a resource based on a list of properties given as key-value pairs.
  33. # Leave properties with empty values out of the inserted resource.
  34. def build_resource(properties):
  35.   resource = {}
  36.   for p in properties:
  37.     # Given a key like "snippet.title", split into "snippet" and "title", where
  38.     # "snippet" will be an object and "title" will be a property in that object.
  39.     prop_array = p.split('.')
  40.     ref = resource
  41.     for pa in range(0, len(prop_array)):
  42.       is_array = False
  43.       key = prop_array[pa]
  44.  
  45.       # For properties that have array values, convert a name like
  46.       # "snippet.tags[]" to snippet.tags, and set a flag to handle
  47.       # the value as an array.
  48.       if key[-2:] == '[]':
  49.         key = key[0:len(key)-2:]
  50.         is_array = True
  51.  
  52.       if pa == (len(prop_array) - 1):
  53.         # Leave properties without values out of inserted resource.
  54.         if properties[p]:
  55.           if is_array:
  56.             ref[key] = properties[p].split(',')
  57.           else:
  58.             ref[key] = properties[p]
  59.       elif key not in ref:
  60.         # For example, the property is "snippet.title", but the resource does
  61.         # not yet have a "snippet" object. Create the snippet object here.
  62.         # Setting "ref = ref[key]" means that in the next time through the
  63.         # "for pa in range ..." loop, we will be setting a property in the
  64.         # resource's "snippet" object.
  65.         ref[key] = {}
  66.         ref = ref[key]
  67.       else:
  68.         # For example, the property is "snippet.description", and the resource
  69.         # already has a "snippet" object.
  70.         ref = ref[key]
  71.   return resource
  72.  
  73. # Remove keyword arguments that are not set
  74. def remove_empty_kwargs(**kwargs):
  75.   good_kwargs = {}
  76.   if kwargs is not None:
  77.     for key, value in kwargs.iteritems():
  78.       if value:
  79.         good_kwargs[key] = value
  80.   return good_kwargs
  81.  
  82. def search_list_by_keyword(client, **kwargs):
  83.   # See full sample for function
  84.   kwargs = remove_empty_kwargs(**kwargs)
  85.  
  86.   response = client.search().list(
  87.     **kwargs
  88.   ).execute()
  89.  
  90.   return print_response(response)
  91.  
  92.  
  93. if __name__ == '__main__':
  94.   # When running locally, disable OAuthlib's HTTPs verification. When
  95.   # running in production *do not* leave this option enabled.
  96.   os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
  97.   client = get_authenticated_service()
  98.  
  99.   search_list_by_keyword(client,
  100.     part='snippet',
  101.     maxResults=25,
  102.     q='katzenvideos',
  103.     type='')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement