Ejemplo n.º 1
0
    def get(self, request):
        media_type = request.query_params.get('media_type', MEDIA_TYPE_TV)
        assert media_type in [MEDIA_TYPE_TV, MEDIA_TYPE_MOVIE]

        nefarious_settings = NefariousSettings.get()

        # prepare query
        tmdb = get_tmdb_client(nefarious_settings)
        query = request.query_params.get('q')

        params = {
            'query': query,
        }

        # search for media
        search = tmdb.Search()

        if media_type == MEDIA_TYPE_MOVIE:
            results = search.movie(**params)
        else:
            results = search.tv(**params)

        return Response(results)
Ejemplo n.º 2
0
    def handle(self, *args, **options):
        nefarious_settings = NefariousSettings.get()
        download_path = settings.INTERNAL_DOWNLOAD_PATH
        tmdb_client = get_tmdb_client(nefarious_settings)
        # use the first super user account to assign media
        user = User.objects.filter(is_superuser=True).first()

        # validate
        if not download_path:
            raise CommandError(
                'INTERNAL_DOWNLOAD_PATH environment variable is not defined')
        elif not os.path.exists(download_path):
            raise CommandError(
                'Path "{}" does not exist'.format(download_path))

        # import
        if options['media_type'] == self.MEDIA_TYPE_TV:
            tv_path = os.path.join(
                download_path, nefarious_settings.transmission_tv_download_dir)
            importer = TVImporter(
                nefarious_settings=nefarious_settings,
                root_path=tv_path,
                tmdb_client=tmdb_client,
                user=user,
            )
            importer.ingest_root(tv_path)
        else:
            movie_path = os.path.join(
                download_path,
                nefarious_settings.transmission_movie_download_dir)
            importer = MovieImporter(
                nefarious_settings=nefarious_settings,
                root_path=movie_path,
                tmdb_client=tmdb_client,
                user=user,
            )
            importer.ingest_root(movie_path)
Ejemplo n.º 3
0
    def get(self, request):
        media_type = request.query_params.get('media_type', MEDIA_TYPE_TV)
        assert media_type in [MEDIA_TYPE_TV, MEDIA_TYPE_MOVIE]

        if 'tmdb_media_id' not in request.query_params:
            raise ValidationError({'tmdb_media_id': ['required parameter']})

        nefarious_settings = NefariousSettings.get()

        params = {
            'language': nefarious_settings.language,
        }

        # prepare query
        tmdb = get_tmdb_client(nefarious_settings)
        tmdb_media_id = request.query_params.get('tmdb_media_id')

        # search for media
        if media_type == MEDIA_TYPE_MOVIE:
            similar_results = tmdb.Movies(id=tmdb_media_id).recommendations(**params)
        else:
            similar_results = tmdb.TV(id=tmdb_media_id).recommendations(**params)

        return Response(similar_results)
Ejemplo n.º 4
0
    def handle(self, *args, **options):

        # create superuser if they don't already exist
        existing_user = User.objects.filter(username=options['username'])
        if not existing_user.exists():
            User.objects.create_superuser(options['username'],
                                          options['email'],
                                          options['password'])
            self.stdout.write(
                self.style.SUCCESS(
                    'Successfully created superuser {}:{} {}'.format(
                        options['username'], options['password'],
                        options['email'])))

        # create settings if they don't already exist
        nefarious_settings, _ = NefariousSettings.objects.get_or_create()

        # populate tmdb configuration if necessary
        if not nefarious_settings.tmdb_configuration or not nefarious_settings.tmdb_languages:
            tmdb_client = get_tmdb_client(nefarious_settings)
            configuration = tmdb_client.Configuration()
            nefarious_settings.tmdb_configuration = configuration.info()
            nefarious_settings.tmdb_languages = configuration.languages()
            nefarious_settings.save()
Ejemplo n.º 5
0
    def post(self, request):
        result = {
            'success': True,
        }
        nefarious_settings = NefariousSettings.get()

        tmdb_media = request.data.get('tmdb_media', {})
        torrent_info = request.data.get('torrent', {})
        torrent_url = torrent_info.get('MagnetUri') or torrent_info.get('Link')

        if not torrent_url:
            return Response({
                'success': False,
                'error': 'Missing torrent link'
            })

        media_type = request.data.get('media_type', MEDIA_TYPE_TV)

        # validate tv
        if media_type == MEDIA_TYPE_TV:
            if 'season_number' not in request.data:
                return Response({
                    'success': False,
                    'error': 'Missing season_number'
                })

        if not is_magnet_url(torrent_url):
            torrent_url = swap_jackett_host(torrent_url, nefarious_settings)

        try:
            torrent_url = trace_torrent_url(torrent_url)
        except Exception as e:
            return Response({
                'success': False,
                'error': 'An unknown error occurred',
                'error_detail': str(e)
            })

        logger_foreground.info('adding torrent: {}'.format(torrent_url))

        # add torrent
        transmission_client = get_transmission_client(nefarious_settings)
        transmission_session = transmission_client.session_stats()

        tmdb = get_tmdb_client(nefarious_settings)

        # set download paths and associate torrent with watch instance
        if media_type == MEDIA_TYPE_MOVIE:
            tmdb_request = tmdb.Movies(tmdb_media['id'])
            tmdb_movie = tmdb_request.info()
            watch_media = WatchMovie(
                user=request.user,
                tmdb_movie_id=tmdb_movie['id'],
                name=tmdb_movie['title'],
                poster_image_url=nefarious_settings.get_tmdb_poster_url(
                    tmdb_movie['poster_path']),
                release_date=parse_date(tmdb_movie['release_date'] or ''),
            )
            watch_media.save()
            download_dir = os.path.join(
                transmission_session.download_dir,
                nefarious_settings.transmission_movie_download_dir.lstrip('/'))
            result['watch_movie'] = WatchMovieSerializer(watch_media).data
        else:
            tmdb_request = tmdb.TV(tmdb_media['id'])
            tmdb_show = tmdb_request.info()

            watch_tv_show, _ = WatchTVShow.objects.get_or_create(
                tmdb_show_id=tmdb_show['id'],
                defaults=dict(
                    user=request.user,
                    name=tmdb_show['name'],
                    poster_image_url=nefarious_settings.get_tmdb_poster_url(
                        tmdb_show['poster_path']),
                ))

            result['watch_tv_show'] = WatchTVShowSerializer(watch_tv_show).data

            # single episode
            if 'episode_number' in request.data:
                tmdb_request = tmdb.TV_Episodes(tmdb_media['id'],
                                                request.data['season_number'],
                                                request.data['episode_number'])
                tmdb_episode = tmdb_request.info()
                watch_media = WatchTVEpisode(
                    user=request.user,
                    watch_tv_show=watch_tv_show,
                    tmdb_episode_id=tmdb_episode['id'],
                    season_number=request.data['season_number'],
                    episode_number=request.data['episode_number'],
                    release_date=parse_date(tmdb_episode['air_date'] or ''))
                watch_media.save()
                result['watch_tv_episode'] = WatchTVEpisodeSerializer(
                    watch_media).data
            # entire season
            else:
                season_result = tmdb.TV_Seasons(tmdb_show['id'],
                                                request.data['season_number'])
                season_data = season_result.info()
                # create the season request
                watch_tv_season_request, _ = WatchTVSeasonRequest.objects.get_or_create(
                    watch_tv_show=watch_tv_show,
                    season_number=request.data['season_number'],
                    defaults=dict(
                        user=request.user,
                        collected=
                        True,  # set collected since we're directly downloading a torrent
                        release_date=parse_date(season_data['air_date']
                                                or '')),
                )
                # create the actual watch season instance
                watch_media = WatchTVSeason(
                    user=request.user,
                    watch_tv_show=watch_tv_show,
                    season_number=request.data['season_number'],
                    release_date=parse_date(season_data['air_date'] or ''))
                watch_media.save()

                # return the season request vs the watch instance
                result[
                    'watch_tv_season_request'] = WatchTVSeasonRequestSerializer(
                        watch_tv_season_request).data

            download_dir = os.path.join(
                transmission_session.download_dir,
                nefarious_settings.transmission_tv_download_dir.lstrip('/'))

        torrent = transmission_client.add_torrent(
            torrent_url,
            paused=settings.DEBUG,
            download_dir=download_dir,
        )
        watch_media.transmission_torrent_hash = torrent.hashString
        watch_media.save()

        return Response(result)
Ejemplo n.º 6
0
def wanted_tv_season_task():
    nefarious_settings = NefariousSettings.get()
    tmdb = get_tmdb_client(nefarious_settings)

    #
    # re-check for requested tv seasons that have had new episodes released from TMDB (which was stale previously)
    #

    for tv_season_request in WatchTVSeasonRequest.objects.filter(
            collected=False):
        season_request = tmdb.TV_Seasons(
            tv_season_request.watch_tv_show.tmdb_show_id,
            tv_season_request.season_number)
        season = season_request.info()

        now = datetime.utcnow()
        last_air_date = parse_date(season.get('air_date')
                                   or '')  # season air date

        # otherwise add any new episodes to our watch list
        for episode in season['episodes']:
            episode_air_date = parse_date(episode.get('air_date') or '')

            # if episode air date exists, use as last air date
            if episode_air_date:
                last_air_date = episode_air_date if not last_air_date or episode_air_date > last_air_date else last_air_date

            try:
                watch_tv_episode, was_created = WatchTVEpisode.objects.get_or_create(
                    tmdb_episode_id=episode['id'],
                    defaults=dict(
                        watch_tv_show=tv_season_request.watch_tv_show,
                        season_number=tv_season_request.season_number,
                        episode_number=episode['episode_number'],
                        user=tv_season_request.user,
                        release_date=episode_air_date,
                    ))
            except IntegrityError as e:
                logger_background.exception(e)
                logger_background.error(
                    'Failed creating tmdb episode {} when show {}, season #{} and episode #{} already exist'
                    .format(episode['id'], tv_season_request.watch_tv_show.id,
                            tv_season_request.season_number,
                            episode['episode_number']))
                continue

            if was_created:

                logger_background.info(
                    'adding newly found episode {} for {}'.format(
                        episode['episode_number'], tv_season_request))

                # queue task to watch episode
                watch_tv_episode_task.delay(watch_tv_episode.id)

        # assume there's no new episodes for anything that's aired this long ago
        days_since_aired = (now.date() -
                            last_air_date).days if last_air_date else 0
        if days_since_aired > 30:
            logger_background.warning(
                'completing old tv season request {}'.format(
                    tv_season_request))
            tv_season_request.collected = True
            tv_season_request.save()
Ejemplo n.º 7
0
 def __init__(self, watch_media_id: int):
     self.nefarious_settings = NefariousSettings.get()
     self.tmdb_client = get_tmdb_client(self.nefarious_settings)
     self.transmission_client = get_transmission_client(self.nefarious_settings)
     self.watch_media = self._get_watch_media(watch_media_id)
     self.tmdb_media = self._get_tmdb_media()