Example #1
0
        def get_info_from_search():
            search_data = tools.get_tmdb_search_data(tmdb_api_key,
                                                     self.movie_title)

            if search_data is None or search_data['total_results'] == 0:
                return False

            movie_data = None
            movie_backup_data = None

            if self.movie_release_year is None:
                movie_data = search_data['results'][0]
            else:

                for result in search_data['results'][:5]:
                    if movie_data is None:
                        if str(self.movie_release_year
                               ) == result['release_date'][:4]:
                            movie_data = result
                        elif result['release_date'][6:8] in ['09', '10', '11', '12'] \
                                and str(self.movie_release_year - 1) == result['release_date'][:4]:
                            movie_data = result
                        elif result['release_date'][6:8] in ['01', '02', '03', '04'] \
                                and str(self.movie_release_year + 1) == result['release_date'][:4]:
                            movie_data = result
                    elif movie_backup_data is None:
                        if str(self.movie_release_year -
                               1) == result['release_date'][:4]:
                            movie_backup_data = result

                        elif str(self.movie_release_year +
                                 1) == result['release_date'][:4]:
                            movie_backup_data = result

                if movie_data is None and movie_backup_data is not None:
                    print(
                        'None of the search results had a correct release year, picking the next best result'
                    )
                    movie_data = movie_backup_data

                if movie_data is None:
                    movie_data = search_data['results'][0]

            self.tmdb_id = movie_data['id']
            self.movie_title = tools.get_clean_string(movie_data['title'])
            self.movie_original_title = tools.get_clean_string(
                movie_data['original_title'])
            self.movie_title_keywords = tools.get_keyword_list(
                movie_data['title'])
            self.movie_original_title_keywords = tools.get_keyword_list(
                movie_data['original_title'])

            if len(movie_data['release_date'][:4]) == 4:
                self.movie_release_year = int(movie_data['release_date'][:4])
            else:
                self.movie_release_year = None
            return True
        def get_info_from_search():
            search_data = tools.get_tmdb_search_data(tmdb_api_key, self.movie_title)

            if search_data is None or search_data['total_results'] == 0:
                return False

            movie_data = None
            movie_backup_data = None

            if self.movie_release_year is None:
                movie_data = search_data['results'][0]
            else:

                for result in search_data['results'][:5]:
                    if movie_data is None:
                        if str(self.movie_release_year) == result['release_date'][:4]:
                            movie_data = result
                        elif result['release_date'][6:8] in ['09', '10', '11', '12'] \
                                and str(self.movie_release_year - 1) == result['release_date'][:4]:
                            movie_data = result
                        elif result['release_date'][6:8] in ['01', '02', '03', '04'] \
                                and str(self.movie_release_year + 1) == result['release_date'][:4]:
                            movie_data = result
                    elif movie_backup_data is None:
                        if str(self.movie_release_year - 1) == result['release_date'][:4]:
                            movie_backup_data = result

                        elif str(self.movie_release_year + 1) == result['release_date'][:4]:
                            movie_backup_data = result

                if movie_data is None and movie_backup_data is not None:
                    print('None of the search results had a correct release year, picking the next best result')
                    movie_data = movie_backup_data

                if movie_data is None:
                    movie_data = search_data['results'][0]

            self.tmdb_id = movie_data['id']
            self.movie_title = tools.get_clean_string(movie_data['title'])
            self.movie_original_title = tools.get_clean_string(movie_data['original_title'])
            self.movie_title_keywords = tools.get_keyword_list(movie_data['title'])
            self.movie_original_title_keywords = tools.get_keyword_list(movie_data['original_title'])

            if len(movie_data['release_date'][:4]) == 4:
                self.movie_release_year = int(movie_data['release_date'][:4])
            else:
                self.movie_release_year = None
            return True
Example #3
0
        def create_youtube_video():

            def get_video_data():

                for tries in range(1, 11):

                    try:

                        with youtube_dl.YoutubeDL({'socket_timeout': '3'}) as ydl:
                            return ydl.extract_info(url, download=False)

                    except DownloadError as e:

                        if 'ERROR: Unable to download webpage:' in e.args[0]:

                            if tries > 3:
                                print('hey, there: error!!!')
                                raise

                            print('failed to get video data, retrying')
                            time.sleep(1)
                        else:
                            return None

            youtube_video = get_video_data()

            if not youtube_video:
                return None

            youtube_video['title'] = tools.get_clean_string(youtube_video['title'])

            if youtube_video['view_count'] < 100:
                youtube_video['view_count'] = 100

	    #print(youtube_video['average_rating'])
            youtube_video['adjusted_rating'] = \
                youtube_video['average_rating'] * (1 - 1 / ((youtube_video['view_count'] / 60) ** 0.5))

            youtube_video['resolution_ratio'] = youtube_video['width'] / youtube_video['height']


            resolution = max(int(youtube_video['height']),
                             int(youtube_video['width'] / 16 * 9))
            resolutions = [144, 240, 360, 480, 720, 1080, 1440, 2160]

            youtube_video['resolution'] = resolutions[bisect(resolutions, resolution * 1.2) - 1]

            if youtube_video['upload_date']:
                date_str = youtube_video['upload_date']
                upload_date = date(int(date_str[:4]), int(date_str[4:6]), int(date_str[6:8]))
                time_delta = date.today() - upload_date
                youtube_video['views_per_day'] = (youtube_video['view_count'] /
                                                  (365 + time_delta.total_seconds() / 60 / 60 / 24))

            else:
                print('no "upload_date"!!!')
            return youtube_video
        def get_info_from_directory():
            clean_name_tuple = tools.get_clean_string(self.name).split(' ')

            if any(clean_name_tuple[-1] == str(year) for year in range(1896, date.today().year + 2)):
                self.movie_release_year = int(clean_name_tuple[-1])
                self.movie_title = ' '.join(clean_name_tuple[:-1])
                self.movie_original_title = ' '.join(clean_name_tuple[:-1])

            else:
                self.movie_release_year = None
                self.movie_title = ' '.join(clean_name_tuple)
                self.movie_original_title = ' '.join(clean_name_tuple)

            self.movie_title_keywords = tools.get_keyword_list(self.movie_title)
            self.movie_original_title_keywords = tools.get_keyword_list(self.movie_original_title)

            return True
Example #5
0
        def get_info_from_directory():
            clean_name_tuple = tools.get_clean_string(self.name).split(' ')
	    #print("clean title " + clean_name_tuple)

            if any(clean_name_tuple[-1] == str(year) for year in range(1896, date.today().year + 2)):
                self.movie_release_year = int(clean_name_tuple[-1])
                self.movie_title = ' '.join(clean_name_tuple[:-1])
                self.movie_original_title = ' '.join(clean_name_tuple[:-1])

            else:
                self.movie_release_year = None
                self.movie_title = ' '.join(clean_name_tuple)
                self.movie_original_title = ' '.join(clean_name_tuple)

            self.movie_title_keywords = tools.get_keyword_list(self.movie_title)
            self.movie_original_title_keywords = tools.get_keyword_list(self.movie_original_title)

            return True
    def __init__(self, url, score=0, preferred_container='mp4', min_resolution=360,
                 max_resolution=1080, force_preferred_container=False):

        ########################################
        self.url = None
        self.source = None
        self.delete = None
        self.complete = None
        self.is_play_trailer = None

        self.title = None
        self.thumbnail_url = None
        self.channel = None
        self.tags = list()

        self.view_count = None
        self.rating = None
        self.adjusted_rating = None
        self.resolution = None
        self.quality_score = None
        self.length = None
        self.resolution_ratio = None

        self.streams = list()
        self.best_video_stream = None
        self.best_audio_stream = None
        self.best_combined_stream = None
        ########################################

        self.url = url
        self.delete = False
        self.is_play_trailer = False
        self.complete = True

        tries = 0
        while True:
            try:
                self.source = YouTube(url)
            except KeyError as e:
                if e.args[0] == 'url':
                    self.delete = True
                    self.is_play_trailer = True
                    # todo (1): add youtube-dl info grabber/downloader
                    # stuff I need: title, length, keywords?
                    return
                elif e.args[0] == 'url_encoded_fmt_stream_map':
                    if tries > 4:
                        print('Failed to load youtube data, retrying. Reason: ' + str(e))
                        self.delete = True
                        return

                    print('Failed to load youtube data, retrying. Reason: ' + str(e))
                    time.sleep(2)
                    tries += 1

                else:
                    raise
            except RegexMatchError as e:
                print('Pytube failed to load video info. Reason: ' + url + ': ' + str(e))
                self.delete = True
                return
            except timeout as e:
                if tries > 4:
                    print('Pytube failed to load video info. Reason: ' + str(e))
                    self.complete = False
                    if Stream.conn_errors > 2:
                        raise
                    else:
                        Stream.conn_errors += 1
                    return

                print('Pytube failed to load video info. Reason: ' + str(e) + ', retrying...')
                tries += 1
                time.sleep(1)
            except URLError as e:
                if tries > 2:
                    print('Pytube failed to load video info. Reason: ' + str(e))
                    self.complete = False
                    if YoutubeVideo.conn_errors > 2:
                        raise
                    else:
                        YoutubeVideo.conn_errors += 1
                    return

                print('Pytube failed to load video info. Reason: ' + str(e) + ', retrying...')
                time.sleep(1)
                tries += 1
            else:
                YoutubeVideo.conn_errors = 0
                break

        self.score = score

        self.title = self.source.title
        self.title = tools.get_clean_string(self.title)
        self.rating = float(self.source.player_config_args['avg_rating'])
        self.view_count = int(self.source.player_config_args['view_count'])
        self.channel = self.source.player_config_args['author']
        self.length = self.source.player_config_args['length_seconds']

        self.thumbnail_url = self.source.thumbnail_url
        try:
            self.thumbnail_url = self.source.thumbnail_url
        except KeyError:
            self.thumbnail_url = None

        try:
            self.tags = self.source.player_config_args['keywords'].split(',')
        except KeyError:
            self.tags = ''

        if self.view_count < 100:
            self.view_count = 100

        self.adjusted_rating = self.rating * (1 - 1 / ((self.view_count / 60) ** 0.5))

        self.load_streams(min_resolution, max_resolution)
        self.update_quality_score(preferred_container)
        self.update_best_audio_stream(preferred_container, force_preferred_container)
        self.update_best_video_stream(preferred_container, force_preferred_container)
        self.update_best_combined_stream(preferred_container, force_preferred_container)

        if self.is_play_trailer:
            self.update_youtube_dl_info()
        def create_youtube_video():

            def get_video_data():

                for tries in range(1, 11):

                    try:

                        with youtube_dl.YoutubeDL({'socket_timeout': '3'}) as ydl:
                            return ydl.extract_info(url, download=False)

                    except DownloadError as e:

                        if 'ERROR: Unable to download webpage:' in e.args[0]:

                            if tries > 3:
                                print('hey, there: error!!!')
                                raise

                            print('failed to get video data, retrying')
                            time.sleep(1)
                        else:
                            return None

            youtube_video = get_video_data()

            if not youtube_video:
                return None

            youtube_video['title'] = tools.get_clean_string(youtube_video['title'])
            if youtube_video['like_count'] is None:
                youtube_video['like_count'] = 1
            if youtube_video['dislike_count'] is None:
                youtube_video['dislike_count'] = 1
            if youtube_video['view_count'] is None:
                youtube_video['view_count'] = (youtube_video['like_count'] + youtube_video['dislike_count']) * 230

            if youtube_video['view_count'] < 100:
                youtube_video['view_count'] = 100
            if youtube_video['average_rating'] is None:
                youtube_video['average_rating'] = youtube_video['like_count'] / \
                                                  (youtube_video['like_count'] + youtube_video['dislike_count'] + 1)
            youtube_video['adjusted_rating'] = \
                youtube_video['average_rating'] * (1 - 1 / ((youtube_video['view_count'] / 60) ** 0.5))

            youtube_video['resolution_ratio'] = youtube_video['width'] / youtube_video['height']

            resolution = max(int(youtube_video['height']),
                             int(youtube_video['width'] / 16 * 9))
            resolutions = [144, 240, 360, 480, 720, 1080, 1440, 2160]

            youtube_video['resolution'] = resolutions[bisect(resolutions, resolution * 1.2) - 1]

            if youtube_video['upload_date']:
                date_str = youtube_video['upload_date']
                upload_date = date(int(date_str[:4]), int(date_str[4:6]), int(date_str[6:8]))
                time_delta = date.today() - upload_date
                youtube_video['views_per_day'] = (youtube_video['view_count'] /
                                                  (365 + time_delta.total_seconds() / 60 / 60 / 24))

            else:
                print('no "upload_date"!!!')
            return youtube_video