示例#1
0
        async Task SelectFile(string id, DownloadBase download)
        {
            var fileToDownload = new List <string>(1)
            {
                "all"
            };


            using var GetFilesMessage = new HttpRequestMessage(HttpMethod.Get, $"torrents/info/{id}");

            var resp = await _httpClient.SendAsync(GetFilesMessage);

            resp.EnsureSuccessStatusCode();
            var torrentInfo = TorrentInfo.FromJson(await resp.Content.ReadAsStringAsync());

            download.FileAvailable = torrentInfo.Files.Select(p => p.Path).ToArray();

            var fileToKeep = await _mediatr.Send(new SelectFileOfTorrent(torrentInfo.Files.Select(p => p.Path)));

            var files = from file in torrentInfo.Files
                        join fileToKeepSelector in fileToKeep.Filenames on file.Path equals fileToKeepSelector
                        select file.Id.ToString();

            if (files.Count() != torrentInfo.Files.Count)
            {
                fileToDownload        = files.ToList();
                download.FileSelected = files.ToArray();
            }
            else
            {
                download.FileSelected = download.FileAvailable;
            }

            using var selectFileMessage = new HttpRequestMessage(HttpMethod.Post, $"torrents/selectFiles/{id}")
                  {
                      Content = new FormUrlEncodedContent(new Dictionary <string, string>
                {
                    {
                        "files", String.Join(",", fileToDownload)
                    }
                })
                  };

            var res = await _httpClient.SendAsync(selectFileMessage);

            res.EnsureSuccessStatusCode();
        }
示例#2
0
        async Task <IEnumerable <Uri> > UnrestrictLink(string id)
        {
            var links = new ConcurrentBag <Uri>();

            using var message = new HttpRequestMessage(HttpMethod.Get, $"torrents/info/{id}");

            var resp = await _httpClient.SendAsync(message);

            try
            {
                resp.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException ex)
            {
                _logger.LogError("Torrent upload failed, check your subscription or your torrent", ex);
                throw;
            }

            var info = TorrentInfo.FromJson(await resp.Content.ReadAsStringAsync());

            Parallel.ForEach(info.Links, link =>
            {
                using var message = new HttpRequestMessage(HttpMethod.Post, "unrestrict/link")
                      {
                          Content = new FormUrlEncodedContent(new Dictionary <string, string>
                    {
                        {
                            "link", link.ToString()
                        }
                    })
                      };

                // It inside an action so async / await is unusable.
                var response = _httpClient.SendAsync(message).GetAwaiter().GetResult();
                response.EnsureSuccessStatusCode();

                var result = Link.FromJson(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());

                links.Add(result.Download);
            });

            return(links.ToList());
        }
示例#3
0
        async Task WaitForDownloadCompletion(string id, DownloadBase request, CancellationToken ct)
        {
            Torrent result;

            using var cancelOperation = ct.Register(() => _httpClient.SendAsync(
                                                        new HttpRequestMessage(HttpMethod.Delete, $"torrents/info/{id}")));

            do
            {
                var message = new HttpRequestMessage(HttpMethod.Get, $"torrents/info/{id}");
                //Even the first time to let API donwload file
                Thread.Sleep(_configuration.IntervalCheckDownload);
                var req = await _httpClient.SendAsync(message);

                result = TorrentInfo.FromJson(await req.Content.ReadAsStringAsync());

                switch (result.Status)
                {
                case "queued":
                case "downloading":
                case "uploading":
                    _progressHandler.OnNext(new ProgressTracker
                    {
                        DownloadStep = DownloadStep.RemoteTorrentDownloadToHttpServer,
                        Progress     = result.Progress,
                        Torrent      = request
                    });
                    break;

                case "error":
                case "virus":
                case "dead":
                    _progressHandler.OnError(new RemoteTorrentDownloadException(result.Status, request));
                    break;
                }
            } while (result.Status != "downloaded");

            _logger.LogTrace("Torrent downloaded");
        }