Example #1
0
    def create_playlist(self, items):
        playlist_body = {
            "snippet": {
                "title": "Lively autogenerated playlist - {}".format(datetime.now().strftime("%Y-%m-%d")),
                "description": "This is an autogenerated playlist by Lively. github.com/cxmplex/Lively",
                "status": {
                    "privacyStatus": "private"
                }
            }
        }
        flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(SECRETS[0], scopes)
        credentials = flow.run_console()
        try:
            self.yt = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, credentials=credentials)
            request = self.yt.playlists().insert(
                part="snippet,status",
                body=playlist_body
            )
            response = request.execute()
        except Exception as e:
            raise YoutubeAPIError(str(e))

        playlist_id = response['id']
        if not playlist_id:
            raise YoutubeAPIError('Failed to create playlist')

        for item in items:
            body = {
                "snippet": {
                    "playlistId": playlist_id,
                    "position": 0,
                    "resourceId": {
                        "kind": "youtube#video",
                        "videoId": item
                    }
                }
            }
            should_break = False
            while not should_break:
                try:
                    request = self.yt.playlistItems().insert(
                        part="snippet",
                        body=body
                    )
                    response = request.execute()
                    if not response:
                        raise YoutubeAPIError("Invalid PlaylistItems addition.")
                    should_break = True
                except Exception:
                    secret = SECRETS[0]
                    SECRETS.remove(secret)
                    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(SECRETS[0], scopes)
                    credentials = flow.run_console()
                    self.yt = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, credentials=credentials)
        return True
Example #2
0
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    playlists = {}

    # Getting youtube playlist
    request = youtube.playlists().list(
        part="snippet,contentDetails",
        maxResults=25,
        mine=True
    )
    response = request.execute()

    print(response)
Example #3
0
def get_authenticated_service():
    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = build(api_service_name, api_version, credentials=credentials)
    return youtube
Example #4
0
def api_response():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = r'C:\Users\edams\AppData\Roaming\Code\User\VSC_Workspace\.vscode\python\.vscode\client_secret_440876599079-lotmaj2kiqssjodtf8k31f98uee6gl7f.apps.googleusercontent.com.json'

    #USING TEXT FILE TO RETREIVE CREDENTIALS
    if os.path.isfile("credentials.json"):
        with open("credentials.json", "r") as f:
            creds_data = json.load(f)
            client_id = creds_data['client_id']
            client_secret = creds_data['client_secret']
            refresh_token = creds_data['refresh_token']
        access_token = refreshToken(client_id, client_secret, refresh_token)
        creds = Credentials(access_token)
    else:
        # Get credentials and create an API client
        flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
            client_secrets_file, scopes)
        creds = flow.run_console()
        creds_data = {
            'token': creds.token,
            'refresh_token': creds.refresh_token,
            'token_uri': creds.token_uri,
            'client_id': creds.client_id,
            'client_secret': creds.client_secret,
            'scopes': creds.scopes
        }
        with open("credentials.json",
                  'w') as outfile:  #WRITING CREDENTIALS TO FILE
            json.dump(creds_data, outfile)
    return build(api_service_name, api_version, credentials=creds)
Example #5
0
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = '../.client_secret.json'

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(api_service_name,
                                              api_version,
                                              credentials=credentials)

    request = youtube.playlistItems().insert(
        part="\'snippet,status\'",
        body={
            "snippet": {
                "playlistId": "PL3AgsfiW8ntWXfIKnARk2jyhP1xDM4-oL",
                "resourceId": {
                    "videoId": "7lECIsRif10"
                }
            }
        })
    response = request.execute()

    print(response)
Example #6
0
    def get_youtube_client(self):
        # Disable OAuthlib's HTTPS verification when running locally.
        # *DO NOT* leave this option enabled in production.
        os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

        api_service_name = "youtube"
        api_version = "v3"
        client_secrets_file = "client_secret.json"

        # Get credentials and create an API client
        scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
        flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
                client_secrets_file, 
                scopes
            )
        credentials = flow.run_console()

        # from the Youtube DATA API
        youtube_client = googleapiclient.discovery.build(
                api_service_name, 
                api_version, 
                credentials = credentials
            )

        return youtube_client
Example #7
0
def get_authenticated_service():
    """
    function get token authenticate for using api
    """
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
    credentials = flow.run_console()
    return credentials.token
def get_authenticated_service():
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE,
                                                     SCOPES)
    credentials = flow.run_console()

    api_service = build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
    return api_service
Example #9
0
 def youtube_authenticated_service(self):
     flow = InstalledAppFlow.from_client_secrets_file(self.client_secrets_file, SCOPES)
     storage = Storage(self.client_oauth2_file)
     credentials = storage.get()
     if credentials is None or credentials.invalid:
         credentials = flow.run_console()
     self.youtube = build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "CLIENT_SECRET.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.thumbnails().set(
        videoId=input('Please enter your VideoID: '),

        media_body=MediaFileUpload(
            input('PLease enter image you want to upload: '))
    )
    response = request.execute()

    print(response)
 def youtube_authenticated_service(self):
     flow = InstalledAppFlow.from_client_secrets_file(self.client_secrets_file, SCOPES)
     storage = Storage(self.client_oauth2_file)
     credentials = storage.get()
     if credentials is None or credentials.invalid:
         credentials = flow.run_console()
     self.youtube = build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
def main():
    #setup
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "secret1.json"
    
    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    # Request (This is what asks Youtube API for the comment data)
    request = youtube.commentThreads().list(
        part="snippet",
        videoId="X4xtZv5nFIk"
    )
    response = request.execute();

    comment_data = response["items"][0];
    comment_snippet = comment_data["snippet"]["topLevelComment"]["snippet"];

    comment_text = comment_snippet["textOriginal"];
    comment_username = comment_snippet["authorDisplayName"];

    print(comment_text);
    print(comment_username);

    inappro = predict([comment_text]);
    if inappro:
        continue; # Don't update title if comment is inappropriate 
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(api_service_name,
                                              api_version,
                                              credentials=credentials)

    request = youtube.playlists().update(
        part="id, snippet",
        body={
            "id": "PLM5ZrANa_78hVN5Nzr5QfKOtK6FJO5vKk",
            "snippet": {
                "title": "uWin Discord CS music",
                "description": "Good morning gamers"
            }
        })
    response = request.execute()

    print(response)
    playlist = "playlist.json"
    file_in = open(playlist, "w+")
    file_in.write(json.dumps(response, indent=4, separators=(',', ': ')))
    file_in.close()
    print("Playlist successfully created. Data is in the file: " + playlist)
Example #14
0
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret.json"
    # query_value = input("Search:")

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(api_service_name,
                                              api_version,
                                              credentials=credentials)

    request = youtube.search().list(part="snippet",
                                    q="hey man he was in my face!")
    response = request.execute()

    #json_values = json.dumps(response)

    #items = response["items"]
    #ids = items[2]
    print(type(response))
Example #15
0
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret_390500009550-dk0q8j8g516lo99anp3ngsrf2u6sha2d.apps.googleusercontent.com.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(api_service_name,
                                              api_version,
                                              credentials=credentials)

    request = youtube.captions().download(
        id="8yMV7mc691bRtJqxYhDZfwpf4t7J0c3U")
    # TODO: For this request to work, you must replace "YOUR_FILE"
    #       with the location where the downloaded content should be written.
    fh = io.FileIO("YOUR_FILE", "wb")

    download = MediaIoBaseDownload(fh, request)
    complete = False
    while not complete:
        status, complete = download.next_chunk()
Example #16
0
def youtube_credentials():
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
    scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.playlists().insert(
        part="snippet",
        body={
            "snippet": {
                "title": "test1"
            }
        }
    )
    request.execute()
    #print(response)
    return youtube
Example #17
0
def main():
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret_839737400366-16nm7ouve15fg7usoccjtoamovd5vjm7.apps.googleusercontent.com.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.channels().list(
        part="snippet,contentDetails,statistics",
        id="UCLGe0PxyRFWmXVGJKq_gGvw"
    )

    requestPlayList = youtube.playlistItems().list(
        part="snippet,contentDetails",
        maxResults=50,
        playlistId="UULGe0PxyRFWmXVGJKq_gGvw"

    )
    global response
    response = request.execute()
    print(response)

    global responsePlaylist
    responsePlaylist = requestPlayList.execute()
    print(responsePlaylist)
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret_743607985340-lpvgb4s2gdef8oklrbn5alvv9pim40dq.apps.googleusercontent.com.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(api_service_name,
                                              api_version,
                                              credentials=credentials)
    for i in range(0, len(title)):
        request = youtube.search().list(part="snippet",
                                        publishedAfter=startdate[i],
                                        publishedBefore=enddate[i],
                                        q=title[i],
                                        type="video",
                                        videoCategoryId="20")
        response = request.execute()
        print(response['pageInfo'])
Example #19
0
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.playlistItems().list(
        part="snippet,contentDetails",
        maxResults=50,
        playlistId="UUzpCDQHf_Usz4rMZIZwwDxA",
        access_token="4/1AY0e-g41qZsGJAGEPCv2VT7kqoxZ7FJXOtVAaKjbp3uPXVLvWjm7og8hbiQ",
        prettyPrint=True
    )

    response = request.execute()

    print("-"*16)
    print(response['items'])
    print("-"*16)
Example #20
0
    def __init__(self, url):
        self.url = url
        self.live_id = self.get_live_id(self.url)

        scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
        os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

        api_service_name = "youtube"
        api_version = "v3"
        client_secrets_file = "CLIENT_SECRET_FILE.json"

        flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
            client_secrets_file, scopes)
        credentials = flow.run_console()
        self.youtube = googleapiclient.discovery.build(
            api_service_name, api_version, credentials=credentials)

        request = self.youtube.liveBroadcasts().list(
            part="snippet",
            id=self.live_id
        )
        response = request.execute()

        item = response['items']
        snippet = item[0]['snippet']
        self.live_chat_id = snippet['liveChatId']

        request = self.youtube.liveChatMessages().list(
            liveChatId=self.live_chat_id,
            part="snippet"
        )
        response = request.execute()
        self.next_page_tok = response['nextPageToken']
Example #21
0
def get_result_duration(videos):
    '''
    Helper method to query the YouTube API for returned video id's in one call.

    Parameters:
    videos - list of video id's to find duration of
    '''
    # parse list of videos into a comma seperated string
    video_ids = ','.join(map(str, videos))

    # setup an API client
    scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        CLIENT_SECRET, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(YT_API_SERVICE,
                                              YT_API_VERSION,
                                              credentials=credentials)

    # make query for contentDetails
    request = youtube.videos().list(part="contentDetails", id=video_ids)
    response = request.execute()

    # lots more todo here
    return response
Example #22
0
    def get_youtube_client(self):
        #Log Into Youtube, taken from Youtube Data API

        # Disable OAuthlib's HTTPS verification when running locally.
        # *DO NOT* leave this option enabled in production.
        # Oauth2 works through SSL layer. If your server is not parametrized to allow HTTPS, the fetch_token method
        # will raise an oauthlib.oauth2.rfc6749.errors.InsecureTransportError.
        # You can disable this check by the follwing:
        os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

        api_service_name = "youtube"
        api_version = "v3"
        client_secrets_file = "client_secret.json"

        # Get credentials and create an API client
        scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
        flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
            client_secrets_file, scopes)
        credentials = flow.run_console()

        # from the Youtube DATA API
        youtube_client = googleapiclient.discovery.build(
            api_service_name, api_version, credentials=credentials)

        return youtube_client
Example #23
0
    def get_youtube_client(self):
        """ Log Into Youtube, Copied from Youtube Data API """
        # Disable OAuthlib's HTTPS verification when running locally.
        # *DO NOT* leave this option enabled in production.
        os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

        api_service_name = "youtube"
        api_version = "v3"
        client_secrets_file = "C:/Users/Farhan Khot/youtube_spotify/youtube_spotify/client_secret.json"
        scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

        # This change from original code makes it so that I dont have to authenticate each time with google
        if os.path.exists("CREDENTIALS_PICKLE_FILE"):
            with open("CREDENTIALS_PICKLE_FILE", 'rb') as f:
                credentials = pickle.load(f)
        else:
            flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes)
            credentials = flow.run_console()
            with open("CREDENTIALS_PICKLE_FILE", 'wb') as f:
                pickle.dump(credentials, f)

        # Get credentials and create an API client
        # flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        #     client_secrets_file, scopes)
        # credentials = flow.run_console()

        # from the Youtube DATA API
        youtube_client = googleapiclient.discovery.build(
            api_service_name, api_version, credentials=credentials)

        return youtube_client
Example #24
0
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(api_service_name,
                                              api_version,
                                              credentials=credentials)

    #Get subscriptions list
    request = youtube.subscriptions().list(part="snippet,contentDetails",
                                           maxResults=50,
                                           mine=True)

    subs = []

    #get subs list
    data = request.execute()
    items = data.get('items')
    for i in items:
        subs.append(i.get('id'))

    #delete sub
    for channel in subs:
        request = youtube.subscriptions().delete(id=channel)
        request.execute()
Example #25
0
    def get_youtube_client(self):
        """
        #THIS PART IS ALL TAKEN FROM GOOGLE API DOCS
        #https://developers.google.com/youtube/v3/docs/playlists/list?apix=true#request-body
        # -*- coding: utf-8 -*-

        # Sample Python code for youtube.playlists.list
        # See instructions for running these code samples locally:
        # https://developers.google.com/explorer-help/guides/code_samples#python

        """
        scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
        # Disable OAuthlib's HTTPS verification when running locally.
        # *DO NOT* leave this option enabled in production.
        os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

        api_service_name = "youtube"
        api_version = "v3"
        client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

        # Get credentials and create an API client
        flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
            client_secrets_file, scopes)
        credentials = flow.run_console()
        youtube = googleapiclient.discovery.build(api_service_name,
                                                  api_version,
                                                  credentials=credentials)

        return youtube
Example #26
0
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(api_service_name,
                                              api_version,
                                              credentials=credentials)

    request = youtube.videos().insert(
        body={},

        # TODO: For this request to work, you must replace "YOUR_FILE"
        #       with a pointer to the actual file you are uploading.
        media_body=MediaFileUpload("YOUR_FILE"))
    response = request.execute()

    print(response)
Example #27
0
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(api_service_name,
                                              api_version,
                                              credentials=credentials)

    request = youtube.playlistItems().list(
        part="snippet", playlistId="PLM5ZrANa_78hVN5Nzr5QfKOtK6FJO5vKk")
    response = request.execute()

    file_out = open("playlist_data.json", "w")
    file_out.write(
        json.dumps(response, sort_keys=True, separators=(',', ': '), indent=4))
    file_out.close()
Example #28
0
def youtube_con_viewers():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "0"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_id.json"
    if os.path.exists(CREDENTIALS_PICKLE_FILE):
        with open(CREDENTIALS_PICKLE_FILE, 'rb') as f:
            credentials = pickle.load(f)
    else:
        flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
        credentials = flow.run_console()
        with open(CREDENTIALS_PICKLE_FILE, 'wb') as f:
            pickle.dump(credentials, f)
    youtube = build(API_SERVICE_NAME, API_VERSION, credentials = credentials, cache_discovery=False)

    request = youtube.liveBroadcasts().list(
        part="snippet,contentDetails,status",
        broadcastStatus="active",
        broadcastType="all"
    )
    response = request.execute()

    video_id = response["items"][0]["id"]

    request = youtube.videos().list(
        part="snippet,liveStreamingDetails,status", id=video_id
    )

    response = request.execute()

    return(response["items"][0]["liveStreamingDetails"]["concurrentViewers"])
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "MY_CLIENT_SECRESTS.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(api_service_name,
                                              api_version,
                                              credentials=credentials)

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

    print(response)
Example #30
0
def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret_886166412429-7i12bii7g8198ef8sk0a19gj7chgpucl.apps.googleusercontent.com.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(api_service_name,
                                              api_version,
                                              credentials=credentials)

    flow.redirect_uri = 'https://127.0.0.1:8000'

    request = youtube.search().list(part="snippet",
                                    maxResults=50,
                                    order="date",
                                    q="surfing",
                                    type="video")
    response = request.execute()

    print(response)
Example #31
0
def changeVideoTitle(id):
    title = "This my 2019 memories picture " + " "
    desc = "This video is about how awesome APIs are. "
    CLIENT_SECRET_FILE = 'client_secret.json'
    SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE,
                                                     SCOPES)
    credentials = flow.run_console()
    youtube = build('youtube', 'v3', credentials=credentials)

    request = youtube.videos().update(
        part="snippet",  #,status
        body={
            "id": id,
            "snippet": {
                "categoryId":
                27,
                # "defaultLanguage": "en",
                "description":
                desc,
                "tags": [
                    "kagaya john", "tom scott", "tomscott", "api", "coding",
                    "application programming interface", "data api"
                ],
                "title":
                title
            },
        })
    response = request.execute()
    print(response)
def _credentials_flow_interactive(client_secrets_path):
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_path,
        scopes=[_ASSISTANT_OAUTH_SCOPE])
    if 'DISPLAY' in os.environ:
        credentials = flow.run_local_server()
    else:
        credentials = flow.run_console()
    return credentials
Example #33
0
def _credentials_flow_interactive(client_secrets_path):
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_path,
        scopes=[_ASSISTANT_OAUTH_SCOPE])
    if 'DISPLAY' in os.environ:
        # Use chromium-browser by default. Raspbian Stretch uses Epiphany by
        # default but that seems to cause issues:
        # https://github.com/google/aiyprojects-raspbian/issues/269
        webbrowser.register('chromium-browser', None, webbrowser.Chrome('chromium-browser'), -1)
        credentials = flow.run_local_server()
    else:
        credentials = flow.run_console()
    return credentials
Example #34
0
def main(client_secrets, scope, save, credentials, headless):
    """Command-line tool for obtaining authorization and credentials from a user.

    This tool uses the OAuth 2.0 Authorization Code grant as described
    in section 1.3.1 of RFC6749:
    https://tools.ietf.org/html/rfc6749#section-1.3.1

    This tool is intended for assist developers in obtaining credentials
    for testing applications where it may not be possible or easy to run a
    complete OAuth 2.0 authorization flow, especially in the case of code
    samples or embedded devices without input / display capabilities.

    This is not intended for production use where a combination of
    companion and on-device applications should complete the OAuth 2.0
    authorization flow to get authorization from the users.

    """

    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets,
        scopes=scope
    )

    if not headless:
        creds = flow.run_local_server()
    else:
        creds = flow.run_console()

    creds_data = {
        'token': creds.token,
        'refresh_token': creds.refresh_token,
        'token_uri': creds.token_uri,
        'client_id': creds.client_id,
        'client_secret': creds.client_secret,
        'scopes': creds.scopes
    }

    if save:
        del creds_data['token']

        config_path = os.path.dirname(credentials)
        if config_path and not os.path.isdir(config_path):
            os.makedirs(config_path)

        with open(credentials, 'w') as outfile:
            json.dump(creds_data, outfile)

        click.echo('credentials saved: %s' % credentials)

    else:
        click.echo(json.dumps(creds_data))
Example #35
0
def get_authenticated_service():
  flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
  credentials = flow.run_console()
  return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)