diff --git a/src/yt_api.py b/src/yt_api.py index 6943e3a..9ff06c3 100644 --- a/src/yt_api.py +++ b/src/yt_api.py @@ -5,8 +5,6 @@ import requests -yt_api_key = os.getenv("YOUTUBE_API") - def api_call(page): """ Handles youtube API calls. Exits runtime upon failure. @@ -29,14 +27,15 @@ def get_upload_list(channel_id): if channel_id[:2] != "UC": print("Channel ID did not start with UC", file=sys.stderr) sys.exit(1) - channel_id[2] = "U" - return channel_id + s = list(channel_id)[1] = "U" + return "".join(s) def get_upload_list_call(channel_id, verbose=False): """ Api call for channel to user uploads. """ if channel_id[:2] != "UC": print("Channel ID did not start with UC", file=sys.stderr) sys.exit(1) + yt_api_key = os.getenv("YOUTUBE_API") data = api_call("https://www.googleapis.com/youtube/v3/channels?id=" + channel_id + "&key=" + yt_api_key + "&part=contentDetails,statistics").content @@ -48,6 +47,7 @@ def get_upload_list_call(channel_id, verbose=False): def get_video_list(playlist_id, video_array, page_token=None): """ Retrieves a list of videos from a youtube playlist. """ + yt_api_key = os.getenv("YOUTUBE_API") if page_token: data = api_call("https://www.googleapis.com/youtube/v3/playlistItems?playlistId=" + playlist_id + "&key=" + yt_api_key + @@ -69,6 +69,7 @@ def get_video_list(playlist_id, video_array, page_token=None): def get_playlist_by_channel(channel_id): """ Retrievs all playlists of a channel. """ + yt_api_key = os.getenv("YOUTUBE_API") data = api_call("https://youtube.googleapis.com/youtube/v3/" + "playlists?part=snippet%2CcontentDetails&channelId=" + channel_id +"&maxResults=50&key=" + yt_api_key).content @@ -76,6 +77,7 @@ def get_playlist_by_channel(channel_id): def get_id_from_playlist(playlist_id): """ No idea """ + yt_api_key = os.getenv("YOUTUBE_API") data = api_call("https://www.googleapis.com/youtube/v3/" + "playlistItems?part=contentDetails,snippet,id&playlistId=" + playlist_id + "&key=" + yt_api_key).content @@ -84,6 +86,7 @@ def get_id_from_playlist(playlist_id): def video_is_valid(videoid): """ Return a boolean if the video is valid in a country. """ + yt_api_key = os.getenv("YOUTUBE_API") data = api_call("https://youtube.googleapis.com/youtube/v3/" + "videos?part=contentDetails®ionCode=NL&id=" + videoid + "&key=" + yt_api_key) @@ -100,6 +103,7 @@ def video_is_valid(videoid): def return_blocked(videoid): """ Prints a list of allowed or blocked countries. """ + yt_api_key = os.getenv("YOUTUBE_API") data = api_call("https://youtube.googleapis.com/youtube/v3/" + "videos?part=contentDetails®ionCode=NL&id=" + videoid + "&key=" + yt_api_key) @@ -114,19 +118,31 @@ def return_blocked(videoid): print(cont['regionRestriction']['blocked']) return False +def take_topic(json_data, verbose=False): + """ Takes first channel with topic in the name. """ + for item in json_data: + channel_name = item['snippet']['channelTitle'] + if verbose: + print(channel_name) + if "Topic" in channel_name: + return item['id']['channelId'] + def search_channel(channel_name, verbose=False): """ Returns the channel ID of the first result. """ + yt_api_key = os.getenv("YOUTUBE_API") data = api_call("https://www.googleapis.com/youtube/v3/" + "search?part=snippet&type=channel&q=" + channel_name + "&key=" + yt_api_key) - + channel_id = take_topic(json.loads(data.content)['items'], verbose) if verbose: + print(f"Channel ID: {channel_id}") print(json.loads(data.content)) - return json.loads(data.content)['items'][0]['id']['channelId'] + return channel_id def video_is_valid_and_statistics(videoid, verbose=False): """ Returns a tuple of if a video is valid and its viewcount. """ + yt_api_key = os.getenv("YOUTUBE_API") data = api_call("https://youtube.googleapis.com/youtube/v3/" + "videos?part=contentDetails,statistics®ionCode=NL&id=" + videoid + "&key=" + yt_api_key)