Exemple #1
0
    def get(self, show_id, ep_id, session):
        """ Get all episodes releases by show ID and episode ID """
        try:
            show = series.show_by_id(show_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'Show with ID %s not found' % show_id
                    }, 404
        try:
            episode = series.episode_by_id(ep_id, session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'Episode with ID %s not found' % ep_id
                    }, 414
        if not series.episode_in_show(show_id, ep_id):
            return {'status': 'error',
                    'message': 'Episode with id %s does not belong to show %s' % (ep_id, show_id)}, 400
        args = release_list_parser.parse_args()
        downloaded = args['downloaded']
        release_items = []
        for release in episode.releases:
            if (downloaded == 'downloaded' and release.downloaded) or \
                    (downloaded == 'not_downloaded' and not release.downloaded) or \
                            downloaded == 'all':
                release_items.append(get_release_details(release))

        return jsonify({
            'releases': release_items,
            'number_of_releases': len(release_items),
            'episode_id': ep_id,
            'show_id': show_id

        })
Exemple #2
0
    def get(self, show_id, ep_id, rel_id, session):
        ''' Get episode release by show ID, episode ID and release ID '''
        try:
            show = series.show_by_id(show_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'Show with ID %s not found' % show_id
                    }, 404
        try:
            episode = series.episode_by_id(ep_id, session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'Episode with ID %s not found' % ep_id
                    }, 414
        try:
            release = series.release_by_id(rel_id, session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'Release with ID %s not found' % rel_id
                    }, 424
        if not series.episode_in_show(show_id, ep_id):
            return {'status': 'error',
                    'message': 'Episode with id %s does not belong to show %s' % (ep_id, show_id)}, 400
        if not series.release_in_episode(ep_id, rel_id):
            return {'status': 'error',
                    'message': 'Release with id %s does not belong to episode %s' % (rel_id, ep_id)}, 410

        return jsonify({
            'show': show.name,
            'show_id': show_id,
            'episode_id': ep_id,
            'release': get_release_details(release)
        })
Exemple #3
0
    def get(self, session=None):
        """ List existing shows """
        args = series_list_parser.parse_args()
        page = args['page']
        max_results = args['max']

        sort_by = args['sort_by']
        order = args['order']
        # In case the default 'desc' order was received
        if order == 'desc':
            order = True

        kwargs = {
            'configured': args.get('in_config'),
            'premieres': args.get('premieres'),
            'status': args.get('status'),
            'days': args.get('days'),
            'session': session

        }

        series_list = series.get_series_summary(**kwargs)
        series_list = series_list.order_by(series.Series.name)

        num_of_shows = series_list.count()
        pages = int(ceil(num_of_shows / float(max_results)))

        shows = sorted_show_list = []
        if page > pages and pages != 0:
            return {'error': 'page %s does not exist' % page}, 400

        start = (page - 1) * max_results
        finish = start + max_results
        if finish > num_of_shows:
            finish = num_of_shows

        for show_number in range(start, finish):
            shows.append(get_series_details(series_list[show_number]))

        if sort_by == 'show_name':
            sorted_show_list = sorted(shows, key=lambda show: show['show_name'], reverse=order)
        elif sort_by == 'episodes_behind_latest':
            sorted_show_list = sorted(shows,
                                      key=lambda show: show['latest_downloaded_episode']['number_of_episodes_behind'],
                                      reverse=order)
        elif sort_by == 'last_download_date':
            sorted_show_list = sorted(shows,
                                      key=lambda show: show['latest_downloaded_episode']['last_downloaded_release'][
                                          'release_first_seen'] if show['latest_downloaded_episode'][
                                          'last_downloaded_release'] else datetime.datetime(1970, 1, 1),
                                      reverse=order)

        return jsonify({
            'shows': sorted_show_list,
            'number_of_shows': num_of_shows,
            'page': page,
            'total_number_of_pages': pages
        })
Exemple #4
0
    def get(self, name, session):
        """ List of shows matching lookup name """
        name = series.normalize_series_name(name)
        matches = series.shows_by_name(name, session=session)

        shows = []
        for match in matches:
            shows.append(get_series_details(match))

        return jsonify({
            'shows': shows,
            'number_of_shows': len(shows)
        })
Exemple #5
0
    def get(self, show_id, session):
        """ Get episodes by show ID """
        try:
            show = series.show_by_id(show_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'Show with ID %s not found' % show_id
                    }, 404
        episodes = [get_episode_details(episode) for episode in show.episodes]

        return jsonify({'show': show.name,
                        'show_id': show_id,
                        'number_of_episodes': len(episodes),
                        'episodes': episodes})
Exemple #6
0
    def get(self, show_id, session):
        """ Get show details by ID """
        try:
            show = series.show_by_id(show_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'Show with ID %s not found' % show_id
                    }, 404

        show = get_series_details(show)

        return jsonify({
            'show': show
        })
Exemple #7
0
 def put(self, show_id, session):
     """ Set the initial episode of an existing show """
     try:
         show = series.show_by_id(show_id, session=session)
     except NoResultFound:
         return {'status': 'error',
                 'message': 'Show with ID %s not found' % show_id
                 }, 404
     data = request.json
     ep_id = data.get('episode_identifier')
     try:
         series.set_series_begin(show, ep_id)
     except ValueError as e:
         return {'status': 'error',
                 'message': e.args[0]
                 }, 400
     return jsonify({'status': 'success',
             'message': 'Episodes will be accepted starting with `%s`' % ep_id,
             'show': get_series_details(show)
             })
Exemple #8
0
 def get(self, show_id, ep_id, session):
     """ Get episode by show ID and episode ID"""
     try:
         show = series.show_by_id(show_id, session=session)
     except NoResultFound:
         return {'status': 'error',
                 'message': 'Show with ID %s not found' % show_id
                 }, 404
     try:
         episode = series.episode_by_id(ep_id, session)
     except NoResultFound:
         return {'status': 'error',
                 'message': 'Episode with ID %s not found' % ep_id
                 }, 414
     if not series.episode_in_show(show_id, ep_id):
         return {'status': 'error',
                 'message': 'Episode with id %s does not belong to show %s' % (ep_id, show_id)}, 400
     return jsonify({
         'show': show.name,
         'show_id': show_id,
         'episode': get_episode_details(episode)
     })
Exemple #9
0
 def post(self, name, session):
     """ Create a new show and set its first accepted episode """
     normalized_name = series.normalize_series_name(name)
     matches = series.shows_by_exact_name(normalized_name, session=session)
     if matches:
         return {'status': 'error',
                 'message': 'Show `%s` already exist in DB' % name
                 }, 500
     show = series.Series()
     show.name = name
     session.add(show)
     data = request.json
     ep_id = data.get('episode_identifier')
     try:
         series.set_series_begin(show, ep_id)
     except ValueError as e:
         return {'status': 'error',
                 'message': e.args[0]
                 }, 400
     return jsonify({'status': 'success',
             'message': 'Successfully added series `%s` and set first accepted episode to `%s`' % (
                 show.name, ep_id),
             'show': get_series_details(show)
             })