Exemple #1
0
 def youtube_video_post(self, search_keyword, prev_datetime):
     youtube = YoutubeSearch(self.youtube_developer_key)
     videos = youtube.search_videos(search_keyword, prev_datetime)
     if not videos:
         logger.info("youtube_video_post(): No tweet messages")
         return
     # Make tweet message.
     for video in reversed(videos):
         str_published_at = video.published_at.strftime("%y/%m/%d %H:%M")
         tweet_msg = utils.make_tweet_msg(
             self.TW_YOUTUBE_TWEET_FORMAT, str_published_at, title=video.title, url=video.get_url()
         )
         self.tweet_msg(tweet_msg)
Exemple #2
0
    def nico_video_post(self, search_keyword, prev_datetime):
        logger.debug("Call nico_video_post({}, {})".format(search_keyword, prev_datetime))
        if prev_datetime:
            now_date = datetime.datetime.now()
            if now_date - prev_datetime < datetime.timedelta(1):
                old_prev_datetime = prev_datetime
                prev_datetime = prev_datetime - datetime.timedelta(hours=2)
                logger.debug("Change prev_datetime: {} -> {}".format(old_prev_datetime, prev_datetime))

        with DbManager() as db_manager:
            nico = NicoSearch(db_manager, self.nico_user_id, self.nico_pass_word)
            nico.login()
            # Search latest videos by NicoNico.
            videos = nico.search_videos(search_keyword, prev_datetime)

            tweet_count = 0
            failed_list = []
            for video in reversed(videos):
                # Check if the video is already posted.
                old_post_video = (
                    db_manager.db_session.query(PostVideo)
                    .filter(sqlalchemy.and_(PostVideo.video_id == video.id))
                    .first()
                )

                if old_post_video:
                    logger.debug("Skip posted video: video={}".format(video))
                    continue

                # Add new post_video to database when not registerd
                post_video = PostVideo(video.id)
                logger.info("Add new post_video to database : post_video={}".format(post_video))
                db_manager.db_session.add(post_video)

                # Make message for twitter.
                str_first_retrieve = video.first_retrieve.strftime("%y/%m/%d %H:%M")
                msg = utils.make_tweet_msg(
                    self.TW_NICO_VIDEO_TWEET_FORMAT, str_first_retrieve, title=video.title, url=video.get_url()
                )
                try:
                    self.tweet_msg(msg, is_sleep=True)
                    tweet_count += 1
                    db_manager.db_session.commit()
                except Exception as e:
                    db_manager.db_session.rollback()
                    logger.exception("Tweet failed msg={}".format(msg))
                    failed_list.append((e, msg))

            logger.info("nico_latest_commenting_video(): {} tweet".format(tweet_count))
            if failed_list:
                raise Exception("Tweet faild {}".format(prettyprint.pp_str(failed_list)))
Exemple #3
0
    def nico_latest_commenting_video_post(
        self, search_keyword, prev_datetime, number_of_results=3, expire_days=30, max_post_count=1
    ):
        logger.debug(
            "Call nico_latest_commenting_video_post({}, {}, {}, {}, {})".format(
                search_keyword, prev_datetime, number_of_results, expire_days, max_post_count
            )
        )
        with DbManager() as db_manager:
            nico = NicoSearch(db_manager, self.nico_user_id, self.nico_pass_word)
            nico.login()
            # Search latest commenting videos by NicoNico.
            it = nico.search_latest_commenting_videos(
                search_keyword, prev_datetime, number_of_results, expire_days, max_post_count
            )
            tweet_count = 0
            failed_list = []
            for video in it:
                # Make message to tweet.
                str_first_retrieve = video.first_retrieve.strftime("%y/%m/%d %H:%M")
                msg = utils.make_tweet_msg(
                    self.TW_NICO_DETAIL_VIDEO_TWEET_FORMAT,
                    str_first_retrieve,
                    video.view_counter,
                    video.num_res,
                    video.mylist_counter,
                    title=video.title,
                    url=video.get_url(),
                )
                try:
                    self.tweet_msg(msg, is_sleep=True)
                    tweet_count += 1
                    db_manager.db_session.commit()
                except Exception as e:
                    db_manager.db_session.rollback()
                    logger.exception("Tweet failed msg={}".format(msg))
                    failed_list.append((e, msg))

            logger.info("nico_latest_commenting_video(): {} tweet".format(tweet_count))
            if failed_list:
                raise Exception("Tweet faild {}".format(prettyprint.pp_str(failed_list)))
Exemple #4
0
    def nico_comment_post(
        self, search_keyword, prev_datetime, max_comment_num=1500, max_tweet_num_per_video=3, filter_func=None
    ):
        logger.debug(
            "Call nico_comment_post({}, {}, {}, {}, {})".format(
                search_keyword, prev_datetime, max_comment_num, max_tweet_num_per_video, filter_func
            )
        )
        with DbManager() as db_manager:
            nico = NicoSearch(db_manager, self.nico_user_id, self.nico_pass_word)
            nico.login()
            # Search latest comments by NicoNico.
            videos = nico.search_videos_with_comments(search_keyword, prev_datetime, max_comment_num)
            tweet_count = 0
            failed_list = []
            for video in videos:
                if filter_func and filter_func(video):
                    continue
                for nico_comment in video.get_latest_comments(max_tweet_num_per_video):
                    # Make message for twitter.
                    msg = utils.make_tweet_msg(
                        self.TW_NICO_COMMENT_TWEET_FORMAT,
                        nico_comment.vpos,
                        nico_comment.post_datetime,
                        comment=nico_comment.comment,
                        title=video.title,
                        url=video.get_url(),
                    )
                    try:
                        self.tweet_msg(msg, is_sleep=True)
                        tweet_count += 1
                    except Exception as e:
                        logger.exception("Tweet failed msg={}".format(msg))
                        failed_list.append((e, msg))

            logger.info("nico_latest_commenting_video(): {} tweet".format(tweet_count))
            if failed_list:
                raise Exception("Tweet faild {}".format(prettyprint.pp_str(failed_list)))