Skip to content

Commit

Permalink
Configurable max poster hours, started simulated youtube video posts #4
Browse files Browse the repository at this point in the history
  • Loading branch information
drkostas committed May 28, 2022
1 parent 00455c7 commit 91e0930
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
7 changes: 3 additions & 4 deletions comments/sample_comments.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Hey! I am a bot.
Hello there!
Nice video!
Woo nice!
Cool video
Nice editing
Ignore this comment
3 changes: 2 additions & 1 deletion confs/commenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ youtube:
client_secret: !ENV ${CLIENT_SECRET}
api_version: v3
read_only_scope: https://www.googleapis.com/auth/youtube.force-ssl
type: normal
sleep_time: 60
max_posted_hours: 250 # max num. of hours to check back for videos
type: simulated # normal, simulated
comment:
- config:
comments_list:
Expand Down
4 changes: 3 additions & 1 deletion youbot/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ def main():
db_conf = conf_obj.get_config('datastore')[0]
# Setup YouTube API
youtube = YoutubeManager(config=you_conf['config'], db_conf=db_conf,
sleep_time=you_conf['sleep_time'], tag=conf_obj.tag)
sleep_time=you_conf['sleep_time'],
max_posted_hours=you_conf['max_posted_hours'],
tag=conf_obj.tag)
# Run in the specified run mode
func = globals()[args.run_mode]
func(youtube, args)
Expand Down
15 changes: 8 additions & 7 deletions youbot/youtube_utils/youtube_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def comment(self, video_id: str, comment_text: str) -> None:
'snippet.topLevelComment.snippet.textOriginal': comment_text}
# self._comment_threads_insert(properties=properties,
# part='snippet')
# TODO: uncomment this when commenter is done
except Exception as exc:
logger.error(f"An error occurred:\n{exc}")

Expand Down Expand Up @@ -139,12 +140,12 @@ def get_channel_info_by_id(self, channel_id: str) -> Union[Dict, None]:

return self._yt_to_channel_dict(channels_response)

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

# Separate the channels list in 50-sized channel lists
Expand All @@ -161,7 +162,7 @@ def get_uploads(self, channels: List, last_n_hours: int = 2) -> 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, last_n_hours):
for upload in self._get_uploads_playlist(uploads_list_id, max_posted_hours):
upload['channel_title'] = channel['snippet']['title']
upload['channel_id'] = channel['id']
yield upload
Expand Down Expand Up @@ -274,8 +275,8 @@ def split_list(input_list: List, chunk_size: int) -> List:

return output_list

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
def _get_uploads_playlist(self, uploads_list_id: str, max_posted_hours: int = 2) -> Dict:
""" Retrieves uploads using the specified playlist ID which were had been added
since the last check.
Args:
Expand All @@ -296,7 +297,7 @@ def _get_uploads_playlist(self, uploads_list_id: str, last_n_hours: int = 2) ->
published_at = dateutil.parser.parse(playlist_item['snippet']['publishedAt'])
video = dict()
# 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(
if published_at >= (datetime.utcnow() - timedelta(hours=max_posted_hours)).replace(
tzinfo=timezone.utc):
video['id'] = playlist_item["snippet"]["resourceId"]["videoId"]
video['published_at'] = playlist_item["snippet"]["publishedAt"]
Expand All @@ -310,7 +311,7 @@ def _get_uploads_playlist(self, uploads_list_id: str, last_n_hours: int = 2) ->
)

def _comment_threads_insert(self, properties: Dict, **kwargs: Any) -> Dict:
""" Comment using the Youtube API.
""" Comment using the YouTube API.
Args:
properties:
**kwargs:
Expand Down
8 changes: 5 additions & 3 deletions youbot/youtube_utils/youtube_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
class YoutubeManager(YoutubeApiV3):
__slots__ = ('db', 'sleep_time')

def __init__(self, config: Dict, db_conf: Dict, sleep_time: int, tag: str):
def __init__(self, config: Dict, db_conf: Dict, sleep_time: int, max_posted_hours: int, tag: str):
self.db = YoutubeMySqlDatastore(config=db_conf['config'])
self.sleep_time = sleep_time
self.max_posted_hours = max_posted_hours
super().__init__(config, tag)

def commenter(self):
Expand All @@ -28,7 +29,7 @@ def commenter(self):
comments = self.db.get_comments(n_recent=50)
video_links_commented = [comment['video_link'] for comment in comments]
latest_videos = self.get_uploads(channels=channel_ids,
last_n_hours=250) # TODO: make this configurable
max_posted_hours=self.max_posted_hours)
comments_added = []
# Sort the videos by the priority of the channels (channel_ids are sorted by priority)
# and comment in the videos not already commented
Expand Down Expand Up @@ -112,7 +113,8 @@ def list_channels(self) -> None:
)
for row in self.db.get_channels()]

headers = ['Priority', 'Channel Name', 'Channel ID', 'Added On', 'Last Commented', 'Channel Photo']
headers = ['Priority', 'Channel Name', 'Channel ID', 'Added On', 'Last Commented',
'Channel Photo']
self.pretty_print(headers, channels)

def list_comments(self, n_recent: int = 50, min_likes: int = -1,
Expand Down

0 comments on commit 91e0930

Please sign in to comment.