Skip to content

Commit

Permalink
Stop getting video IDs if they surpassed the time threshold set #4
Browse files Browse the repository at this point in the history
  • Loading branch information
drkostas committed Jun 4, 2021
1 parent 1a7d069 commit 7a08897
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ dmypy.json
/.idea

# Tmp files
*tmp.*
*tmp*.*

# Tars
*.gz
Expand Down
5 changes: 3 additions & 2 deletions youbot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def get_args() -> argparse.Namespace:
"""Setup the argument parser
""" Setup the argument parser.
Returns:
argparse.Namespace:
Expand Down Expand Up @@ -60,9 +60,10 @@ def main():
v_info = yout_manager.get_channel_info_by_username('Veritasium')
ku_info = yout_manager.get_channel_info_by_username('Kurzgesagt')
channel_ids = [pewd_info['id'], v_info['id'], ku_info['id']]
for video in yout_manager.get_uploads(channels=channel_ids):
for video in yout_manager.get_uploads(channels=channel_ids, last_n_hours=12000):
logger.info(video)


if __name__ == '__main__':
try:
main()
Expand Down
20 changes: 12 additions & 8 deletions youbot/youtube_utils/youtube_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _build_api(client_id: str, client_secret: str, api_version: str, read_only_s
return api

@staticmethod
def channel_from_response(response: Dict) -> Union[Dict, None]:
def _channel_from_response(response: Dict) -> Union[Dict, None]:
"""
Transforms a YouTube API response into a channel Dict.
Expand Down Expand Up @@ -101,7 +101,7 @@ def get_channel_info_by_username(self, username: str) -> Union[Dict, None]:
fields='items(id,snippet(title))'
).execute()
if channels_response:
channel = self.channel_from_response(channels_response)
channel = self._channel_from_response(channels_response)
if channel is not None:
channel['username'] = username
else:
Expand All @@ -122,13 +122,14 @@ def get_channel_info_by_id(self, channel_id: str) -> Union[Dict, None]:
fields='items(id,snippet(title))'
).execute()

return self.channel_from_response(channels_response)
return self._channel_from_response(channels_response)

def get_uploads(self, channels: List) -> Dict:
def get_uploads(self, channels: List, last_n_hours: int = 2) -> Dict:
""" Retrieves new uploads for the specified channels.
Args:
channels(list): A list with channel IDs
last_n_hours:
"""

# Separate the channels list in 50-sized channel lists
Expand All @@ -145,7 +146,7 @@ def get_uploads(self, channels: List) -> Dict:
# For each playlist ID, get 50 videos
for channel in channels_to_check:
uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"]
for upload in self.get_uploads_playlist(uploads_list_id):
for upload in self._get_uploads_playlist(uploads_list_id, last_n_hours):
upload['channel_title'] = channel['snippet']['title']
upload['channel_id'] = channel['id']
yield upload
Expand Down Expand Up @@ -174,7 +175,7 @@ def split_list(input_list: List, chunk_size: int) -> List:

return output_list

def get_uploads_playlist(self, uploads_list_id: str) -> Dict:
def _get_uploads_playlist(self, uploads_list_id: str, last_n_hours: int = 2) -> Dict:
""" Retrieves uploads using the specified playlist ID which were have been added
since the last check.
Expand All @@ -195,13 +196,16 @@ def get_uploads_playlist(self, uploads_list_id: str) -> Dict:
for playlist_item in playlist_items_response["items"]:
published_at = dateutil.parser.parse(playlist_item['snippet']['publishedAt'])
video = dict()
# Return the video only if it was published in the last 2 hours
if published_at >= (datetime.utcnow() - timedelta(hours=2)).replace(
# Return the video only if it was published in the last `last_n_hours` hours
if published_at >= (datetime.utcnow() - timedelta(hours=last_n_hours)).replace(
tzinfo=timezone.utc):
video['id'] = playlist_item["snippet"]["resourceId"]["videoId"]
video['published_at'] = playlist_item["snippet"]["publishedAt"]
video['title'] = playlist_item["snippet"]["title"]
yield video
# else:
# return

playlist_items_request = self._api.playlistItems().list_next(
playlist_items_request, playlist_items_response
)

0 comments on commit 7a08897

Please sign in to comment.