public async Task<byte[]> DownloadTorrentFile(Torrent torrent) { var uriStr = torrent.Uri.ToString(); const string remove = ".html"; if (uriStr.EndsWith(remove)) { uriStr = uriStr.Substring(0, uriStr.LastIndexOf(remove, StringComparison.Ordinal)); } var response = await _httpClient.GetByteArrayAsync(new Uri("/torrents" + uriStr, UriKind.Relative)); return response; }
private static List<Torrent> GetTorrents(string source, Status status) { var list = new List<Torrent>(); var parser = new HtmlParser(); var htmlDocument = parser.Parse(source); var torrentTable = htmlDocument.QuerySelector("table.data"); var t = torrentTable as IHtmlTableElement; foreach (var tr in t.Rows) { if (tr.ClassName == "firstr") continue; var torrent = new Torrent {Status = status}; var i = 0; foreach (var cell in tr.Cells) { switch (i) { case 0: string name; Uri uri; ExtractFromTorrentNameCell(cell, out name, out uri); torrent.Name = name; torrent.Uri = uri; if (status == Status.Active) torrent.Magnet = ExtractMagnetFromTorrentNameCell(cell); break; case 1: torrent.Hash = ExtractHashFromControlCell(cell); break; case 2: torrent.Size = ByteSize.Parse(cell.TextContent).Bytes; break; case 3: torrent.Files = int.Parse(cell.TextContent); break; case 4: torrent.Age = ExtractAgeFromAgeCell(cell); break; case 5: torrent.Seed = int.Parse(cell.TextContent); break; case 6: torrent.Leech = int.Parse(cell.TextContent); break; case 7: var deletedByUser = cell.QuerySelector("a.plain") as IHtmlAnchorElement; torrent.DeletedBy = deletedByUser.TextContent; torrent.DeletedByProfileUri = new Uri(deletedByUser.PathName, UriKind.Relative); break; } i++; } list.Add(torrent); } return list; }
public async Task<EditTorrentFormContent> GetEditTorrentFormContent(Torrent torrent) { var editTorrentFormContent = new EditTorrentFormContent(); var uri = new Uri("/torrents/edit/" + torrent.Hash + "/", UriKind.Relative); var response = await _httpClient.GetAsync(uri); var source = await response.Content.ReadAsStringAsync(); var parser = new HtmlParser(); var htmlDocument = parser.Parse(source); var categoryIdSelectTag = htmlDocument.QuerySelector("#categoryId") as IHtmlSelectElement; editTorrentFormContent.CategoryId = int.Parse(categoryIdSelectTag.Value); var subCategoryIdSelectTag = htmlDocument.QuerySelector("select[name=\"sub_cat\"]") as IHtmlSelectElement; editTorrentFormContent.SubCategoryId = int.Parse(subCategoryIdSelectTag.Value); var nameInputTag = htmlDocument.QuerySelector("input[name=\"name\"]") as IHtmlInputElement; editTorrentFormContent.Name = nameInputTag.Value; var bbcodeInput = htmlDocument.QuerySelector("#bbcode") as IHtmlTextAreaElement; editTorrentFormContent.Description = bbcodeInput.Value; return editTorrentFormContent; }