Exemplo n.º 1
0
        async Task ProcessTorznab(HttpListenerContext context)
        {
            var query       = HttpUtility.ParseQueryString(context.Request.Url.Query);
            var inputStream = context.Request.InputStream;

            var indexerId = context.Request.Url.Segments[2].TrimEnd('/').ToLower();
            var indexer   = indexerManager.GetIndexer(indexerId);

            if (context.Request.Url.Segments.Length > 4 && context.Request.Url.Segments[3] == "download/")
            {
                var downloadSegment = HttpServerUtility.UrlTokenDecode(context.Request.Url.Segments[4].TrimEnd('/'));
                var downloadLink    = Encoding.UTF8.GetString(downloadSegment);
                var downloadBytes   = await indexer.Download(new Uri(downloadLink));

                await context.Response.OutputStream.WriteAsync(downloadBytes, 0, downloadBytes.Length);

                return;
            }

            var torznabQuery = TorznabQuery.FromHttpQuery(query);

            if (torznabQuery.RageID != 0)
            {
                torznabQuery.ShowTitles = await sonarrApi.GetShowTitle(torznabQuery.RageID);
            }
            else if (!string.IsNullOrEmpty(torznabQuery.SearchTerm))
            {
                torznabQuery.ShowTitles = new string[] { torznabQuery.SearchTerm }
            }
            ;

            var releases = await indexer.PerformQuery(torznabQuery);

            Program.LoggerInstance.Debug(string.Format("Found {0} releases from {1}", releases.Length, indexer.DisplayName));

            var severUrl = string.Format("{0}://{1}:{2}/", context.Request.Url.Scheme, context.Request.Url.Host, context.Request.Url.Port);

            var resultPage = new ResultPage(new ChannelInfo
            {
                Title            = indexer.DisplayName,
                Description      = indexer.DisplayDescription,
                Link             = indexer.SiteLink,
                ImageUrl         = new Uri(severUrl + "logos/" + indexerId + ".png"),
                ImageTitle       = indexer.DisplayName,
                ImageLink        = indexer.SiteLink,
                ImageDescription = indexer.DisplayName
            });

            // add Jackett proxy to download links...
            foreach (var release in releases)
            {
                if (release.Link == null || release.Link.Scheme == "magnet")
                {
                    continue;
                }
                var originalLink = release.Link;
                var encodedLink  = HttpServerUtility.UrlTokenEncode(Encoding.UTF8.GetBytes(originalLink.ToString())) + "/download.torrent";
                var proxyLink    = string.Format("{0}api/{1}/download/{2}", severUrl, indexerId, encodedLink);
                release.Link = new Uri(proxyLink);
            }

            resultPage.Releases.AddRange(releases);

            var xml = resultPage.ToXml(new Uri(severUrl));

            var responseBytes = Encoding.UTF8.GetBytes(xml);

            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.ContentLength64 = responseBytes.LongLength;
            context.Response.ContentType     = "application/rss+xml";
            await context.Response.OutputStream.WriteAsync(responseBytes, 0, responseBytes.Length);
        }
    }
Exemplo n.º 2
0
        async Task ProcessTorznab(HttpListenerContext context)
        {

            var query = HttpUtility.ParseQueryString(context.Request.Url.Query);
            var inputStream = context.Request.InputStream;

            var indexerId = context.Request.Url.Segments[2].TrimEnd('/').ToLower();
            var indexer = indexerManager.GetIndexer(indexerId);

            if (context.Request.Url.Segments.Length > 4 && context.Request.Url.Segments[3] == "download/")
            {
                var downloadSegment = HttpServerUtility.UrlTokenDecode(context.Request.Url.Segments[4].TrimEnd('/'));
                var downloadLink = Encoding.UTF8.GetString(downloadSegment);
                var downloadBytes = await indexer.Download(new Uri(downloadLink));
                await context.Response.OutputStream.WriteAsync(downloadBytes, 0, downloadBytes.Length);
                return;
            }

            var torznabQuery = TorznabQuery.FromHttpQuery(query);

            if (torznabQuery.RageID != 0)
                torznabQuery.ShowTitles = await sonarrApi.GetShowTitle(torznabQuery.RageID);
            else if (!string.IsNullOrEmpty(torznabQuery.SearchTerm))
                torznabQuery.ShowTitles = new string[] { torznabQuery.SearchTerm };

            var releases = await indexer.PerformQuery(torznabQuery);

            Program.LoggerInstance.Debug(string.Format("Found {0} releases from {1}", releases.Length, indexer.DisplayName));

            var severUrl = string.Format("{0}://{1}:{2}/", context.Request.Url.Scheme, context.Request.Url.Host, context.Request.Url.Port);

            var resultPage = new ResultPage(new ChannelInfo
            {
                Title = indexer.DisplayName,
                Description = indexer.DisplayDescription,
                Link = indexer.SiteLink,
                ImageUrl = new Uri(severUrl + "logos/" + indexerId + ".png"),
                ImageTitle = indexer.DisplayName,
                ImageLink = indexer.SiteLink,
                ImageDescription = indexer.DisplayName
            });

            // add Jackett proxy to download links...
            foreach (var release in releases)
            {
                if (release.Link == null || release.Link.Scheme == "magnet")
                    continue;
                var originalLink = release.Link;
                var encodedLink = HttpServerUtility.UrlTokenEncode(Encoding.UTF8.GetBytes(originalLink.ToString())) + "/download.torrent";
                var proxyLink = string.Format("{0}api/{1}/download/{2}", severUrl, indexerId, encodedLink);
                release.Link = new Uri(proxyLink);
            }

            resultPage.Releases.AddRange(releases);

            var xml = resultPage.ToXml(new Uri(severUrl));

            var responseBytes = Encoding.UTF8.GetBytes(xml);
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.ContentLength64 = responseBytes.LongLength;
            context.Response.ContentType = "application/rss+xml";
            await context.Response.OutputStream.WriteAsync(responseBytes, 0, responseBytes.Length);

        }