Exemplo n.º 1
0
    def get(self, request):
        nefarious_settings = NefariousSettings.get()
        transmission_client = get_transmission_client(nefarious_settings)

        watch_movies = request.query_params.getlist('watch_movies', [])
        watch_tv_shows = request.query_params.getlist('watch_tv_shows', [])

        results = []
        querysets = []

        # movies
        if watch_movies:
            querysets.append(WatchMovie.objects.filter(id__in=watch_movies))
        # tv shows
        if watch_tv_shows:
            querysets.append(
                WatchTVEpisode.objects.filter(
                    watch_tv_show__id__in=watch_tv_shows))
            querysets.append(
                WatchTVSeason.objects.filter(
                    watch_tv_show__id__in=watch_tv_shows))

        for qs in querysets:

            for media in qs:

                if isinstance(media, WatchTVSeason):
                    media_serializer = WatchTVSeasonSerializer
                elif isinstance(media, WatchTVEpisode):
                    media_serializer = WatchTVEpisodeSerializer
                else:
                    media_serializer = WatchMovieSerializer

                result = {
                    'watchMedia': media_serializer(media).data,
                }

                if media.transmission_torrent_hash:

                    try:
                        torrent = transmission_client.get_torrent(
                            media.transmission_torrent_hash)
                    except (KeyError, ValueError
                            ):  # torrent no longer exists or was invalid
                        pass
                    except Exception as e:
                        logger_foreground.error(str(e))
                        raise e
                    else:
                        result['torrent'] = TransmissionTorrentSerializer(
                            torrent).data

                results.append(result)

        return Response(results)
Exemplo n.º 2
0
    def get(self, request):
        nefarious_settings = NefariousSettings.get()
        transmission_client = get_transmission_client(nefarious_settings)

        watch_movies = request.query_params.getlist('watch_movies')
        watch_tv_episodes = request.query_params.getlist('watch_tv_episodes')
        watch_tv_seasons = request.query_params.getlist('watch_tv_seasons')

        querysets = []
        torrents = []
        torrent_hashes = []

        # movies
        if watch_movies:
            querysets.append(WatchMovie.objects.filter(id__in=watch_movies))
        # tv episodes
        if watch_tv_episodes:
            querysets.append(
                WatchTVEpisode.objects.filter(id__in=watch_tv_episodes))
        # tv seasons
        if watch_tv_seasons:
            querysets.append(
                WatchTVSeason.objects.filter(id__in=watch_tv_seasons))

        for query in querysets:
            torrent_hashes += [
                media.transmission_torrent_hash for media in query
                if media.transmission_torrent_hash
            ]

        for torrent_hash in torrent_hashes:

            try:
                torrent = transmission_client.get_torrent(torrent_hash)
                torrents.append(torrent)
            except (KeyError,
                    ValueError):  # torrent no longer exists or was invalid
                continue
            except Exception as e:
                logging.error(str(e))
                raise e

        return Response(
            TransmissionTorrentSerializer(torrents, many=True).data)