From 69dd73dc96b10be19c494a2754a9c9fa7c035cd4 Mon Sep 17 00:00:00 2001 From: Kim Date: Wed, 2 Dec 2020 11:23:23 +0100 Subject: [PATCH] separated Class File and example usage code --- GetLastVideo.py | 60 ----------------------------------------------- GetLatestVideo.py | 40 +++++++++++++++++++++++++++++++ main.py | 20 ++++++++++++++++ 3 files changed, 60 insertions(+), 60 deletions(-) delete mode 100644 GetLastVideo.py create mode 100644 GetLatestVideo.py create mode 100644 main.py diff --git a/GetLastVideo.py b/GetLastVideo.py deleted file mode 100644 index 593a4fe..0000000 --- a/GetLastVideo.py +++ /dev/null @@ -1,60 +0,0 @@ -import requests - - -class API: - def __init__(self, youtube_api_key, channel_id): - self.api_key = youtube_api_key - self.channel_id = channel_id - - def is_not_used(self): - pass - - def get_video_items(self): - request = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' + \ - self.channel_id + '&maxResults=10&order=date&type=video&key=' + \ - self.api_key - - return requests.get(request).json() - - def get_newest_video(self, items_json): - self.is_not_used() - return items_json['items'][0] - - def get_url(self, video_json): - self.is_not_used() - return video_json['id']['videoId'] - - def get_title(self, video_json): - self.is_not_used() - return video_json['snippet']['title'] - - def start_find_video_process(self): - self.is_not_used() - video_items_json = var.get_video_items() - new_video_json = var.get_newest_video(video_items_json) - - video_url_snippet = var.get_url(new_video_json) - video_url = 'https://www.youtube.com/watch?v=' + video_url_snippet - - video_title = var.get_title(new_video_json) - - return video_title, video_url - - -# Add your Youtube Data API V3 Key here -API_KEY = '' - -# Add the Channel IDs here -# When you only know the channel name you can get the ID from here https://commentpicker.com/youtube-channel-id.php -CHANNEL_ID = [ - 'ID1', - 'ID2' -] - -videos = [] - -for item in CHANNEL_ID: - var = API(API_KEY, item) - videos.append(var.start_find_video_process()) - -print(videos) diff --git a/GetLatestVideo.py b/GetLatestVideo.py new file mode 100644 index 0000000..e723a97 --- /dev/null +++ b/GetLatestVideo.py @@ -0,0 +1,40 @@ +import requests + + +class API: + def __init__(self, youtube_api_key): + self.api_key = youtube_api_key + + def get_video_items(self, channel_id): + try: + request = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' + \ + channel_id + '&maxResults=10&order=date&type=video&key=' + \ + self.api_key + + return requests.get(request).json() + + except Exception as error: + print(error) + + @staticmethod + def get_newest_video(items_json): + return items_json['items'][0] + + @staticmethod + def get_url(video_json): + return video_json['id']['videoId'] + + @staticmethod + def get_title(video_json): + return video_json['snippet']['title'] + + def get_latest_video(self, channel_id): + video_items_json = self.get_video_items(channel_id) + new_video_json = self.get_newest_video(video_items_json) + + video_url_snippet = self.get_url(new_video_json) + video_url = 'https://www.youtube.com/watch?v=' + video_url_snippet + + video_title = self.get_title(new_video_json) + + return video_title, video_url diff --git a/main.py b/main.py new file mode 100644 index 0000000..dc7a634 --- /dev/null +++ b/main.py @@ -0,0 +1,20 @@ +from GetLatestVideo import API + +# Add your Youtube Data API V3 Key here +API_KEY = '' + +# Add the Channel IDs here +# When you only know the channel name you can get the ID from here https://commentpicker.com/youtube-channel-id.php +CHANNEL_ID = [ + 'ID1', + 'ID2' +] + +videos = [] + +yt = API(API_KEY) + +for channel in CHANNEL_ID: + videos.append(yt.get_latest_video(channel)) + +print(videos)