def save_and_post_video(self, video): video_title = video["snippet"]["title"] video_id = video["id"]["videoId"] video_url = f"https://www.youtube.com/watch?v={video_id}" msg = f"[Busy Beaver] YouTube Video Poll -- posting {video_title}" logger.info(msg) youtube_video = YouTubeVideo( youtube_id=video_id, title=video_title, published_at=video["snippet"]["publishedAt"], description=video["snippet"]["description"], ) db.session.add(youtube_video) db.session.commit() slack_msg = f"A new video has been released: {video_url}" chipy_slack.post_message(slack_msg, channel=self.channel)
def parse_and_post_videos(self, videos): last_video = YouTubeVideo.query.order_by(desc("published_at")).first() if not last_video: self.save_and_post_video(videos[0]) return # TODO only post last video without sleep # Maybe we need to rethink how we are doing this, but it's worth exploring # Have a field on the model that says posted or not? for video in videos: published_str = video["snippet"]["publishedAt"] published_dt = YouTubeVideo.date_str_to_datetime(published_str) if last_video.published_at < published_dt: self.save_and_post_video(video) # time.sleep(int(config.YOUTUBE_SECONDS_BETWEEN_POST)) else: break
def sort_by_published(video_json: Dict) -> datetime: publish_at = video_json["snippet"]["publishedAt"].split(".")[0] return YouTubeVideo.date_str_to_datetime(publish_at)