def add_youtube_channel_subscription_by_id(self, channel_id):
     """
     Subscribes to a channel based on channel ID
     :return:
     """
     self.logger.info("Adding subscription to channel: '{}'".format(channel_id))
     add_subscription(load_keys(1)[0], channel_id)
 def add_youtube_channel_subscription_by_username(self, username):
     """
     Subscribes to a channel based on username
     :return:
     """
     self.logger.info("Adding subscription to channel: '{}'".format(username))
     add_subscription(load_keys(1)[0], username, by_username=True)
 def get_single_video(self, video_url):
     """
     Fetches a specified video based on url
     :return:
     """
     self.logger.info("Fetching video: {}".format(video_url))
     reggie = re.match(YOUTUBE_URL_PATTERN, video_url)
     video_id = reggie.groups()[-1]
     self.logger.debug("{} --> ID: {}".format(video_url, video_id))
     video_d = list_uploaded_videos_videos(load_keys(1)[0], [video_id], 50)[0]
     download_thumbnails_threaded([video_d])
     DownloadViewListener.download_video(video_d,
                                         youtube_dl_finished_listener=[
                                             self.model.playback_grid_view_listener.downloadFinished],
                                         db_update_listeners=[
                                             self.model.playback_grid_view_listener.downloadedVideosChangedinDB])
Example #4
0
def get_new_and_updated_videos(vid_paths):
    """
    :param vid_paths: dict(video_id, vid_path)
    :return:
    """
    logger = create_logger(__name__ + ".new_and_updated")
    db_videos = get_videos_by_ids(vid_paths.keys())

    return_videos = []
    for video in db_videos:
        if not video.vid_path or not video.downloaded:
            video_d = Video.to_video_d(video)
            video_d.vid_path = vid_paths[video.video_id]
            video_d.downloaded = True
            video_d.date_downloaded = datetime.datetime.utcnow()
            logger.info("Found video needing update: {} - {}".format(
                video.video_id, video.title))

            return_videos.append(video_d)
        vid_paths.pop(video.video_id, None)

    new_videos = []
    if len(vid_paths) > 0:
        youtube_keys = load_keys(1)
        logger.info(
            "Grabbing new video(s) information from youtube for: {}".format(
                vid_paths.keys()))
        response_videos = list_uploaded_videos_videos(youtube_keys[0],
                                                      vid_paths.keys(), 30)
        for video in response_videos:
            video.vid_path = vid_paths[video.video_id]
            video.downloaded = True
            video.watched = False
            video.date_downloaded = datetime.datetime.utcnow()
            logger.info("Found new video: {} - {}".format(
                video.video_id, video.title))
            new_videos.append(video)

    return_videos.extend(new_videos)
    logger.info("Downloading thumbnails for: {}".format(return_videos))
    download_thumbnails_threaded(return_videos)
    return return_videos
Example #5
0
def run_channels_test():
    logger.info('Running Channels Test')
    subscriptions = get_subscriptions(cached_subs)
    youtube_keys = load_keys(len(subscriptions))
    test_threads = []
    results = []
    logger.info("Channels Test: Starting miss and pages tests")
    for subscription, youtube_key in (subscriptions, youtube_keys):
        test = RunTestsThreaded(subscription, youtube_key, results)
        test.start()
        test_threads.append(test)

    logger.info("Channels Test: Waiting for test threads")
    for thread in test_threads:
        thread.join()

    for result in results:
        test = Test(result[0], result[1], result[2], result[3])
        db_session.add(test)
    db_session.commit()
    db_session.remove()
Example #6
0
 def get_single_video(self, video_url):
     """
     Fetches a specified video based on url
     :return:
     """
     self.logger.info("Fetching video: {}".format(video_url))
     video_id = video_url.split('v=')[
         -1]  # FIXME: Make a proper input sanitizer
     self.logger.debug("{} --> ID: {}".format(video_url, video_id))
     video_d = list_uploaded_videos_videos(load_keys(1)[0], [video_id],
                                           50)[0]
     download_thumbnails_threaded([video_d])
     # self.logger.debug(video_d.__dict__)
     # DownloadHandler.download_video(video_d)
     DownloadHandler.download_video(
         video_d,
         youtube_dl_finished_listener=[
             GridViewListener.static_self.downloadFinished
         ],
         db_update_listeners=[
             GridViewListener.static_self.downloadedVideosChangedinDB
         ])
    def new_file(self, vid_id, vid_path):
        vid = db_session.query(Video).get(vid_id)
        if vid:
            if not vid.downloaded:
                vid.vid_path = vid_path
                vid.date_downloaded = datetime.datetime.utcnow()
                vid.downloaded = True

                thumb_path = os.path.join(THUMBNAILS_PATH,
                                          '{}.jpg'.format(vid.video_id))
                downloaded_thumbnail = os.path.isfile(thumb_path)
                if downloaded_thumbnail and (not vid.thumbnail_path):
                    vid.thumbnail_path = thumb_path
                    self.logger.warning(
                        "Thumbnail downloaded, but path didn't exist in db, for video: {}"
                        .format(vid.__dict__))
                elif (not vid.thumbnail_path) or (not downloaded_thumbnail):
                    if not downloaded_thumbnail:
                        self.logger.warning(
                            "Thumbnail path in db, but not on disk, for video: {}"
                            .format(vid.__dict__))
                    self.logger.info("Downloading thumbnail for: {}".format(
                        vid.__dict__))
                    download_thumbnails_threaded([vid])

                self.logger.info(
                    "Updating existing record in db: {} - {}".format(
                        vid.title, vid.__dict__))
                db_session.commit()
                self.model.update_subfeed_videos_from_db()
                self.model.update_playback_videos_from_db()
            else:
                self.logger.info(
                    "File already downloaded by this system: {} - {}".format(
                        vid.title, vid.__dict__))
            db_session.remove()

        else:
            db_session.remove()
            youtube_keys = load_keys(1)
            self.logger.info(
                "Grabbing new video information from youtube: {}".format(
                    vid_id))
            response_videos = list_uploaded_videos_videos(
                youtube_keys[0], [vid_id], 1)
            if len(response_videos) > 0:
                video = response_videos[0]
                video.vid_path = vid_path
                video.downloaded = True
                video.watched = False
                video.date_downloaded = datetime.datetime.utcnow()
                self.logger.info("Downloading thumbnail: {} - {}".format(
                    video.title, video.__dict__))
                download_thumbnails_threaded([video])
                self.logger.info("Adding new file to db: {} - {}".format(
                    video.title, video.__dict__))
                UpdateVideo(video,
                            finished_listeners=[
                                self.model.playback_grid_view_listener.
                                downloadedVideosChangedinDB
                            ]).start()
            else:
                self.logger.warning(
                    "Video with id {}, not found on youtube servers".format(
                        vid_id))
Example #8
0
def cli(no_gui, test_channels, update_watch_prio, set_watched_day,
        refresh_and_print_subfeed, print_subscriptions, print_watched_videos,
        print_discarded_videos, print_downloaded_videos, print_playlist_items,
        print_playlist_items_url_only, auth_oauth2):
    logger = create_logger(__name__)

    if update_watch_prio:
        videos = db_session.query(Video).all()
        watch_prio = read_config('Play', 'default_watch_prio')
        logger.debug("Setting watch_prio {}, for: {} videos".format(
            watch_prio, len(videos)))
        for video in videos:
            video.watch_prio = watch_prio
        db_session.commit()
        return

    if set_watched_day:
        videos = db_session.query(Video).filter(
            or_(Video.downloaded == True, (Video.vid_path.is_(None)))).all()
        for video in videos:
            vid_age = datetime.datetime.utcnow() - video.date_published
            if vid_age > datetime.timedelta(days=int(set_watched_day)):
                logger.debug("Setting watched, {} - {} - {}".format(
                    vid_age, video.title, video.__dict__))
                video.watched = True
        db_session.commit()
        return

    if test_channels:
        run_channels_test()

    if refresh_and_print_subfeed:
        cli_refresh_and_print_subfeed()

    if print_subscriptions:
        cached_subs = True
        subs = get_subscriptions(cached_subs)
        for channel in subs:
            if channel.subscribed_override:
                print(("[{}]    {} [Subscription override]".format(
                    channel.id, channel.title)))
            else:
                print(("[{}]    {}".format(channel.id, channel.title)))

    if print_watched_videos:
        videos = db_session.query(Video).filter(
            and_(Video.watched is True, (Video.vid_path.isnot(None)))).all()
        print_functions.print_videos(videos, path_only=True)

    if print_discarded_videos:
        videos = db_session.query(Video).filter(
            and_(Video.discarded is True, (Video.vid_path.isnot(None)))).all()
        print_functions.print_videos(videos, path_only=True)

    if print_downloaded_videos:
        videos = db_session.query(Video).filter(
            and_(Video.downloaded is True,
                 (Video.vid_path.isnot(None)))).all()
        print_functions.print_videos(videos, path_only=True)

    if print_playlist_items:
        youtube_auth_resource = load_keys(1)[0]
        playlist_video_items = []
        youtube.youtube_requests.list_uploaded_videos(youtube_auth_resource,
                                                      playlist_video_items,
                                                      print_playlist_items, 50)
        for vid in playlist_video_items:
            if print_playlist_items_url_only:
                print(vid.url_video)
            else:
                print(vid)

    if auth_oauth2:
        youtube_oauth = youtube_auth_oauth()
        if youtube_oauth is None:
            logger.critical("Failed to authenticate YouTube API OAuth2!")
            return None
        save_youtube_resource_oauth(youtube_oauth)

    if no_gui:
        run_with_cli()
    else:
        if LEGACY_EXCEPTION_HANDLER:
            """
            PyQT raises and catches exceptions, but doesn't pass them along. 
            Instead it just exits with a status of 1 to show an exception was caught. 
            """
            # Back up the reference to the exceptionhook
            sys._excepthook = sys.excepthook

            def my_exception_hook(exctype, value, traceback):
                global exc_id, exceptions
                # Ignore KeyboardInterrupt so a console python program can exit with Ctrl + C.
                if issubclass(exctype, KeyboardInterrupt):
                    sys.__excepthook__(exctype, value, traceback)
                    return

                # Log the exception with the logger
                logger.exception("Intercepted Exception #{}".format(exc_id),
                                 exc_info=(exctype, value, traceback))

                # Store intercepted exceptions in a reference list of lists
                exceptions.append([exctype, value, traceback, exc_id])

                # Increment Exception Identifier
                exc_id += 1

                # Call the normal Exception hook after
                # noinspection PyProtectedMember
                sys._excepthook(exctype, value, traceback)

                # sys.exit(1)       # Alternatively, exit

            # Set the exception hook to our wrapping function
            sys.excepthook = my_exception_hook

        run_with_gui()