Пример #1
0
def add_id_to_user(user_id, ids_to_add, list_type):
    if list_type == ListType.SERIES:
        query = Series.query.filter(Series.id.in_(ids_to_add)).all()
    elif list_type == ListType.ANIME:
        query = Anime.query.filter(Anime.id.in_(ids_to_add)).all()
    elif list_type == ListType.MOVIES:
        query = Movies.query.filter(Movies.id.in_(ids_to_add)).all()

    data = []
    for media in query:
        if list_type == ListType.SERIES:
            user_list = SeriesList(user_id=user_id,
                                   media_id=media.id,
                                   current_season=len(media.eps_per_season),
                                   last_episode_watched=media.eps_per_season[-1].episodes,
                                   status=Status.COMPLETED,
                                   total=media.total_episodes)
        elif list_type == ListType.ANIME:
            user_list = AnimeList(user_id=user_id,
                                  media_id=media.id,
                                  current_season=len(media.eps_per_season),
                                  last_episode_watched=media.eps_per_season[-1].episodes,
                                  status=Status.COMPLETED,
                                  total=media.total_episodes)
        elif list_type == ListType.MOVIES:
            user_list = MoviesList(user_id=user_id,
                                   media_id=media.id,
                                   status=Status.COMPLETED,
                                   total=1)

        db.session.add(user_list)
        app.logger.info('[User {}] {} Added [ID {}] in the category: Completed'
                        .format(user_id, list_type.value.replace('list', ''), media.id))

        # Set the last update
        set_last_update(media=media, media_type=list_type, new_status=Status.COMPLETED, user_id=user_id)

        # Compute the new time spent
        if list_type == ListType.SERIES or list_type == ListType.ANIME:
            compute_time_spent(media=media, new_watched=media.total_episodes, list_type=list_type, user_id=user_id)
        elif list_type == ListType.MOVIES:
            compute_time_spent(media=media, list_type=list_type, movie_status=Status.COMPLETED, movie_add=True,
                               user_id=user_id)

        if list_type == ListType.SERIES:
            covers_path = "../static/covers/series_covers"
        elif list_type == ListType.ANIME:
            covers_path = "../static/covers/anime_covers"
        elif list_type == ListType.MOVIES:
            covers_path = "../static/covers/movies_covers"

        data.append({'name': media.name,
                     'cover': f'{covers_path}/{media.image_cover}',
                     'type': list_type.value.replace('list', '')})

    db.session.commit()
    return data
Пример #2
0
def update_rewatch():
    try:
        json_data = request.get_json()
        new_rewatch = int(json_data['rewatch'])
        media_id = int(json_data['element_id'])
        media_list = json_data['element_type']
    except:
        return '', 400

    # Check if the <media_list> exist and is valid
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400

    # Check that <new_rewatch> is between [0-10]
    if 0 > new_rewatch > 10:
        return '', 400

    media = check_media(media_id, list_type)
    if not media or (media[1].status != Status.COMPLETED
                     and media[1].status != Status.COMPLETED_ANIMATION):
        return '', 400

    # Get the old data
    old_rewatch = media[1].rewatched

    # Set the new data
    media[1].rewatched = new_rewatch
    app.logger.info('[{}] Series ID {} re-watched {}x times'.format(
        current_user.id, media_id, new_rewatch))

    # total eps/movies watched
    if list_type != ListType.MOVIES:
        media[1].eps_watched = media[0].total_episodes + (
            new_rewatch * media[0].total_episodes)
    elif list_type == ListType.MOVIES:
        media[1].eps_watched = 1 + new_rewatch

    # Commit the changes
    db.session.commit()

    # Compute the new time spent
    if list_type != ListType.MOVIES:
        compute_time_spent(media=media[0],
                           list_type=list_type,
                           old_rewatch=old_rewatch,
                           new_rewatch=new_rewatch)
    elif list_type == ListType.MOVIES:
        compute_time_spent(media=media[0],
                           list_type=list_type,
                           movie_status=media[1].status,
                           old_rewatch=old_rewatch,
                           new_rewatch=new_rewatch)

    return '', 204
Пример #3
0
def delete_element():
    try:
        json_data = request.get_json()
        media_id = int(json_data['delete'])
        media_list = json_data['element_type']
    except:
        return '', 400

    # Check if the <media_list> exist and is valid
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400

    media = check_media(media_id, list_type)
    if media is None:
        return '', 400

    # Get the old data
    old_rewatch = media[1].rewatched

    # Compute the new time spent
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        old_watched = media[1].eps_watched
        compute_time_spent(media=media[0],
                           old_watched=old_watched,
                           list_type=list_type,
                           old_rewatch=old_rewatch)
    elif list_type == ListType.MOVIES:
        compute_time_spent(media=media[0],
                           list_type=list_type,
                           movie_status=media[1].status,
                           movie_delete=True,
                           old_rewatch=old_rewatch)

    # Delete the media from the user's list
    db.session.delete(media[1])
    db.session.commit()
    app.logger.info('[User {}] {} [ID {}] successfully removed.'.format(
        current_user.id, list_type.value.replace('list', ''), media_id))

    return '', 204
Пример #4
0
def add_element():
    try:
        json_data = request.get_json()
        media_id = json_data['element_id']
        media_list = json_data['element_type']
        media_cat = json_data['element_cat']
    except:
        return '', 400

    # Check if the <media_list> exist and is valid
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400

    # Check if <status> is valid compared to <list_type>
    new_status = check_cat_type(list_type, media_cat)
    if not new_status:
        return '', 400

    # Check if the <media>
    media = check_media(media_id, list_type, add=True)
    if media is None:
        return '', 400

    # Setup the <season>, <episode> and <category> of the <media>
    new_watched = 1
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        new_season = 1
        new_episode = 1
        if new_status == Status.COMPLETED:
            new_season = len(media.eps_per_season)
            new_episode = media.eps_per_season[-1].episodes
            new_watched = media.total_episodes
        elif new_status == Status.RANDOM or new_status == Status.PLAN_TO_WATCH:
            new_episode = 0
            new_watched = 0
    elif list_type == ListType.MOVIES:
        if new_status == Status.COMPLETED:
            if MoviesGenre.query.filter_by(media_id=media.id,
                                           genre="Animation").first():
                new_status = Status.COMPLETED_ANIMATION
        else:
            new_watched = 0

    # Add <media> to the <user>
    if list_type == ListType.SERIES:
        user_list = SeriesList(user_id=current_user.id,
                               media_id=media.id,
                               current_season=new_season,
                               last_episode_watched=new_episode,
                               status=new_status,
                               eps_watched=new_watched)
    elif list_type == ListType.ANIME:
        user_list = AnimeList(user_id=current_user.id,
                              media_id=media.id,
                              current_season=new_season,
                              last_episode_watched=new_episode,
                              status=new_status,
                              eps_watched=new_watched)
    elif list_type == ListType.MOVIES:
        user_list = MoviesList(user_id=current_user.id,
                               media_id=media.id,
                               status=new_status,
                               eps_watched=new_watched)

    # Commit the changes
    db.session.add(user_list)
    db.session.commit()
    app.logger.info('[User {}] {} Added [ID {}] in the category: {}'.format(
        current_user.id, list_type.value.replace('list', ''), media_id,
        new_status.value))

    # Set the last update
    set_last_update(media=media, media_type=list_type, new_status=new_status)

    # Compute the new time spent
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        compute_time_spent(media=media,
                           new_watched=new_watched,
                           list_type=list_type)
    elif list_type == ListType.MOVIES:
        compute_time_spent(media=media,
                           list_type=list_type,
                           movie_status=new_status,
                           movie_add=True)

    return '', 204
Пример #5
0
def change_element_category():
    try:
        json_data = request.get_json()
        media_new_cat = json_data['status']
        media_id = int(json_data['element_id'])
        media_list = json_data['element_type']
    except:
        return '', 400

    # Check the <media_list> parameter
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400

    # Check if the <status> parameter
    new_status = check_cat_type(list_type, media_new_cat)
    if not new_status:
        return '', 400

    # Recover the media
    media = check_media(media_id, list_type)
    if media is None:
        return '', 400

    # Get the old status
    old_status = media[1].status

    # Set the new status
    media[1].status = new_status

    # Get the old rewatched
    old_rewatch = media[1].rewatched
    # Reset the rewatched time multiplier
    media[1].rewatched = 0

    # Set and change accordingly <last_episode_watched>, <current_season> and <eps_watched> for anime and series
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        old_watched = media[1].eps_watched
        if new_status == Status.COMPLETED:
            media[1].current_season = len(media[0].eps_per_season)
            media[1].last_episode_watched = media[0].eps_per_season[
                -1].episodes
            media[1].eps_watched = media[0].total_episodes
            new_watched = media[0].total_episodes
        elif new_status == Status.RANDOM or new_status == Status.PLAN_TO_WATCH:
            media[1].current_season = 1
            media[1].last_episode_watched = 0
            media[1].eps_watched = 0
            new_watched = 0
        else:
            new_watched = old_watched
    elif list_type == ListType.MOVIES:
        if new_status == Status.COMPLETED or new_status == Status.COMPLETED_ANIMATION:
            media[1].eps_watched = 1
        else:
            media[1].eps_watched = 0

    # Set the last updates
    set_last_update(media=media[0],
                    media_type=list_type,
                    old_status=old_status,
                    new_status=new_status)

    # Compute the new time spent
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        compute_time_spent(media=media[0],
                           list_type=list_type,
                           old_watched=old_watched,
                           new_watched=new_watched,
                           old_rewatch=old_rewatch)
    elif list_type == ListType.MOVIES:
        compute_time_spent(media=media[0],
                           list_type=list_type,
                           movie_status=media[1].status,
                           old_rewatch=old_rewatch,
                           movie_runtime=media[0].runtime)

    db.session.commit()
    app.logger.info(
        "[User {}] {}'s category [ID {}] changed from {} to {}.".format(
            current_user.id, media_id, list_type.value.replace('list', ''),
            old_status.value, new_status.value))

    return '', 204
Пример #6
0
def update_element_episode():
    try:
        json_data = request.get_json()
        new_episode = int(json_data['episode']) + 1
        media_id = int(json_data['element_id'])
        media_list = json_data['element_type']
    except:
        return '', 400

    # Check if the media_list exist and is valid
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400
    finally:
        if list_type == ListType.MOVIES:
            return '', 400

    # Check if the media exists
    media = check_media(media_id, list_type)
    if not media or media[1].status == Status.RANDOM or media[
            1].status == Status.PLAN_TO_WATCH:
        return '', 400

    # Check if the episode number is between 1 and <last_episode>
    if 1 > new_episode > media[0].eps_per_season[media[1].current_season -
                                                 1].episodes:
        return '', 400

    # Get the old data
    old_season = media[1].current_season
    old_episode = media[1].last_episode_watched
    old_watched = media[1].eps_watched

    # Set the new data
    new_watched = sum(
        [x.episodes
         for x in media[0].eps_per_season[:old_season - 1]]) + new_episode
    media[1].last_episode_watched = new_episode
    media[1].eps_watched = new_watched + (media[1].rewatched *
                                          media[0].total_episodes)
    app.logger.info(
        '[User {}] {} [ID {}] episode updated from {} to {}'.format(
            current_user.id, list_type.value.replace('list', ''), media_id,
            old_episode, new_episode))

    # Commit the changes
    db.session.commit()

    # Set the last updates
    set_last_update(media=media[0],
                    media_type=list_type,
                    old_season=old_season,
                    new_season=old_season,
                    old_episode=old_episode,
                    new_episode=new_episode)

    # Compute the new time spent
    compute_time_spent(media=media[0],
                       old_watched=old_watched,
                       new_watched=new_watched,
                       list_type=list_type)

    return '', 204