Exemplo n.º 1
0
    def post(self, request):
        nefarious_settings = NefariousSettings.get()
        torrent = request.data.get('torrent')
        media_type = request.data.get('media_type', MEDIA_TYPE_TV)
        if not is_magnet_url(torrent):
            torrent = swap_jackett_host(torrent, nefarious_settings)

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

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

        logging.info('adding torrent: {}'.format(torrent))

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

        if media_type == MEDIA_TYPE_MOVIE:
            download_dir = os.path.join(
                transmission_session.download_dir,
                nefarious_settings.transmission_movie_download_dir.lstrip('/'))
        else:
            download_dir = os.path.join(
                transmission_session.download_dir,
                nefarious_settings.transmission_tv_download_dir.lstrip('/'))

        transmission_client.add_torrent(
            torrent,
            paused=True,
            download_dir=download_dir,
        )

        return Response({'success': True})
Exemplo n.º 2
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)
Exemplo n.º 3
0
def cross_seed_task(torrent_hash: str):
    # TODO - this isn't used and is a work in progress

    nefarious_settings = NefariousSettings.get()
    transmission_client = get_transmission_client(nefarious_settings)

    try:
        torrent = transmission_client.get_torrent(torrent_hash)
    except KeyError:
        logging.warning('{} no longer in transmission'.format(torrent_hash))
        return

    logging.info('Attempting cross seed for {}'.format(torrent.name))

    valid_results = []

    search = SearchTorrents(MEDIA_TYPE_MOVIE,
                            torrent.name,
                            search_seed_only=True)
    if search.ok:
        for result in search.results:
            transmission_client.add_torrent()
            normalized_title = regex.sub(ParserBase.word_delimiter_regex, ' ',
                                         torrent.name)
            if result['Title'].lower() in [
                    torrent.name.lower(),
                    normalized_title.lower()
            ]:
                # TODO - the sizes won't match due to the inaccuracy of the scraping values
                # add paused torrent and verify the sizes match
                valid_results.append(result)

    # trace the "torrent url" (sometimes magnet) in each valid result
    valid_results = results_with_valid_urls(valid_results, nefarious_settings)

    logging.info(valid_results)

    seed_only_indexers = get_seed_only_indexers(nefarious_settings)

    if valid_results:
        for seed_only_indexer in seed_only_indexers:

            logging.info(
                'Looking for cross seed results for indexer {}'.format(
                    seed_only_indexer))

            # filter results to this seed-only indexer
            indexer_results = [
                r for r in valid_results if r['TrackerId'] == seed_only_indexer
            ]
            if not indexer_results:
                logging.info(
                    'Indexer {} does not have any results for {}'.format(
                        seed_only_indexer, torrent.name))
                continue

            # get the "best" result
            best_result = get_best_torrent_result(indexer_results)

            logging.info('Cross seeding {} for indexer {}'.format(
                best_result['Title'], best_result['Tracker']))

            # add a new key to our result object with the traced torrent url
            best_result[
                'torrent_url'] = best_result['MagnetUri'] or trace_torrent_url(
                    swap_jackett_host(best_result['Link'], nefarious_settings))

            # add to transmission
            transmission_session = transmission_client.session_stats()
            torrent = transmission_client.add_torrent(
                best_result['torrent_url'],
                paused=True,  # TODO
                # TODO
                #download_dir=self._get_download_dir(transmission_session)
            )
    else:
        logging.info('No valid cross seeding options for {}'.format(
            torrent.name))