Beispiel #1
0
def get_credentials(*, scopes):
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    credential_dir = os.path.join(HOME_DIR, '.cache', __package__, 'credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir, 'googleapis.json')

    store = Storage(credential_path)
    credentials = store.get()

    # see https://developers.google.com/api-client-library/python/auth/web-app
    # kw: "incremental scopes"
    if not credentials or credentials.invalid or not credentials.has_scopes(scopes):
        flow = client.flow_from_clientsecrets(GOOGLE_SECRETS, scopes)
        flow.user_agent = 'Bonobo ETL (https://www.bonobo-project.org/)'
        flags = argparser.parse_args(['--noauth_local_webserver'])
        credentials = tools.run_flow(flow, store, flags)
        print('Storing credentials to ' + credential_path)
    return credentials
    def upload_video(self, files, title="video", description="description", category="22", keywords="",
                     privacy="public"):
        argparser.add_argument("--file")
        argparser.add_argument("--title")
        argparser.add_argument("--description")
        argparser.add_argument("--category")
        argparser.add_argument("--keywords")
        argparser.add_argument("--privacyStatus", choices=self.VALID_PRIVACY_STATUSES)
        arguments = ["--file=" + files, "--title=" + title, "--description=" + description, "--category=" + category,
                     "--keywords=" + keywords, "--privacyStatus=" + privacy]
        args = argparser.parse_args(arguments)

        if not os.path.exists(args.file):
            exit("Error finding video file exiting...")

        tempyou = self.get_authenticated_service(args)

        videoid = None

        try:
            videoid = self.initialize_upload(tempyou, args)
            print "Done uploading... Success!"
        except HttpError, error:
            print "A connection error (%d) occured: %s " % (error.resp.status, error.content)
            exit("Error uploading.....")
def upload_video(title, video_path, caption_path, keywords,
                 description, thumbnail, public=False):
    if public:
        priv_status = "public"
    else:
        priv_status = "private"

    args = argparser.parse_args([
        "--file", video_path,
        "--caption", caption_path,
        "--title", title,
        "--description", description,
        "--keywords", ",".join(keywords),
        "--thumbnail", thumbnail,
        "--privacyStatus", priv_status
    ])

    if not os.path.exists(args.file):
        exit("Please specify a valid file using the --file= parameter.")

    youtube = get_authenticated_service(args)
    try:
        initialize_upload(youtube, args)
    except HttpError as e:
        print(("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)))
Beispiel #4
0
    def __init__(self, secrets_file="client_secrets.json"):
        self.service = None
        httplib2.RETRIES = 1
        self.MAX_RETRIES = 10
        self.RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, httplib.NotConnected,
                                     httplib.IncompleteRead, httplib.ImproperConnectionState,
                                     httplib.CannotSendRequest, httplib.CannotSendHeader,
                                     httplib.ResponseNotReady, httplib.BadStatusLine)
        self.RETRIABLE_STATUS_CODES = [500, 502, 503, 504]
        self.CLIENT_SECRETS_FILE = secrets_file
        self.scope = "https://www.googleapis.com/auth/youtube"
        self.YOUTUBE_API_SERVICE_NAME = "youtube"
        self.YOUTUBE_API_VERSION = "v3"

        self.MISSING_CLIENT_SECRETS_MESSAGE = """
        WARNING: Please configure OAuth 2.0

        To make this sample run you will need to populate the client_secrets.json file
        found at:

           %s

        with information from the Developers Console
        https://console.developers.google.com/

        For more information about the client_secrets.json file format, please visit:
        https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
        """ % os.path.abspath(os.path.join(os.path.dirname(__file__),
                                           self.CLIENT_SECRETS_FILE))

        self.VALID_PRIVACY_STATUSES = ("public", "private", "unlisted")

        self.__get_authenticated_service(argparser.parse_args([], None))
Beispiel #5
0
 def authentication(self, storage):
     return run_flow(
         OAuth2WebServerFlow(
             client_id=self.google_client_id,
             client_secret=self.google_client_secret,
             scope=['https://www.googleapis.com/auth/gmail.modify']),
         storage, argparser.parse_args([]))
Beispiel #6
0
def authenService():
    CLIENT_SECRETS_FILE = "client_secrets.json"
    #display following message if file not found
    MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0

To make this sample run you will need to populate the client_secrets.json file
found at:

   %s

with information from the Developers Console
https://console.developers.google.com/

For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
                                   CLIENT_SECRETS_FILE))

    YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
    YOUTUBE_API_SERVICE_NAME = "youtube"
    YOUTUBE_API_VERSION = "v3"

    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,message=MISSING_CLIENT_SECRETS_MESSAGE,scope=YOUTUBE_READ_WRITE_SCOPE)

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

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

    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,http=credentials.authorize(httplib2.Http()))
    return youtube
    def ping_youtube(self, *args, **kwargs):
        '''pings youtube for number of subscribers on a given channel ID'''

        #Youtube API V3 authentication
        #External modules and dependencies are required
        try:
            argparser.add_argument("--message", required=False,
                        help="Text of message to post.")
            args = argparser.parse_args()
            youtube = get_authenticated_service(args)
            #headers = ['youtube_id', 'video_id']

        #Get statistics from channel id 
            for i,ID in enumerate(self.data_dict['youtube_channel_id']):
                subs = youtube.channels().list(part="statistics", 
                    id=ID).execute()
                if subs['items']:
                    #print 'No. of subscribers:',subs['items'][0]['statistics']['subscriberCount']
                    self.data_dict['subscribers '+self.stamp].append(subs['items'][0]['statistics']['subscriberCount'])
                else:
                    #print 'No youtube subscribers found'
                    self.data_dict['subscribers '+self.stamp].append('-')

        #Get video views for given id
            for i,ID in enumerate(self.data_dict['video_id']):
                video = youtube.videos().list(part="statistics", 
                    id=ID).execute()
                if video['items']:
                    #print 'No. of video views:',video['items'][0]['statistics']['viewCount']
                    self.data_dict['video views '+self.stamp].append(video['items'][0]['statistics']['viewCount'])
                else:
                    #print 'No youtube video was found' 
                    self.data_dict['video views '+self.stamp].append('-')
        except Exception, error:
            self.log_error('youtube')
def subscriber_views():
  now = datetime.now()
  start = (now - timedelta(days=30)).strftime("%Y-%m-%d")
  end = (now - timedelta(days=1)).strftime("%Y-%m-%d")

  argparser.add_argument("--metrics", help="Report metrics",
    default="views,averageViewDuration,averageViewPercentage")
  argparser.add_argument("--dimensions", help="Report dimensions",
    default="day,subscribedStatus")
  argparser.add_argument("--start-date", default=start,
    help="Start date, in YYYY-MM-DD format")
  argparser.add_argument("--end-date", default=end,
    help="End date, in YYYY-MM-DD format")
  argparser.add_argument("--max-results", help="Max results", default=90)
  argparser.add_argument("--sort", help="Sort order", default="-day")
  args = argparser.parse_args()

  (youtube, youtube_analytics) = get_authenticated_services(args)
  try:
    channel_id = get_channel_id(youtube)
    analytics_results, headers = run_analytics_report(youtube_analytics, channel_id, args)
    df_total_views = pd.DataFrame(analytics_results,columns=["date","subscribedStatus","views","averageViewDuration","averageViewPercentage"])
    # df_total_views.loc[:,"netSubscribers"] = df_total_views["subscribersGained"] - df_total_views["subscribersLost"]
    # df_total_views.loc[:,"totalWatchTime"] = df_total_views["views"]*df_total_views["averageViewDuration"]
    df_total_views.set_index("date",inplace=True)
    # df_total_views.to_csv("Youtube Subscribed vs Not Subscribed.csv")
    return df_total_views
  except HttpError, e:
    print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
    def __enter__(self):
        # Ensure that we have not re-entered
        if self.temp_path != None or self.service != None:
            raise Exception('Cannot use multiple nested with blocks on same Youtube object!')

        flow = flow_from_clientsecrets(
            self.client_secrets_path,
            scope=YOUTUBE_UPLOAD_SCOPE,
            message=MISSING_CLIENT_SECRETS_MESSAGE)

        temp_file = NamedTemporaryFile(delete=False)
        self.temp_path = temp_file.name
        temp_file.close()

        storage = Storage(self.temp_path)
        credentials = storage.get()

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

        self.service = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
            http=credentials.authorize(httplib2.Http()))

        return self
def setup():
	query = raw_input("Enter your query: ")
	argparser.add_argument("--q", help="Search term", default=query) #change the default to the search term you want to search
	argparser.add_argument("--max-results", help="Max results", default=50) #default number of results which are returned. It can vary from 0 - 100
	options = argparser.parse_args()

	page_tokens = {}
	responses = []
	search_response = youtube.search().list( # Call the search.list method to retrieve results matching the specified query term.
		 q=options.q,
		 type="video",
		 part="id,snippet",
		 maxResults=options.max_results
		).execute()	
	responses.append(search_response)

	for i in range(0,4):
		page_tokens['p' + str(i)] = (search_response['nextPageToken'])
		search_response = youtube.search().list( # Call the search.list method to retrieve results matching the specified query term.
		 q=options.q,
		 type="video",
		 part="id,snippet",
		 maxResults=options.max_results,
		 pageToken = page_tokens['p' + str(i)]
		).execute()
		

		responses.append(search_response)

	print json.dumps(responses, indent=4, sort_keys=True)
def main():
  now = datetime.now()
  one_day_ago = (now - timedelta(days=1)).strftime("%Y-%m-%d")
  one_week_ago = (now - timedelta(days=7)).strftime("%Y-%m-%d")

  argparser.add_argument("--channel_id", help="Channel ID",
    required=True)
  argparser.add_argument("--content_owner_id", help="Content Owner Id",
    required=True)
  argparser.add_argument("--metrics", help="Report metrics",
    default="views,comments,favoritesAdded,favoritesRemoved,likes,dislikes,shares")
  argparser.add_argument("--dimensions", help="Report dimensions",
    default="video")
  argparser.add_argument("--start-date", default=one_week_ago,
    help="Start date, in YYYY-MM-DD format")
  argparser.add_argument("--end-date", default=one_day_ago,
    help="End date, in YYYY-MM-DD format")
  argparser.add_argument("--max-results", help="Max results", default=10)
  argparser.add_argument("--sort", help="Sort order", default="-views")
  args = argparser.parse_args()

  youtube_analytics = get_authenticated_services(args)
  try:
    run_analytics_report(youtube_analytics, args)
  except HttpError, e:
    print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
Beispiel #12
0
def ListPlaylist():
  CLIENT_SECRETS_FILE = "../client_secrets.json"
  CLIENT_SECRETS_FILE = os.path.join(os.path.dirname(__file__),
                                   CLIENT_SECRETS_FILE)

  # This variable defines a message to display if the CLIENT_SECRETS_FILE is
  # missing.
  MISSING_CLIENT_SECRETS_MESSAGE = """
  WARNING: Please configure OAuth 2.0

  To make this sample run you will need to populate the client_secrets.json file
  found at:

     %s

  with information from the Developers Console
  https://console.developers.google.com/

  For more information about the client_secrets.json file format, please visit:
  https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
  """ % os.path.abspath(os.path.join(os.path.dirname(__file__),
                                     CLIENT_SECRETS_FILE))

  # This OAuth 2.0 access scope allows for full read/write access to the
  # authenticated user's account.
  YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
  YOUTUBE_API_SERVICE_NAME = "youtube"
  YOUTUBE_API_VERSION = "v3"

  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
    message=MISSING_CLIENT_SECRETS_MESSAGE,
    scope=YOUTUBE_READ_WRITE_SCOPE,
    redirect_uri="http://youtube.com",)


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

  if credentials is None or credentials.invalid:
    ##flags = argparser.parse_args()
    flags = argparser.parse_args(args=[])
    credentials = run_flow(flow, storage, flags)

  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    http=credentials.authorize(httplib2.Http()))

  # This code creates a new, private playlist in the authorized user's channel.
  list_playlists_response = youtube.playlists().list(
    part="id, snippet",
    mine=True,
    maxResults=50
  ).execute()

  data = []

  for item in list_playlists_response.get("items", []):
    print "title: %s (id: %s)" % (item["snippet"]["title"], item["id"])
    data.append([item["snippet"]["title"].encode(), item["id"].encode()])

  return data
Beispiel #13
0
def setup():
    '''Attempt to setup a nice youtube objeect'''
    # do not actually parse arguments
    args = argparser.parse_args(args=[])
    args.noauth_local_webserver=True
    youtube = get_authenticated_service(args)
    return youtube
Beispiel #14
0
    def retrieve_credentials(self):
        storage = Storage(self.storage_path)

        credentials = storage.get()
        if credentials is None or credentials.invalid:
            credentials = run_flow(self.create_oauth_flow(), storage, argparser.parse_args([]), http=self.http)

        return credentials
Beispiel #15
0
def setup_args():
    argparser.add_argument("--videoid", help="Required; ID for video.")
    argparser.add_argument("--debug", help="Prints all video comments read.", action="store_true")
    argparser.add_argument("--showreason", help="Prints comment that classifies video as weird.", action="store_true")
    args = argparser.parse_args()
    if not args.videoid:
        exit("Please specify videoid using the --videoid= parameter.")
    return args
Beispiel #16
0
def main():
  client_id = sys.argv[1]
  client_secret = sys.argv[2]
  scope = sys.argv[3]
  flow = OAuth2WebServerFlow(client_id, client_secret, scope)
  storage = Storage('google.json')
  flags = argparser.parse_args([])
  run_flow(flow, storage, flags)
 def render(self, search_args):
     argparser.add_argument("--q", help="Search term", default=search_args)
     argparser.add_argument("--max-results", help="Max results", default=25)
     argparser.add_argument("--type", help="Type of result", default="video")
     args = argparser.parse_args()
     try:
         self.__youtube_search(args)
     except HttpError as e:
         print ("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))
  def search_by_word(self, search_term):
    args = argparser.parse_args()
    args.q = search_term

    try:
      return self.youtube_search(args)
    except HttpError as e:
      print("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))
      return ''
Beispiel #19
0
def add_song(playlistId, videoId):
  CLIENT_SECRETS_FILE = "../client_secrets.json"
  CLIENT_SECRETS_FILE = os.path.join(os.path.dirname(__file__),
                                   CLIENT_SECRETS_FILE)

  # This variable defines a message to display if the CLIENT_SECRETS_FILE is
  # missing.
  MISSING_CLIENT_SECRETS_MESSAGE = """
  WARNING: Please configure OAuth 2.0

  To make this sample run you will need to populate the client_secrets.json file
  found at:

     %s

  with information from the Developers Console
  https://console.developers.google.com/

  For more information about the client_secrets.json file format, please visit:
  https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
  """ % os.path.abspath(os.path.join(os.path.dirname(__file__),
                                     CLIENT_SECRETS_FILE))

  # This OAuth 2.0 access scope allows for full read/write access to the
  # authenticated user's account.
  YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
  YOUTUBE_API_SERVICE_NAME = "youtube"
  YOUTUBE_API_VERSION = "v3"

  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
    message=MISSING_CLIENT_SECRETS_MESSAGE,
    scope=YOUTUBE_READ_WRITE_SCOPE,
    redirect_uri="http://youtube.com",)


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

  if credentials is None or credentials.invalid:
    ##flags = argparser.parse_args()
    flags = argparser.parse_args(args=[])
    credentials = run_flow(flow, storage, flags)

  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    http=credentials.authorize(httplib2.Http()))

  playlistitems_list_request = youtube.playlistItems().insert(
    part="snippet",
    body={
      "snippet": {
        "playlistId":playlistId,
        "resourceId": {"kind":"youtube#video", "videoId":videoId}
      }
    }
  ).execute()

  return "okkkkkkkkkkkkkkkkkkkkkk"
def getAllVideosFromChannel():
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
    message=MISSING_CLIENT_SECRETS_MESSAGE,
    scope=YOUTUBE_READONLY_SCOPE)

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

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

  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    http=credentials.authorize(httplib2.Http()))

  # Retrieve the contentDetails part of the channel resource for the
  # authenticated user's channel.
  channels_response = youtube.channels().list(
    mine=True,
    part="contentDetails"
  ).execute()

  for channel in channels_response["items"]:
    # From the API response, extract the playlist ID that identifies the list
    # of videos uploaded to the authenticated user's channel.
    uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"]

    if __debug__:
      print "Videos in list %s" % uploads_list_id

    # Retrieve the list of videos uploaded to the authenticated user's channel.
    playlistitems_list_request = youtube.playlistItems().list(
      playlistId=uploads_list_id,
      part="snippet",
      maxResults=50
    )
    
    result = dict()

    while playlistitems_list_request:
      playlistitems_list_response = playlistitems_list_request.execute()

      # Print information about each video.
      for playlist_item in playlistitems_list_response["items"]:
        title = playlist_item["snippet"]["title"]
        video_id = playlist_item["snippet"]["resourceId"]["videoId"]
        if __debug__:
          print "%s (%s)" % (title, video_id)
        result[title] = video_id

      playlistitems_list_request = youtube.playlistItems().list_next(
        playlistitems_list_request, playlistitems_list_response)

    if __debug__:
      print
    return result
def get_gdata_credentials(secrets, creds, scope, force=False):
    storage = Storage(creds)
    credentials = storage.get()
    if credentials is None or credentials.invalid or force:
      credentials = run_flow(flow_from_clientsecrets(secrets, scope=scope), storage, argparser.parse_args([]))
      
    if credentials.access_token_expired:
        credentials.refresh(httplib2.Http())
        
    return credentials
Beispiel #22
0
def scrapingInYoutube(text):
       
    #sudo pip install --upgrade google-api-python-client
    

    # Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
    # tab of
    #   https://cloud.google.com/console
    # Please ensure that you have enabled the YouTube Data API for your project.
    DEVELOPER_KEY = "AIzaSyAcmrNRK5GBEmlUWTFidyqj08m3572jUG8"
    YOUTUBE_API_SERVICE_NAME = "youtube"
    YOUTUBE_API_VERSION = "v3"

    def youtube_search(options, keyword):
        youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
            developerKey=DEVELOPER_KEY)

        # Call the search.list method to retrieve results matching the specified
        # query term.
        options.q=keyword
        search_response = youtube.search().list(
            q=options.q,
            part="id,snippet",
            maxResults=options.max_results
        ).execute()

        videos = []
        channels = []
        playlists = []

        # Add each result to the appropriate list, and then display the lists of
        # matching videos, channels, and playlists.
        
        for search_result in search_response.get("items", []):
            if search_result["id"]["kind"] == "youtube#video":
                videos.append('\"%s\" (%s)' % ((search_result["snippet"]["title"]).decode('utf-8'),
                                            search_result["id"]["videoId"]))
            

        #videos = unicode(videos)
        #videos = videos.split('\n')

        #print "Videos:\n", "\n".join(videos), "\n"

        # Edit tracklist in JSON
        # {name: [name], id: [id]}
        tracklist = videos

        return tracklist

    #argparser.add_argument("--q", help="Search term", default="Google")
    #argparser.add_argument("--max-results", help="Max results", default=25)
    args = argparser.parse_args()
    print args
    return youtube_search(args, text)
Beispiel #23
0
def get_authenticated_service():
    upload_scope = 'https://www.googleapis.com/auth/youtube.upload'
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=upload_scope)
    storage = Storage("%s-oauth2.json" % sys.argv[0])
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        flags = argparser.parse_args(args=['--noauth_local_webserver'])
        credentials = run_flow(flow, storage, flags)

    return build('youtube', 'v3', http=credentials.authorize(httplib2.Http()))
Beispiel #24
0
def build_youtube():
    flow = flow_from_clientsecrets(
        CLIENT_SECRETS_FILE, message=MISSING_CLIENT_SECRETS_MESSAGE, scope=YOUTUBE_READ_WRITE_SCOPE
    )
    storage = Storage("%s-oauth2.json" % sys.argv[0])
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        flags = argparser.parse_args()
        credentials = run_flow(flow, storage, flags)
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=credentials.authorize(httplib2.Http()))
    return youtube
def google_auth(client_id, client_secret, scope):
    flow = OAuth2WebServerFlow(client_id=client_id,
                           client_secret=client_secret,
                           scope=scope,
                           redirect_uri='http://localhost')
    storage = Storage('creds.data')
    flags = argparser.parse_args(args=[])
    credentials = run_flow(flow, storage, flags)
    access_token = credentials.access_token
    os.remove('creds.data')
    return access_token
Beispiel #26
0
def authentication():



    # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
    # the OAuth 2.0 information for this application, including its client_id and
    # client_secret. You can acquire an OAuth 2.0 client ID and client secret from
    # the Google Developers Console at
    # https://console.developers.google.com/.
    # Please ensure that you have enabled the YouTube Data API for your project.
    # For more information about using OAuth2 to access the YouTube Data API, see:
    #   https://developers.google.com/youtube/v3/guides/authentication
    # For more information about the client_secrets.json file format, see:
    #   https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
    CLIENT_SECRETS_FILE = "client_secrets.json"

    # This variable defines a message to display if the CLIENT_SECRETS_FILE is
    # missing.
    MISSING_CLIENT_SECRETS_MESSAGE = """
    WARNING: Please configure OAuth 2.0

    To make this sample run you will need to populate the client_secrets.json file
    found at:

       %s

    with information from the Developers Console
    https://console.developers.google.com/

    For more information about the client_secrets.json file format, please visit:
    https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
    """ % os.path.abspath(os.path.join(os.path.dirname(__file__),
                                       CLIENT_SECRETS_FILE))

    # This OAuth 2.0 access scope allows for full read/write access to the
    # authenticated user's account.
    YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
    YOUTUBE_API_SERVICE_NAME = "youtube"
    YOUTUBE_API_VERSION = "v3"

    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
      message=MISSING_CLIENT_SECRETS_MESSAGE,
      scope=YOUTUBE_READ_WRITE_SCOPE)

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

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

    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
      http=credentials.authorize(httplib2.Http()))
    return youtube;
Beispiel #27
0
def upload(file, title, desc):
    args = argparser.parse_args()
    args.file = file
    args.title = title
    args.description = desc
    
    youtube = get_authenticated_service(args)
    try:
        return initialize_upload(youtube, args)
    except HttpError as e:
        print("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))
Beispiel #28
0
def _get_youtube_service():
    flow = flow_from_clientsecrets(settings.GOOGLE_API_CREDENTIALS, scope='https://www.googleapis.com/auth/youtube')

    storage = Storage(os.path.join(settings.DATA_ROOT, 'google-credentials-oauth.json'))
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        args = sys.argv[2:]
        args.append('--noauth_local_webserver')
        credentials = run_flow(flow, storage, argparser.parse_args(args))

    return build('youtube', 'v3', http=credentials.authorize(httplib2.Http()))
Beispiel #29
0
def get_authenticated_service(args):
    log.info("youtube.get_authenticated_service")
    with tempfile.NamedTemporaryFile() as temp:
        temp.write(client_secrets.encode('utf-8'))
        temp.flush()    
        flow = flow_from_clientsecrets(temp.name, scope=YOUTUBE_UPLOAD_SCOPE, message="missing client_secrets.json")
    storage = Storage("%s-oauth2.json" % sys.argv[0])
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        credentials = run_flow(flow, storage, argparser.parse_args())
    auth = credentials.authorize(httplib2.Http())
    return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=auth)
Beispiel #30
0
  def get_authenticated_service(init=False):
    flow = flow_from_clientsecrets("application_secrets.json",
      scope=YOUTUBE_READ_WRITE_SCOPE,
      message=MISSING_CLIENT_SECRETS_MESSAGE)

    storage = Storage("user_oauth2.json")
    credentials = storage.get()
    if init and (credentials is None or credentials.invalid):
      args = argparser.parse_args(["--noauth_local_webserver"])
      credentials = run_flow(flow, storage, args)

    return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
      http=credentials.authorize(httplib2.Http()))
Beispiel #31
0
YOUTUBE_API_VERSION = "v3"

u1 = ""
u2 = ""
u3 = ""
u4 = ""
u5 = ""

argparser.add_argument("--q",
                       help="Search term",
                       default=random.choice(newlist))
youtube = build(YOUTUBE_API_SERVICE_NAME,
                YOUTUBE_API_VERSION,
                developerKey=DEVELOPER_KEY)
argparser.add_argument("--max-results", help="Max results", default=5)
options = argparser.parse_args()

search_response = youtube.search().list(
    q=options.q, part="id,snippet", maxResults=options.max_results).execute()

videos = []
channels = []
playlists = []

for search_result in search_response.get("items", []):
    if search_result["id"]["kind"] == "youtube#video":
        videos.append("%s " % (search_result["id"]["videoId"]))

u1 = videos[0]
u2 = videos[1]
u3 = videos[2]
Beispiel #32
0
# the Google APIs Console
FLOW = OAuth2WebServerFlow(client_id=client_id,
                           client_secret=client_secret,
                           scope=scope,
                           user_agent=user_agent)

# To disable the local server feature, uncomment the following line:
# FLAGS.auth_local_webserver = False

# If the Credentials don't exist or are invalid, run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
    credentials = run_flow(FLOW, storage, argparser.parse_args([]))

# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)

# Build a service object for interacting with the API. Visit
# the Google APIs Console
# to get a developerKey for your own application.
service = build(serviceName='calendar',
                version='v3',
                http=http,
                developerKey=developerKey)

Beispiel #33
0
    def add_to_playlist():
        # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
        # the OAuth 2.0 information for this application, including its client_id and
        # client_secret. You can acquire an OAuth 2.0 client ID and client secret from
        # the Google Developers Console at
        # https://console.developers.google.com/.
        # Please ensure that you have enabled the YouTube Data API for your project.
        # For more information about using OAuth2 to access the YouTube Data API, see:
        #   https://developers.google.com/youtube/v3/guides/authentication
        # For more information about the client_secrets.json file format, see:
        #   https://developers.google.com/api-client-library/python/guide/aaa_client_secrets

        CLIENT_SECRETS_FILE = "client_secret_903221304600-cqh20m4v1fitiu8u9okgrh2k27t0is67.apps.googleusercontent.com.json"

        # This variable defines a message to display if the CLIENT_SECRETS_FILE is
        # missing.
        MISSING_CLIENT_SECRETS_MESSAGE = """
        WARNING: Please configure OAuth 2.0
        
        To make this sample run you will need to populate the client_secrets.json file
        found at:
        
           %s
        
        with information from the Developers Console
        https://console.developers.google.com/
        
        For more information about the client_secrets.json file format, please visit:
        https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
        """ % os.path.abspath(os.path.join(os.path.dirname(__file__),
                                           CLIENT_SECRETS_FILE))

        # This OAuth 2.0 access scope allows for full read/write access to the
        # authenticated user's account.
        YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
        YOUTUBE_API_SERVICE_NAME = "youtube"
        YOUTUBE_API_VERSION = "v3"
        flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
                                       message=MISSING_CLIENT_SECRETS_MESSAGE,
                                       scope=YOUTUBE_READ_WRITE_SCOPE)

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

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

        youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
                        http=credentials.authorize(httplib2.Http()))

        add_video_request = youtube.playlistItems().insert(
            part="snippet",
            body={
                'snippet': {
                    'playlistId': globals.YOUTUBE_PLAYLIST,
                    'resourceId': {
                        'kind': 'youtube#video',
                        'videoId': video_id[0].strip("()")
                    }
                    #'position': 0
                }
            }
        ).execute()
Beispiel #34
0
def search_args_parse():
    argparser.add_argument("--q", help="Search term", default="Google")
    argparser.add_argument("--max-results", help="Max results", default=25)
    args = argparser.parse_args()

    return args
Beispiel #35
0
def gather(search_query):
    argparser.add_argument("--q", help="Search term", default=search_query)
    argparser.add_argument("--max-results", help="Max results", default=50)
    arguments = argparser.parse_args()
    options = arguments

    youtube = build(API_NAME, API_VERSION, developerKey=API_KEY)

    search_response = youtube.search().list(
        q=options.q,
        type="video",
        part="id,snippet",
        maxResults=options.max_results).execute()

    videos = {}

    for search_result in search_response.get("items", []):
        if search_result["id"]["kind"] == "youtube#video":
            videos[search_result["id"]
                   ["videoId"]] = search_result["snippet"]["title"]

    s = ','.join(videos.keys())

    videos_list_response = youtube.videos().list(
        id=s, part='id,statistics,snippet,status,contentDetails').execute()

    res = []
    for i in videos_list_response['items']:
        temp_res = dict(v_id=i['id'], v_title=videos[i['id']])
        temp_res.update(i['statistics'])
        temp_res.update(i['status'])
        temp_res.update(i['contentDetails'])
        temp_res.update(i['snippet'])
        channel_details = cd.get_all_data(temp_res['channelId'])
        res.append(temp_res)
        temp_res.update(channel_details)

    dataframe = pd.DataFrame.from_dict(res)
    # drop useless columns from dataframe
    try:
        dataframe.drop([
            'license', 'licensedContent', 'dimension', 'thumbnails', 'v_title',
            'uploadStatus', 'defaultLanguage'
        ],
                       axis=1,
                       inplace=True)
    except ValueError:
        dataframe.drop([
            'license', 'licensedContent', 'dimension', 'thumbnails', 'v_title',
            'uploadStatus'
        ],
                       axis=1,
                       inplace=True)
    dataframe.drop(['caption', 'embeddable'], axis=1, inplace=True)
    try:
        dataframe.drop(['contentRating'], axis=1, inplace=True)
    except ValueError:
        pass
    try:
        dataframe.drop(['defaultAudioLanguage'], axis=1, inplace=True)
    except ValueError:
        pass
    try:
        dataframe.drop(['regionRestriction'], axis=1, inplace=True)
    except ValueError:
        pass
    names = dataframe.columns.values
    f = open("data.csv", "a")
    dataframe.to_csv(path_or_buf=f, encoding='utf-8')
    f.close()
Beispiel #36
0
            except Exception, e:
                print str(e)
                pass

        print "Videos:\n", "\n".join(videos), "\n"
        print "Channels:\n", "\n".join(channels), "\n"
        print "Playlists:\n", "\n".join(playlists), "\n"

        s = search_response.get('nextPageToken', None)


if __name__ == "__main__":
    f = open('colors.txt', 'r')
    h = open('errors.txt', 'w')
    primary_colors = []

    for i in f:
        primary_colors.append(i.strip())

    argparser.add_argument("--q", help="Search term", default='Color')
    argparser.add_argument("--max-results", help="Max results", default=25)

    for i in primary_colors:
        args = argparser.parse_args(("--q %s" % (i)).split())
        print args

        try:
            youtube_search(args)
        except HttpError, e:
            print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
Beispiel #37
0
def login():
    global youtube
    args = argparser.parse_args()
    youtube = get_authenticated_service(
        args)  #authenticates the api and saves it to youtube
    getLiveId(youtube)