Example #1
0
def add_id_to_db(ids_to_add, list_type, begin=0, maxi=0):
    series_id = []
    anime_id = []
    movies_id = []
    for i, tmdb_id in enumerate(ids_to_add):
        if list_type != ListType.MOVIES:
            _set_task_progress(i * (maxi/len(ids_to_add)))
        else:
            _set_task_progress(begin + i*((90-begin)/len(ids_to_add)))
        try:
            api_data = ApiData().get_details_and_credits_data(tmdb_id, list_type)
            if list_type != ListType.MOVIES:
                genres = [g['id'] for g in api_data.get('genres')]
                if api_data.get('origin_country') == 'JP' or api_data.get('original_language') == 'ja' and 16 in genres:
                    list_type = ListType.ANIME
                else:
                    list_type = ListType.SERIES
            media_details = MediaDetails(api_data, list_type).get_media_details()
            media = AddtoDB(media_details, list_type).add_media_to_db()

            if list_type == ListType.SERIES:
                series_id.append(media.id)
            elif list_type == ListType.ANIME:
                anime_id.append(media.id)
            elif list_type == ListType.MOVIES:
                movies_id.append(media.id)

        except Exception as e:
            print(e)

    db.session.commit()
    return series_id, anime_id, movies_id
Example #2
0
    def get_anime_genres(self):
        a_genres_list = []
        try:
            anime_search = ApiData().anime_search(self.media_data.get("name"))
            anime_genres = ApiData().get_anime_genres(
                anime_search["results"][0]["mal_id"])['genres']
        except Exception as e:
            app.logger.error(
                '[ERROR] - Requesting the Jikan API: {}'.format(e),
                {'API': 'Jikan'})
            anime_genres = None

        if anime_genres:
            for i in range(0, len(anime_genres)):
                genres_dict = {
                    'genre': anime_genres[i]['name'],
                    'genre_id': int(anime_genres[i]['mal_id'])
                }
                a_genres_list.append(genres_dict)

        return a_genres_list
Example #3
0
def autocomplete():
    search = request.args.get('q')

    # Get the users results
    users = User.query.filter(User.username.like('%' + search + '%'),
                              User.active == True).all()
    users_results = []
    for user in users:
        users_results.append(Autocomplete(user).get_user_dict())

    # Get the media results
    try:
        media_data = ApiData().TMDb_search(search)
    except Exception as e:
        media_data = {}
        app.logger.error('[ERROR] - Requesting the TMDB API: {}'.format(e))

    media_results = []
    if media_data.get('total_results', 0) or 0 > 0:
        for i, result in enumerate(media_data["results"]):
            if i >= media_data["total_results"] or i > 19 or len(
                    media_results) >= 7:
                break
            if result.get('known_for_department'):
                continue
            media_results.append(Autocomplete(result).get_autocomplete_dict())

    # Create the <total_results> list
    total_results = media_results + users_results
    if len(total_results) == 0:
        return jsonify(search_results=[{
            'nb_results': 0,
            'category': None
        }]), 200

    total_results = sorted(total_results, key=lambda i: i['category'])

    return jsonify(search_results=total_results), 200
Example #4
0
    def get_media_cover(self):
        media_cover_name = 'default.jpg'
        media_cover_path = self.media_data.get('poster_path') or None

        if media_cover_path:
            media_cover_name = '{}.jpg'.format(secrets.token_hex(8))
            try:
                ApiData().save_api_cover(media_cover_path, media_cover_name,
                                         self.list_type)
            except Exception as e:
                app.logger.error(
                    '[ERROR] - Trying to recover the poster: {}'.format(e))
                media_cover_name = 'default.jpg'

        return media_cover_name
Example #5
0
def current_trends():
    try:
        series_info = ApiData().get_trending_tv()
    except Exception as e:
        series_info = {'results': []}
        app.logger.error('[ERROR] - Getting the Series trending info: {}.'.format(e))
        flash('The current TV trends from TMDB are not available right now.', 'warning')

    try:
        anime_info = ApiData().get_trending_anime()
    except Exception as e:
        anime_info = {'top': []}
        app.logger.error('[ERROR] - Getting the anime trending info: {}.'.format(e))
        flash('The current anime trends from Jikan are not available right now.', 'warning')

    try:
        movies_info = ApiData().get_trending_movies()
    except Exception as e:
        movies_info = {'results': []}
        app.logger.error('[ERROR] - Getting the movies trending info: {}.'.format(e))
        flash('The current movies trends from TMDB are not available right now.', 'warning')

    series_results = TrendingData(series_info).get_trending_series()
    anime_results = TrendingData(anime_info).get_trending_anime()
    movies_results = TrendingData(movies_info).get_trending_movies()

    template = 'current_trends_pc.html'
    platform = str(request.user_agent.platform)
    if platform == "iphone" or platform == "android" or not platform or platform == 'None':
        template = 'current_trends_mobile.html'

    return render_template(template,
                           title="Current trends",
                           series_trends=series_results,
                           anime_trends=anime_results,
                           movies_trends=movies_results)
Example #6
0
def search_media():
    search = request.args.get('search')
    page = request.args.get('page', 1)

    if not search or len(search) == 0:
        return redirect(request.referrer)

    try:
        data_search = ApiData().TMDb_search(search, page=page)
    except Exception as e:
        data_search = {}
        app.logger.error(
            '[SYSTEM] - Error requesting the TMDB API: {}'.format(e))
        flash('Sorry, an error occured, the TMDB API is unreachable for now.',
              'warning')

    if data_search.get("total_results", 0) == 0:
        flash('Sorry, no results found for your query.', 'warning')
        return redirect(request.referrer)

    # Recover 1 page of results (20 max) without peoples
    media_results = []
    for result in data_search["results"]:
        if result.get('known_for_department'):
            continue

        media_data = {
            'name':
            result.get('original_title') or result.get('original_name'),
            'overview':
            result.get('overview'),
            'first_air_date':
            result.get('first_air_date') or result.get('release_date'),
            'api_id':
            result['id']
        }

        # Modify the first_air_date / release_date format
        if media_data['first_air_date'] == '':
            media_data['first_air_date'] = 'Unknown'

        # Recover the poster_path or take a default image
        if result["poster_path"]:
            media_data["poster_path"] = "{}{}".format(
                "http://image.tmdb.org/t/p/w300", result["poster_path"])
        else:
            media_data["poster_path"] = url_for(
                'static', filename="covers/anime_covers/default.jpg")

        # Put data in different lists in function of media type
        if result['media_type'] == 'tv':
            media_data['url'] = f"https://www.themoviedb.org/tv/{result['id']}"
            media_data['media'] = 'Series'
            if result['origin_country'] == 'JP' or result['original_language'] == 'ja' \
                    and 16 in result['genre_ids']:
                media_data['media_type'] = ListType.ANIME.value
                media_data['name'] = result['name']
                media_data['media'] = 'Anime'
                media_results.append(media_data)
            else:
                media_data['media_type'] = ListType.SERIES.value
                media_results.append(media_data)
        elif result['media_type'] == 'movie':
            media_data['media'] = 'Movies'
            media_data['media_type'] = ListType.MOVIES.value
            media_data[
                'url'] = f"https://www.themoviedb.org/movie/{result['id']}"
            if result['original_language'] == 'ja' and 16 in result[
                    'genre_ids']:
                media_data['name'] = result['title']
            media_results.append(media_data)

    # Get the plateform to display the appropriate template
    mobile = False
    platform = str(request.user_agent.platform)
    if platform == "iphone" or platform == "android" or platform == 'None' or not platform:
        mobile = True

    return render_template('media_search.html',
                           title="Search",
                           mobile=mobile,
                           all_results=media_results,
                           search=search,
                           page=int(page),
                           total_results=data_search['total_results'])
Example #7
0
def media_sheet(media_type, media_id):
    # Check if <media_type> is valid
    try:
        media_type = MediaType(media_type)
    except ValueError:
        abort(404)

    if media_type == MediaType.SERIES:
        list_type = ListType.SERIES
    elif media_type == MediaType.ANIME:
        list_type = ListType.ANIME
    elif media_type == MediaType.MOVIES:
        list_type = ListType.MOVIES

    # Check if <media_id> came from an API and if in local DB
    api_id = request.args.get('search')

    if api_id:
        search = {'themoviedb_id': media_id}
    else:
        search = {'id': media_id}

    html_template = 'media_sheet_tv.html'
    if list_type == ListType.SERIES:
        media = Series.query.filter_by(**search).first()
    elif list_type == ListType.ANIME:
        media = Anime.query.filter_by(**search).first()
    elif list_type == ListType.MOVIES:
        media = Movies.query.filter_by(**search).first()
        html_template = 'media_sheet_movies.html'

    # If <media> does not exist and <api_id> is provived: Add the <media> to DB, else abort.
    if not media:
        if api_id:
            try:
                media_api_data = ApiData().get_details_and_credits_data(
                    media_id, list_type)
            except Exception as e:
                app.logger.error(
                    '[ERROR] - Impossible to get the details and credits from API ({}) ID [{}]: {}'
                    .format(media_type.value, media_id, e))
                flash(
                    'Sorry, a problem occured trying to load the media info. Please try again later.',
                    'warning')
                return redirect(request.referrer)
            try:
                media_details = MediaDetails(media_api_data,
                                             list_type).get_media_details()
            except Exception as e:
                app.logger.error(
                    '[ERROR] - Occured trying to parse the API data to dict ({}) ID [{}]: {}'
                    .format(media_type.value, media_id, e))
                flash(
                    'Sorry, a problem occured trying to load the media info. Please try again later.',
                    'warning')
                return redirect(request.referrer)
            try:
                media = AddtoDB(media_details, list_type).add_media_to_db()
            except Exception as e:
                app.logger.error(
                    '[ERROR] - Occured trying to add media ({}) ID [{}] to DB: {}'
                    .format(media_type.value, media_id, e))
                flash(
                    'Sorry, a problem occured trying to load the media info. Please try again later.',
                    'warning')
                return redirect(request.referrer)
        else:
            abort(404)

    # If <media> and <api_id> provived: redirect to get a nice URL.
    if media and api_id:
        return redirect(
            url_for('main.media_sheet',
                    media_type=media_type.value,
                    media_id=media.id))

    media_info = MediaDict(media, list_type).create_list_dict()
    title = media_info['display_name']

    return render_template(html_template,
                           title=title,
                           data=media_info,
                           media_list=list_type.value)
Example #8
0
def refresh_element_data(api_id, list_type):
    ApiModel = ApiData.get_API_model(list_type)
    data = ApiModel(API_id=api_id).update_media_data()

    # Update the main details for each media
    if list_type == ListType.SERIES:
        Series.query.filter_by(api_id=api_id).update(data['media_data'])
    elif list_type == ListType.ANIME:
        Anime.query.filter_by(api_id=api_id).update(data['media_data'])
    elif list_type == ListType.MOVIES:
        Movies.query.filter_by(api_id=api_id).update(data['media_data'])
    elif list_type == ListType.GAMES:
        Games.query.filter_by(api_id=api_id).update(data['media_data'])

    # Commit the new changes
    db.session.commit()

    # Check the episodes/seasons
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        if list_type == ListType.SERIES:
            element = Series.query.filter_by(api_id=api_id).first()
            old_seas_eps = [
                n.episodes for n in SeriesEpisodesPerSeason.query.filter_by(
                    media_id=element.id).all()
            ]
        elif list_type == ListType.ANIME:
            element = Anime.query.filter_by(api_id=api_id).first()
            old_seas_eps = [
                n.episodes for n in AnimeEpisodesPerSeason.query.filter_by(
                    media_id=element.id).all()
            ]

        new_seas_eps = [d['episodes'] for d in data['seasons_data']]

        if new_seas_eps != old_seas_eps:
            if list_type == ListType.SERIES:
                users_list = SeriesList.query.filter_by(
                    media_id=media.id).all()

                for user in users_list:
                    episodes_watched = user.total

                    count = 0
                    for i in range(0, len(data['seasons_data'])):
                        count += data['seasons_data'][i]['episodes']
                        if count == episodes_watched:
                            user.last_episode_watched = data['seasons_data'][
                                i]['episodes']
                            user.current_season = data['seasons_data'][i][
                                'season']
                            break
                        elif count > episodes_watched:
                            user.last_episode_watched = data['seasons_data'][
                                i]['episodes'] - (count - episodes_watched)
                            user.current_season = data['seasons_data'][i][
                                'season']
                            break
                        elif count < episodes_watched:
                            try:
                                data['seasons_data'][i + 1]['season']
                            except IndexError:
                                user.last_episode_watched = data[
                                    'seasons_data'][i]['episodes']
                                user.current_season = data['seasons_data'][i][
                                    'season']
                                break

                SeriesEpisodesPerSeason.query.filter_by(
                    media_id=media.id).delete()
                db.session.commit()

                for seas in data['seasons_data']:
                    season = SeriesEpisodesPerSeason(media_id=element.id,
                                                     season=seas['season'],
                                                     episodes=seas['episodes'])
                    db.session.add(season)
                db.session.commit()
            elif list_type == ListType.ANIME:
                users_list = AnimeList.query.filter_by(media_id=media.id).all()

                for user in users_list:
                    episodes_watched = user.total

                    count = 0
                    for i in range(0, len(data['seasons_data'])):
                        count += data['seasons_data'][i]['episodes']
                        if count == episodes_watched:
                            user.last_episode_watched = data['seasons_data'][
                                i]['episodes']
                            user.current_season = data['seasons_data'][i][
                                'season']
                            break
                        elif count > episodes_watched:
                            user.last_episode_watched = data['seasons_data'][
                                i]['episodes'] - (count - episodes_watched)
                            user.current_season = data['seasons_data'][i][
                                'season']
                            break
                        elif count < episodes_watched:
                            try:
                                data['seasons_data'][i + 1]['season']
                            except IndexError:
                                user.last_episode_watched = data[
                                    'seasons_data'][i]['episodes']
                                user.current_season = data['seasons_data'][i][
                                    'season']
                                break

                AnimeEpisodesPerSeason.query.filter_by(
                    media_id=media.id).delete()
                db.session.commit()

                for seas in data['seasons_data']:
                    season = AnimeEpisodesPerSeason(media_id=element.id,
                                                    season=seas['season'],
                                                    episodes=seas['episodes'])
                    db.session.add(season)
                db.session.commit()

    return True
Example #9
0
def media_sheet(media_type, media_id):
    # Check if <media_type> is valid
    try:
        media_type = MediaType(media_type)
        models = get_models_group(media_type)
        list_type = ListType(media_type.value.lower() + 'list')
    except ValueError:
        abort(400)

    # Check if <media_id> came from an API
    from_api = request.args.get('search')

    # Check <media> in local DB
    search = {'id': media_id}
    if from_api:
        search = {'api_id': media_id}
    media = models[0].query.filter_by(**search).first()

    # If not <media> and <api_id>: Add <media> to DB, else abort.
    if not media:
        if from_api:
            API_model = ApiData.get_API_model(media_type)
            try:
                media = API_model(API_id=media_id).save_media_to_db()
                db.session.commit()
            except Exception as e:
                flash(
                    'Sorry, a problem occured trying to load the media info. Please try again later.',
                    'warning')
                app.logger.error(
                    '[ERROR] - Occured trying to add media ({}) ID [{}] to DB: {}'
                    .format(list_type.value, media_id, e))
                location = request.referrer or '/'
                return redirect(location)
        else:
            abort(400)

    # If <media> and <api_id>: redirect for URL with media.id instead of media.api_id
    if media and from_api:
        return redirect(
            url_for('main.media_sheet',
                    media_type=media_type.value,
                    media_id=media.id))

    # Get the list info of the user on this media
    list_info = media.get_user_list_info()

    # Get the HTML template
    template = models[0].media_sheet_template()

    # Get the history of the media for the user
    media_updates = UserLastUpdate.query.filter(UserLastUpdate.user_id == current_user.id,
                                                UserLastUpdate.media_type == list_type,
                                                UserLastUpdate.media_id == media_id)\
        .order_by(UserLastUpdate.date.desc()).all()
    history = current_user._shape_to_dict_updates(media_updates)

    # Get the Genre form for books
    form = GenreForm()
    form_cover = CoverForm()
    genres = None
    if list_type == ListType.BOOKS:
        genres = db.session.query(func.group_concat(BooksGenre.genre.distinct())) \
            .filter(BooksGenre.media_id == media_id).first()

    return render_template(template,
                           title=media.name,
                           media=media,
                           list_info=list_info,
                           form=form,
                           genres=genres,
                           media_list=list_type.value,
                           form_cover=form_cover,
                           media_updates=history)
Example #10
0
def automatic_media_refresh():
    app.logger.info(
        '###################################################################')
    app.logger.info('[SYSTEM] - Starting automatic media refresh')

    # Recover all the data
    all_series_tmdb_id = [
        m.themoviedb_id
        for m in Series.query.filter(Series.lock_status != True)
    ]
    all_anime_tmdb_id = [
        m.themoviedb_id for m in Anime.query.filter(Anime.lock_status != True)
    ]
    all_movies_tmdb_id = [
        m.themoviedb_id
        for m in Movies.query.filter(Movies.lock_status != True)
    ]

    # Recover from API all the changed <TV_show> ID
    try:
        all_id_tv_changes = ApiData().get_changed_data(
            list_type=ListType.SERIES)
    except Exception as e:
        app.logger.error(
            '[ERROR] - Requesting the changed data from TMDB API: {}'.format(
                e))
        return

    # Recover from API all the changed <Movies> ID
    try:
        all_id_movies_changes = ApiData().get_changed_data(
            list_type=ListType.MOVIES)
    except Exception as e:
        app.logger.error(
            '[ERROR] - Requesting the changed data from TMDB API: {}'.format(
                e))
        return

    # Refresh Series
    for element in all_id_tv_changes['results']:
        if element['id'] in all_series_tmdb_id:
            try:
                a = refresh_element_data(element['id'], ListType.SERIES)
                if not a:
                    raise
                app.logger.info(
                    '[INFO] - Refreshed Series with TMDB ID: [{}]'.format(
                        element['id']))
            except Exception as e:
                app.logger.error('[ERROR] - While refreshing: {}'.format(e))

    # Refresh Anime
    for element in all_id_tv_changes["results"]:
        if element["id"] in all_anime_tmdb_id:
            try:
                refresh_element_data(element["id"], ListType.ANIME)
                app.logger.info(
                    '[INFO] - Refreshed Anime with TMDB ID: [{}]'.format(
                        element['id']))
            except Exception as e:
                app.logger.error('[ERROR] - While refreshing: {}'.format(e))

    # Refresh movies
    for element in all_id_movies_changes["results"]:
        if element["id"] in all_movies_tmdb_id:
            try:
                refresh_element_data(element["id"], ListType.MOVIES)
                app.logger.info(
                    '[INFO] - Refreshed Movie with TMDB ID: [{}]'.format(
                        element['id']))
            except Exception as e:
                app.logger.error('[ERROR] - While refreshing: {}'.format(e))

    app.logger.info('[SYSTEM] - Automatic refresh completed')
    app.logger.info(
        '###################################################################')
Example #11
0
def refresh_element_data(api_id, list_type):
    media_data = ApiData().get_details_and_credits_data(api_id, list_type)
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        data = MediaDetails(media_data, list_type,
                            updating=True).get_media_details()
        if not data['tv_data']:
            return None
    elif list_type == ListType.MOVIES:
        data = MediaDetails(media_data, list_type,
                            updating=True).get_media_details()
        if not data['movies_data']:
            return None

    # Update the main details for each media
    if list_type == ListType.SERIES:
        Series.query.filter_by(themoviedb_id=api_id).update(data['tv_data'])
    elif list_type == ListType.ANIME:
        Anime.query.filter_by(themoviedb_id=api_id).update(data['tv_data'])
    elif list_type == ListType.MOVIES:
        Movies.query.filter_by(themoviedb_id=api_id).update(
            data['movies_data'])

    # Commit the new changes
    db.session.commit()

    # Check the episodes/seasons
    if list_type != ListType.MOVIES:
        if list_type == ListType.SERIES:
            media = Series.query.filter_by(themoviedb_id=api_id).first()
            old_seas_eps = [
                n.episodes for n in SeriesEpisodesPerSeason.query.filter_by(
                    media_id=media.id).all()
            ]
        elif list_type == ListType.ANIME:
            media = Anime.query.filter_by(themoviedb_id=api_id).first()
            old_seas_eps = [
                n.episodes for n in AnimeEpisodesPerSeason.query.filter_by(
                    media_id=media.id).all()
            ]

        new_seas_eps = [d['episodes'] for d in data['seasons_data']]

        if new_seas_eps != old_seas_eps:
            if list_type == ListType.SERIES:
                users_list = SeriesList.query.filter_by(
                    media_id=media.id).all()

                for user in users_list:
                    episodes_watched = user.eps_watched

                    count = 0
                    for i in range(0, len(data['seasons_data'])):
                        count += data['seasons_data'][i]['episodes']
                        if count == episodes_watched:
                            user.last_episode_watched = data['seasons_data'][
                                i]['episodes']
                            user.current_season = data['seasons_data'][i][
                                'season']
                            break
                        elif count > episodes_watched:
                            user.last_episode_watched = data['seasons_data'][
                                i]['episodes'] - (count - episodes_watched)
                            user.current_season = data['seasons_data'][i][
                                'season']
                            break
                        elif count < episodes_watched:
                            try:
                                data['seasons_data'][i + 1]['season']
                            except IndexError:
                                user.last_episode_watched = data[
                                    'seasons_data'][i]['episodes']
                                user.current_season = data['seasons_data'][i][
                                    'season']
                                break

                SeriesEpisodesPerSeason.query.filter_by(
                    media_id=media.id).delete()
                db.session.commit()

                for seas in data['seasons_data']:
                    season = SeriesEpisodesPerSeason(media_id=media.id,
                                                     season=seas['season'],
                                                     episodes=seas['episodes'])
                    db.session.add(season)
                db.session.commit()
            elif list_type == ListType.ANIME:
                users_list = AnimeList.query.filter_by(media_id=media.id).all()

                for user in users_list:
                    episodes_watched = user.eps_watched

                    count = 0
                    for i in range(0, len(data['seasons_data'])):
                        count += data['seasons_data'][i]['episodes']
                        if count == episodes_watched:
                            user.last_episode_watched = data['seasons_data'][
                                i]['episodes']
                            user.current_season = data['seasons_data'][i][
                                'season']
                            break
                        elif count > episodes_watched:
                            user.last_episode_watched = data['seasons_data'][
                                i]['episodes'] - (count - episodes_watched)
                            user.current_season = data['seasons_data'][i][
                                'season']
                            break
                        elif count < episodes_watched:
                            try:
                                data['seasons_data'][i + 1]['season']
                            except IndexError:
                                user.last_episode_watched = data[
                                    'seasons_data'][i]['episodes']
                                user.current_season = data['seasons_data'][i][
                                    'season']
                                break

                AnimeEpisodesPerSeason.query.filter_by(
                    media_id=media.id).delete()
                db.session.commit()

                for seas in data['seasons_data']:
                    season = AnimeEpisodesPerSeason(media_id=media.id,
                                                    season=seas['season'],
                                                    episodes=seas['episodes'])
                    db.session.add(season)
                db.session.commit()

    return True