Exemple #1
0
    def delete(self, show_id, ep_id, session):
        """ Deletes all episodes releases by show ID and episode ID """
        try:
            series.show_by_id(show_id, session=session)
        except NoResultFound:
            raise NotFoundError('show with ID %s not found' % show_id)
        try:
            episode = series.episode_by_id(ep_id, session)
        except NoResultFound:
            raise NotFoundError('episode with ID %s not found' % ep_id)
        if not series.episode_in_show(show_id, ep_id):
            raise BadRequest('episode with id %s does not belong to show %s' %
                             (ep_id, show_id))

        args = release_delete_parser.parse_args()
        downloaded = args.get('downloaded') is True if args.get(
            'downloaded') is not None else None
        release_items = []
        for release in episode.releases:
            if downloaded and release.downloaded or downloaded is False and not release.downloaded or not downloaded:
                release_items.append(release)

        for release in release_items:
            if args.get('forget'):
                fire_event('forget', release.title)
            series.delete_release_by_id(release.id)
        return success_response(
            'successfully deleted all releases for episode %s from show %s' %
            (ep_id, show_id))
Exemple #2
0
    def delete(self, show_id, season_id, session):
        """ Deletes all season releases by show ID and season ID """
        try:
            db.show_by_id(show_id, session=session)
        except NoResultFound:
            raise NotFoundError('show with ID %s not found' % show_id)
        try:
            season = db.season_by_id(season_id, session)
        except NoResultFound:
            raise NotFoundError('seasons with ID %s not found' % season_id)
        if not db.season_in_show(show_id, season_id):
            raise BadRequest('season with id %s does not belong to show %s' %
                             (season_id, show_id))

        args = release_delete_parser.parse_args()
        downloaded = args.get('downloaded') is True if args.get(
            'downloaded') is not None else None
        release_items = []
        for release in season.releases:
            if (downloaded and release.downloaded
                    or downloaded is False and not release.downloaded
                    or not downloaded):
                release_items.append(release)

        for release in release_items:
            if args.get('forget'):
                fire_event('forget', release.title)
            db.delete_season_release_by_id(release.id)
        return success_response(
            'successfully deleted all releases for season %s from show %s' %
            (season_id, show_id))
Exemple #3
0
    def delete(self, show_id, ep_id, rel_id, session):
        """ Delete episode release by show ID, episode ID and release ID """
        try:
            series.show_by_id(show_id, session=session)
        except NoResultFound:
            raise NotFoundError('show with ID %s not found' % show_id)
        try:
            series.episode_by_id(ep_id, session)
        except NoResultFound:
            raise NotFoundError('episode with ID %s not found' % ep_id)
        try:
            release = series.release_by_id(rel_id, session)
        except NoResultFound:
            raise NotFoundError('release with ID %s not found' % rel_id)
        if not series.episode_in_show(show_id, ep_id):
            raise BadRequest('episode with id %s does not belong to show %s' %
                             (ep_id, show_id))
        if not series.release_in_episode(ep_id, rel_id):
            raise BadRequest('release id %s does not belong to episode %s' %
                             (rel_id, ep_id))
        args = delete_parser.parse_args()
        if args.get('forget'):
            fire_event('forget', release.title)

        series.delete_release_by_id(rel_id)
        return success_response(
            'successfully deleted release %d from episode %d' %
            (rel_id, ep_id))
Exemple #4
0
    def delete(self, show_id, season_id, rel_id, session):
        """ Delete episode release by show ID, season ID and release ID """
        try:
            db.show_by_id(show_id, session=session)
        except NoResultFound:
            raise NotFoundError('show with ID %s not found' % show_id)
        try:
            db.season_by_id(season_id, session)
        except NoResultFound:
            raise NotFoundError('season with ID %s not found' % season_id)
        try:
            release = db.season_release_by_id(rel_id, session)
        except NoResultFound:
            raise NotFoundError('release with ID %s not found' % rel_id)
        if not db.season_in_show(show_id, season_id):
            raise BadRequest('season with id %s does not belong to show %s' %
                             (season_id, show_id))
        if not db.release_in_season(season_id, rel_id):
            raise BadRequest('release id %s does not belong to season %s' %
                             (rel_id, season_id))
        args = delete_parser.parse_args()
        if args.get('forget'):
            fire_event('forget', release.title)

        db.delete_season_release_by_id(rel_id)
        return success_response(
            'successfully deleted release %d from season %d' %
            (rel_id, season_id))
Exemple #5
0
    def get(self, show_id, session):
        """ Get episodes by show ID """
        args = entity_parser.parse_args()

        # Pagination and sorting params
        page = args['page']
        per_page = args['per_page']
        sort_order = args['order']

        # Handle max size limit
        if per_page > 100:
            per_page = 100

        descending = sort_order == 'desc'

        start = per_page * (page - 1)
        stop = start + per_page

        kwargs = {
            'start': start,
            'stop': stop,
            'descending': descending,
            'session': session
        }

        try:
            show = db.show_by_id(show_id, session=session)
        except NoResultFound:
            raise NotFoundError('show with ID %s not found' % show_id)

        total_items = db.show_episodes(show, count=True, session=session)

        if not total_items:
            return jsonify([])

        episodes = [
            episode.to_dict() for episode in db.show_episodes(show, **kwargs)
        ]

        total_pages = int(ceil(total_items / float(per_page)))

        if total_pages < page and total_pages != 0:
            raise NotFoundError('page %s does not exist' % page)

        # Actual results in page
        actual_size = min(per_page, len(episodes))

        # Get pagination headers
        pagination = pagination_headers(total_pages, total_items, actual_size,
                                        request)

        # Created response
        rsp = jsonify(episodes)

        # Add link header to response
        rsp.headers.extend(pagination)

        # Add series ID header
        rsp.headers.extend({'Series-ID': show_id})
        return rsp
Exemple #6
0
    def put(self, show_id, ep_id, rel_id, session):
        """ Resets a downloaded release status """
        try:
            series.show_by_id(show_id, session=session)
        except NoResultFound:
            raise NotFoundError('show with ID %s not found' % show_id)
        try:
            series.episode_by_id(ep_id, session)
        except NoResultFound:
            raise NotFoundError('episode with ID %s not found' % ep_id)
        try:
            release = series.release_by_id(rel_id, session)
        except NoResultFound:
            raise NotFoundError('release with ID %s not found' % rel_id)
        if not series.episode_in_show(show_id, ep_id):
            raise BadRequest('episode with id %s does not belong to show %s' %
                             (ep_id, show_id))
        if not series.release_in_episode(ep_id, rel_id):
            raise BadRequest('release id %s does not belong to episode %s' %
                             (rel_id, ep_id))

        if not release.downloaded:
            raise BadRequest('release with id %s is not set as downloaded' %
                             rel_id)
        release.downloaded = False

        rsp = jsonify(release.to_dict())
        rsp.headers.extend({'Series-ID': show_id, 'Episode-ID': ep_id})
        return rsp
Exemple #7
0
    def get(self, show_id, ep_id, rel_id, session):
        """ Get episode release by show ID, episode ID and release ID """
        try:
            series.show_by_id(show_id, session=session)
        except NoResultFound:
            raise NotFoundError('show with ID %s not found' % show_id)
        try:
            series.episode_by_id(ep_id, session)
        except NoResultFound:
            raise NotFoundError('episode with ID %s not found' % ep_id)
        try:
            release = series.release_by_id(rel_id, session)
        except NoResultFound:
            raise NotFoundError('release with ID %s not found' % rel_id)
        if not series.episode_in_show(show_id, ep_id):
            raise BadRequest('episode with id %s does not belong to show %s' % (ep_id, show_id))
        if not series.release_in_episode(ep_id, rel_id):
            raise BadRequest('release id %s does not belong to episode %s' % (rel_id, ep_id))

        rsp = jsonify(release.to_dict())
        rsp.headers.extend({
            'Series-ID': show_id,
            'Episode-ID': ep_id
        })
        return rsp
Exemple #8
0
    def get(self, list_id, session=None):
        """ Get entries by list ID """
        try:
            list = db.get_list_by_id(list_id=list_id, session=session)
        except NoResultFound:
            raise NotFoundError('list_id %d does not exist' % list_id)

        args = entries_parser.parse_args()

        # Pagination and sorting params
        page = args['page']
        per_page = args['per_page']
        sort_by = args['sort_by']
        sort_order = args['order']

        # Handle max size limit
        if per_page > 100:
            per_page = 100

        start = per_page * (page - 1)
        stop = start + per_page
        descending = sort_order == 'desc'

        kwargs = {
            'start': start,
            'stop': stop,
            'list_id': list_id,
            'order_by': sort_by,
            'descending': descending,
            'session': session,
        }

        total_items = list.entries.count()

        if not total_items:
            return jsonify([])

        log.debug('entry lists entries count is %d', total_items)
        entries = [entry.to_dict() for entry in db.get_entries_by_list_id(**kwargs)]

        # Total number of pages
        total_pages = int(ceil(total_items / float(per_page)))

        if page > total_pages:
            raise NotFoundError('page %s does not exist' % page)

        # Actual results in page
        actual_size = min(len(entries), per_page)

        # Get pagination headers
        pagination = pagination_headers(total_pages, total_items, actual_size, request)

        # Create response
        rsp = jsonify(entries)

        # Add link header to response
        rsp.headers.extend(pagination)
        return rsp
Exemple #9
0
    def get(self, list_id, session=None):
        """ Get movies by list ID """
        args = movies_parser.parse_args()

        # Pagination and sorting params
        page = args['page']
        per_page = args['per_page']
        sort_by = args['sort_by']
        sort_order = args['order']

        start = per_page * (page - 1)
        stop = start + per_page
        descending = bool(sort_order == 'desc')

        kwargs = {
            'start': start,
            'stop': stop,
            'list_id': list_id,
            'order_by': sort_by,
            'descending': descending,
            'session': session
        }
        try:
            ml.get_list_by_id(list_id=list_id, session=session)
        except NoResultFound:
            raise NotFoundError('list_id %d does not exist' % list_id)

        total_items = ml.get_movies_by_list_id(count=True, **kwargs)

        if not total_items:
            return jsonify([])

        movies = [
            movie.to_dict() for movie in ml.get_movies_by_list_id(**kwargs)
        ]

        total_pages = int(ceil(total_items / float(per_page)))

        if page > total_pages:
            raise NotFoundError('page %s does not exist' % page)

        # Actual results in page
        actual_size = min(len(movies), per_page)

        # Get pagination headers
        pagination = pagination_headers(total_pages, total_items, actual_size,
                                        request)

        # Create response
        rsp = jsonify(movies)

        # Add link header to response
        rsp.headers.extend(pagination)
        return rsp
Exemple #10
0
    def put(self, schedule_id, session=None):
        """ Update schedule """
        new_schedule = request.json

        schedules = self.manager.config.get('schedules', [])

        # Checks for boolean config
        if schedules is True:
            self.manager.config['schedules'] = DEFAULT_SCHEDULES
        elif schedules is False:
            raise Conflict('Schedules are disables in config')

        schedule, idx = _schedule_by_id(schedule_id,
                                        self.manager.config['schedules'])
        if not schedule:
            raise NotFoundError('schedule %d not found' % schedule_id)

        new_schedule['id'] = id(schedule)
        self.manager.config['schedules'][idx] = new_schedule

        self.manager.save_config()
        self.manager.config_changed()
        resp = jsonify(new_schedule)
        resp.status_code = 201
        return resp
Exemple #11
0
    def get(self, session=None):
        """TheTVDB series search"""
        args = search_parser.parse_args()
        language = args['language']

        search_name = args.get('search_name')
        imdb_id = args.get('imdb_id')
        zap2it_id = args.get('zap2it_id')
        force_search = args.get('force_search')

        if not any(arg for arg in [search_name, imdb_id, zap2it_id]):
            raise BadRequest('Not enough lookup arguments')
        kwargs = {
            'search_name': search_name,
            'imdb_id': imdb_id,
            'zap2it_id': zap2it_id,
            'force_search': force_search,
            'session': session,
            'language': language,
        }
        try:
            search_results = search_for_series(**kwargs)
        except LookupError as e:
            raise NotFoundError(e.args[0])
        return jsonify([a.to_dict() for a in search_results])
Exemple #12
0
    def get(self, tvdb_id, session=None):
        args = episode_parser.parse_args()
        language = args['language']

        absolute_number = args.get('absolute_number')
        season_number = args.get('season_number')
        ep_number = args.get('ep_number')
        air_date = args.get('air_date')

        if not ((season_number and ep_number) or absolute_number or air_date):
            raise BadRequest('not enough parameters for lookup. Either season and episode number or absolute number '
                             'are required.')
        kwargs = {'tvdb_id': tvdb_id,
                  'session': session,
                  'language': language}

        if absolute_number:
            kwargs['absolute_number'] = absolute_number
        if season_number and ep_number:
            kwargs['season_number'] = season_number
            kwargs['episode_number'] = ep_number
        if air_date:
            kwargs['first_aired'] = air_date

        try:
            episode = lookup_episode(**kwargs)
        except LookupError as e:
            raise NotFoundError(e.args[0])
        return jsonify(episode.to_dict())
Exemple #13
0
 def get(self, rejected_entry_id, session=None):
     """ Returns a rejected entry """
     try:
         entry = session.query(RememberEntry).filter(RememberEntry.id == rejected_entry_id).one()
     except NoResultFound:
         raise NotFoundError('rejected entry ID %d not found' % rejected_entry_id)
     return jsonify(rejected_entry_to_dict(entry))
Exemple #14
0
    def get(self, session=None):
        """ Get TMDB movie data """
        args = tmdb_parser.parse_args()
        title = args.get('title')
        tmdb_id = args.get('tmdb_id')
        imdb_id = args.get('imdb_id')

        posters = args.pop('include_posters', False)
        backdrops = args.pop('include_backdrops', False)

        if not (title or tmdb_id or imdb_id):
            raise BadRequest(description)

        lookup = plugin.get('api_tmdb', 'tmdb.api').lookup

        try:
            movie = lookup(session=session, **args)
        except LookupError as e:
            raise NotFoundError(e.args[0])

        return_movie = movie.to_dict()

        if posters:
            return_movie['posters'] = [p.to_dict() for p in movie.posters]

        if backdrops:
            return_movie['backdrops'] = [p.to_dict() for p in movie.backdrops]

        return jsonify(return_movie)
Exemple #15
0
 def get(self, list_id, session=None):
     """ Get list by ID """
     try:
         movie_list = ml.get_list_by_id(list_id=list_id, session=session)
     except NoResultFound:
         raise NotFoundError('list_id %d does not exist' % list_id)
     return jsonify(movie_list.to_dict())
Exemple #16
0
 def get(self, entry_id, session=None):
     """Get a pending entry by ID"""
     try:
         entry = db.get_entry_by_id(session, entry_id)
     except NoResultFound:
         raise NotFoundError('No pending entry with ID %s' % entry_id)
     return jsonify(entry.to_dict())
Exemple #17
0
    def get(self, session=None):
        """ List all rejected entries """
        args = rejected_parser.parse_args()

        # Pagination and sorting params
        page = args['page']
        per_page = args['per_page']
        sort_by = args['sort_by']
        sort_order = args['order']

        # Handle max size limit
        if per_page > 100:
            per_page = 100

        descending = sort_order == 'desc'

        # Handle max size limit
        if per_page > 100:
            per_page = 100

        start = per_page * (page - 1)
        stop = start + per_page

        kwargs = {
            'start': start,
            'stop': stop,
            'descending': descending,
            'sort_by': sort_by,
            'session': session,
        }

        total_items = db.get_rejected(session, count=True)

        if not total_items:
            return jsonify([])

        failed_entries = [
            rejected_entry_to_dict(reject)
            for reject in db.get_rejected(**kwargs)
        ]

        total_pages = int(ceil(total_items / float(per_page)))

        if page > total_pages:
            raise NotFoundError('page %s does not exist' % page)

        # Actual results in page
        actual_size = min(per_page, len(failed_entries))

        # Get pagination headers
        pagination = pagination_headers(total_pages, total_items, actual_size,
                                        request)

        # Created response
        rsp = jsonify(failed_entries)

        # Add link header to response
        rsp.headers.extend(pagination)

        return rsp
Exemple #18
0
    def get(self, title, session=None):
        args = series_parser.parse_args()
        language = args['language']

        try:
            tvdb_id = int(title)
        except ValueError:
            tvdb_id = None

        kwargs = {'session': session,
                  'language': language}

        if tvdb_id:
            kwargs['tvdb_id'] = tvdb_id
        else:
            kwargs['name'] = title

        try:
            series = lookup_series(**kwargs)
        except LookupError as e:
            raise NotFoundError(e.args[0])

        result = series.to_dict()
        if args.get('include_actors'):
            result['actors'] = series.actors
        return jsonify(result)
Exemple #19
0
 def delete(self, list_id, session=None):
     """ Delete pending list by ID """
     try:
         delete_list_by_id(list_id=list_id, session=session)
     except NoResultFound:
         raise NotFoundError('list_id %d does not exist' % list_id)
     return success_response('list successfully deleted')
Exemple #20
0
 def post(self, list_id, session=None):
     """ Add movies to list by ID """
     try:
         ml.get_list_by_id(list_id=list_id, session=session)
     except NoResultFound:
         raise NotFoundError('list_id %d does not exist' % list_id)
     data = request.json
     movie_identifiers = data.get('movie_identifiers', [])
     # Validates ID type based on allowed ID
     for id_name in movie_identifiers:
         if list(id_name)[0] not in MovieListBase().supported_ids:
             raise BadRequest('movie identifier %s is not allowed' %
                              id_name)
     title, year = data['movie_name'], data.get('movie_year')
     movie = ml.get_movie_by_title_and_year(list_id=list_id,
                                            title=title,
                                            year=year,
                                            session=session)
     if movie:
         raise Conflict('movie with name "%s" already exist in list %d' %
                        (title, list_id))
     movie = ml.MovieListMovie()
     movie.title = title
     movie.year = year
     movie.ids = ml.get_db_movie_identifiers(
         identifier_list=movie_identifiers, session=session)
     movie.list_id = list_id
     session.add(movie)
     session.commit()
     response = jsonify(movie.to_dict())
     response.status_code = 201
     return response
Exemple #21
0
 def delete(self, list_id, session=None):
     """ Delete list by ID """
     try:
         movie_list = ml.get_list_by_id(list_id=list_id, session=session)
     except NoResultFound:
         raise NotFoundError('list_id %d does not exist' % list_id)
     session.delete(movie_list)
     return success_response('successfully deleted list')
Exemple #22
0
    def get(self, list_id, entry_id, session=None):
        """ Get an entry by list ID and entry ID """
        try:
            entry = db.get_entry_by_id(list_id=list_id, entry_id=entry_id, session=session)
        except NoResultFound:
            raise NotFoundError('could not find entry with id %d in list %d' % (entry_id, list_id))

        return jsonify(entry.to_dict())
Exemple #23
0
 def get(self, path: str, session: Session = None) -> Response:
     """Get schema definition"""
     try:
         schema = resolve_ref(request.full_path)
     except RefResolutionError:
         raise NotFoundError('invalid schema path')
     schema['id'] = request.url
     return jsonify(rewrite_refs(schema, request.url_root))
Exemple #24
0
 def delete(self, rejected_entry_id, session=None):
     """ Deletes a rejected entry """
     try:
         entry = session.query(RememberEntry).filter(RememberEntry.id == rejected_entry_id).one()
     except NoResultFound:
         raise NotFoundError('rejected entry ID %d not found' % rejected_entry_id)
     session.delete(entry)
     return success_response('successfully deleted rejected entry %i' % rejected_entry_id)
Exemple #25
0
 def delete(self, entry_id, session=None):
     """Delete a pending entry"""
     try:
         entry = db.get_entry_by_id(session, entry_id)
     except NoResultFound:
         raise NotFoundError('No pending entry with ID %s' % entry_id)
     session.delete(entry)
     return success_response('successfully deleted entry with ID %s' % entry_id)
Exemple #26
0
 def get(self, seen_entry_id, session):
     """ Get seen entry by ID """
     try:
         seen_entry = db.get_entry_by_id(seen_entry_id, session=session)
     except NoResultFound:
         raise NotFoundError(
             'Could not find entry ID {0}'.format(seen_entry_id))
     return jsonify(seen_entry.to_dict())
Exemple #27
0
    def delete(self, show_id, ep_id, session):
        """ Forgets episode by show ID and episode ID """
        try:
            show = series.show_by_id(show_id, session=session)
        except NoResultFound:
            raise NotFoundError('show with ID %s not found' % show_id)
        try:
            episode = series.episode_by_id(ep_id, session)
        except NoResultFound:
            raise NotFoundError('episode with ID %s not found' % ep_id)
        if not series.episode_in_show(show_id, ep_id):
            raise BadRequest('episode with id %s does not belong to show %s' % (ep_id, show_id))

        args = delete_parser.parse_args()
        series.remove_series_entity(show.name, episode.identifier, args.get('forget'))

        return success_response('successfully removed episode %s from show %s' % (ep_id, show_id))
Exemple #28
0
    def get(self, session=None):
        """ List of previously accepted entries """
        args = history_parser.parse_args()

        # Pagination and sorting params
        page = args['page']
        per_page = args['per_page']
        sort_by = args['sort_by']
        sort_order = args['order']

        # Hard limit results per page to 100
        if per_page > 100:
            per_page = 100

        # Filter param
        task = args['task']

        # Build query
        query = session.query(db.History)
        if task:
            query = query.filter(db.History.task == task)

        total_items = query.count()

        if not total_items:
            return jsonify([])

        total_pages = int(ceil(total_items / float(per_page)))

        if page > total_pages:
            raise NotFoundError('page %s does not exist' % page)

        start = (page - 1) * per_page
        finish = start + per_page

        # Choose sorting order
        order = desc if sort_order == 'desc' else asc

        # Get items
        try:
            items = query.order_by(order(getattr(db.History, sort_by))).slice(
                start, finish)
        except AttributeError as e:
            raise BadRequest(str(e))

        # Actual results in page
        actual_size = min(items.count(), per_page)

        # Get pagination headers
        pagination = pagination_headers(total_pages, total_items, actual_size,
                                        request)

        # Create response
        rsp = jsonify([item.to_dict() for item in items])

        # Add link header to response
        rsp.headers.extend(pagination)
        return rsp
Exemple #29
0
    def get(self, show_id, ep_id, session):
        """ Get episode by show ID and episode ID"""
        try:
            series.show_by_id(show_id, session=session)
        except NoResultFound:
            raise NotFoundError('show with ID %s not found' % show_id)
        try:
            episode = series.episode_by_id(ep_id, session)
        except NoResultFound:
            raise NotFoundError('episode with ID %s not found' % ep_id)
        if not series.episode_in_show(show_id, ep_id):
            raise BadRequest('episode with id %s does not belong to show %s' % (ep_id, show_id))

        rsp = jsonify(episode.to_dict())

        # Add Series-ID header
        rsp.headers.extend({'Series-ID': show_id})
        return rsp
Exemple #30
0
    def get(self, task, session: Session = None) -> Response:
        """ Get task config """
        if task not in self.manager.user_config.get('tasks', {}):
            raise NotFoundError(f'task `{task}` not found')

        return jsonify({
            'name': task,
            'config': self.manager.user_config['tasks'][task]
        })