Skip to content

Commit

Permalink
Added youtube API key to functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
remi1111 committed Mar 30, 2024
1 parent 72bb2ed commit 8441e69
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/yt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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 +
Expand All @@ -69,13 +69,15 @@ 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
return data

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
Expand All @@ -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&regionCode=NL&id=" +
videoid + "&key=" + yt_api_key)
Expand All @@ -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&regionCode=NL&id=" +
videoid + "&key=" + yt_api_key)
Expand All @@ -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&regionCode=NL&id=" +
videoid + "&key=" + yt_api_key)
Expand Down

0 comments on commit 8441e69

Please sign in to comment.