Exemplo n.º 1
0
def get_settings(session, user_id: int):
    """
    Get a list of settings for the user who's id is user_id.

    :param session: the db session.
    :param user_id: the id of the user.
    :return: a list of settings for the user who's id is user_id.
    """

    # Get user
    user = db_calls.get_user_id(session, user_id)

    # Check if the user was found
    if user is None:
        return {}

    # Get excluded channel list
    excluded_channel_list = db_calls.get_user_excluded_channels(
        session, user_id)
    current_excluded_channel_list = []

    for excluded_channel in excluded_channel_list:
        current_excluded_channel_list.append(excluded_channel.channel_id)

    return {
        'include_adult_channels': user.show_adult,
        'language': user.language,
        'excluded_channel_list': current_excluded_channel_list
    }
Exemplo n.º 2
0
def process_alarms(session: sqlalchemy.orm.Session):
    """
    Process the alarms that exist in the DB.

    :param session: the db session.
    """

    alarms = db_calls.get_alarms(session)

    for a in alarms:
        user = db_calls.get_user_id(session, a.user_id)
        search_adult = user.show_adult if user is not None else False

        if a.alarm_type == response_models.AlarmType.LISTINGS.value:
            titles = [a.show_name]

            db_shows = []
        else:
            titles = get_show_titles(session, a.trakt_id, a.is_movie)

            db_shows = search_sessions_db_with_tmdb_id(
                session,
                a.trakt_id,
                a.is_movie,
                only_new=True,
                show_season=a.show_season,
                show_episode=a.show_episode,
                use_excluded_channels=True,
                user_id=user.id)

        db_shows += search_sessions_db(session,
                                       titles,
                                       a.is_movie,
                                       complete_title=True,
                                       only_new=True,
                                       show_season=a.show_season,
                                       show_episode=a.show_episode,
                                       search_adult=search_adult,
                                       use_excluded_channels=True,
                                       user_id=user.id,
                                       ignore_with_tmdb_id=True)

        if len(db_shows) > 0:
            process_emails.set_language(user.language)
            process_emails.send_alarms_email(user.email, db_shows)

    # Update the datetime of the last processing of the alarms
    last_update = db_calls.get_last_update(session)
    last_update.alarms_datetime = datetime.datetime.utcnow()
    db_calls.commit(session)
Exemplo n.º 3
0
def delete_old_sessions(db_session: sqlalchemy.orm.Session, start_datetime: datetime.datetime,
                        end_datetime: datetime.datetime, channels: List[str]) -> int:
    """
    Delete sessions that no longer exist.
    Send emails to the users whose reminders are associated with such sessions.

    :param db_session: the DB session.
    :param start_datetime: the start of the interval of interest.
    :param end_datetime: the end of the interval of interest.
    :param channels: the set of channels.
    :return: the number of deleted sessions.
    """

    nb_deleted_sessions = 0

    # Get the old show sessions
    old_sessions = db_calls.search_old_sessions(db_session, start_datetime, end_datetime, channels)

    for s in old_sessions:
        nb_deleted_sessions += 1

        # Get the reminders associated with this session
        reminders = db_calls.get_reminders_session(db_session, s.id)

        if len(reminders) != 0:
            # Get the session
            show_session = db_calls.get_show_session_complete(db_session, s.id)
            show_result = response_models.LocalShowResult.create_from_show_session(show_session[0], show_session[1],
                                                                                   show_session[2])

            # Warn all users with the reminders for this session
            for r in reminders:
                user = db_calls.get_user_id(db_session, r.user_id)

                process_emails.send_deleted_sessions_email(user.email, [show_result])

                # Delete the reminder
                db_session.delete(r)

            # Commit to ensure there are no more references to the session
            db_calls.commit(db_session)

        # Delete the session
        db_session.delete(s)

    db_session.commit()

    return nb_deleted_sessions
Exemplo n.º 4
0
    def get(self, args):
        """Get search results for the search_text, using the Trakt API."""

        search_text = args['search_text']
        language = args['language']
        is_movie = None

        for k, v in args.items():
            if v is None:
                continue

            if k == 'is_movie':
                is_movie = v

        if len(search_text) < 2:
            return flask.make_response('Search Text Too Small', 400)

        with session_scope() as session:
            search_adult = False

            # Get the user settings of whether it should look in channels with adult content or not
            if 'HTTP_AUTHORIZATION' in flask.request.headers.environ:
                token = flask.request.headers.environ['HTTP_AUTHORIZATION'][7:]
                user_id = authentication.get_token_field(
                    token.encode(), 'user')

                user = db_calls.get_user_id(session, user_id)
                search_adult = user.show_adult if user is not None else False

            if search_text[0] == '"' and search_text[-1] == '"':
                search_text = search_text[1:-1]
                exact_name = True
            else:
                exact_name = False

            more_results, shows = processing.search_show_information(
                session, search_text, is_movie, language, search_adult,
                exact_name)

            response_dict = {'show_list': shows}

            # Add a remark when there are more results than those on the response
            if more_results and not exact_name:
                response_dict['remark'] = 'Incomplete List'

            return flask.make_response(flask.jsonify(response_dict), 200)
Exemplo n.º 5
0
    def put(self, args):
        """Change user's settings, that require the password to be sent."""

        password = args['password']

        new_password = None

        for k, v in args.items():
            if v is None:
                continue

            if k == 'new_password':
                new_password = v

        with session_scope() as session:
            # Get the user id from the token
            token = flask.request.headers.environ['HTTP_AUTHORIZATION'][7:]
            user_id = authentication.get_token_field(token.encode(), 'user')

            user = db_calls.get_user_id(session, user_id)

            # Check if the password is valid
            valid = verify_login_credentials(user.email, password)

            if not valid:
                return flask.make_response('Unauthorized Access',
                                           403)  # Should be 503

            # Update new_password
            if new_password is not None:
                if password == new_password:
                    return flask.make_response('Same Password', 400)

                if processing.change_user_settings(
                        session, {ChangeType.NEW_PASSWORD.value: new_password},
                        user_id):
                    return flask.make_response('', 200)
                else:
                    return flask.make_response('', 400)

            # No parameters
            else:
                return flask.make_response('Missing Parameter', 400)
Exemplo n.º 6
0
def process_reminders(session: sqlalchemy.orm.Session) -> None:
    """
    Process the reminders that exist in the DB, sending an email when the session is within the desired time frame.

    :param session: the db session.
    """

    reminders_sessions = db_calls.get_sessions_reminders(session)

    for a_s in reminders_sessions:
        reminder: models.Reminder = a_s[0]
        show_session: models.ShowSession = a_s[1]

        anticipation_hours = int(reminder.anticipation_minutes / 60)

        # Replace the time minutes and seconds so that it can ensure the anticipation hours
        show_time = show_session.date_time.replace(
            minute=0, second=0) - datetime.timedelta(minutes=5)

        # If it is time to fire the reminder
        if datetime.datetime.utcnow() + datetime.timedelta(
                hours=anticipation_hours) > show_time:
            show_session_tuple = db_calls.get_show_session_complete(
                session, show_session.id)
            user = db_calls.get_user_id(session, reminder.user_id)

            local_show_result = response_models.LocalShowResult.create_from_show_session(
                show_session_tuple[0], show_session_tuple[1],
                show_session_tuple[2])

            process_emails.set_language(user.language)
            process_emails.send_reminders_email(user.email,
                                                [local_show_result])

            session.delete(reminder)
            session.commit()
Exemplo n.º 7
0
    def get(self, args):
        """Get search results for the search_text or the show_id, in the listings and streaming services."""

        search_text = None
        show_id = None
        is_movie = None

        for k, v in args.items():
            if v is None:
                continue

            if k == 'search_text':
                search_text = v.strip()

            if k == 'show_id':
                show_id = v

            if k == 'is_movie':
                is_movie = v

        if search_text is None and show_id is None:
            return flask.make_response('Invalid request', 400)

        with session_scope() as session:
            search_adult = False

            # Get the user settings of whether it should look in channels with adult content or not
            if 'HTTP_AUTHORIZATION' in flask.request.headers.environ:
                token = flask.request.headers.environ['HTTP_AUTHORIZATION'][7:]
                user_id = authentication.get_token_field(
                    token.encode(), 'user')

                user = db_calls.get_user_id(session, user_id)
                search_adult = user.show_adult if user is not None else False

            # Check whether it is a request by id or by text
            if show_id is not None:
                if is_movie is None:
                    return flask.make_response('Invalid request', 400)

                titles = processing.get_show_titles(session, show_id, is_movie)

                db_shows = processing.search_sessions_db_with_tmdb_id(
                    session, show_id, is_movie)
            else:
                if len(search_text) < 2:
                    return flask.make_response('Search Text Too Small', 400)

                titles = [search_text]

                db_shows = []

            # If it is a search with id
            # - we only want exact title matches
            # - for those results that don't have a TMDB id
            complete_title = show_id is not None
            ignore_with_tmdb_id = show_id is not None

            # db_shows += processing.search_streaming_services_shows_db(session, titles, is_movie=is_movie,
            #                                                          complete_title=complete_title,
            #                                                          search_adult=search_adult,
            #                                                          ignore_with_tmdb_id=ignore_with_tmdb_id)

            db_shows += processing.search_sessions_db(
                session,
                titles,
                is_movie=is_movie,
                complete_title=complete_title,
                search_adult=search_adult,
                ignore_with_tmdb_id=ignore_with_tmdb_id)

            response_dict = {'show_list': auxiliary.list_to_json(db_shows)}

            show_dict = {}

            # If it is a search by id, add information on the premiere of the show
            if show_id is not None:
                show = db_calls.get_show_data_by_tmdb_id(
                    session, show_id, is_movie)

                if show is not None:
                    if show.premiere_date is not None:
                        show_dict['premiere_date'] = show.premiere_date

                        if show.season_premiere is not None:
                            show_dict['season_premiere'] = show.season_premiere

            response_dict['show'] = show_dict

            return flask.make_response(flask.jsonify(response_dict), 200)