-
Notifications
You must be signed in to change notification settings - Fork 0
93 写个小程序来追剧之三 结合uTorrent自动下载
Jinxin Chen edited this page Dec 11, 2019
·
1 revision
之前写了一篇文章,见用 .NET Core 写个小程序来追剧之二 - 添加下载任务到uTorrent客户端,后来发现uTorrent可以监控文件夹的种子文件来自动添加下载任务,这样想要实现自动下载功能就更简单了,只要将最新的种子文件下载到uTorrent监控的目录中即可。
打开uTorrent,设置 --> 目录:
- 设置种子文件默认下载位置
- 设定自动载入 torrent 文件
- 设定任务完成后将 torrent 文件移入位置
打开 chd 种子下载页面,查看到种子下载格式如下:
/download.php?id=[id]
id可以从种子节点a标签的href属性得到:
解析id代码如下:
var latestEpisode = new Episode()
{
EpisodeName = aEpisodeNode.Attributes["title"].Value,
EpisodeId = Regex.Match(aEpisodeNode.Attributes["href"].Value, @"id=(?<tag>[0-9]+)").Result("$1")
};
下载种子代码如下:
private static void DownloadTorrent(string baseUrl, string downloadUrl, string downloadPath, CookieContainer cookieContainer, Episode latestEpisode)
{
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = new Uri(baseUrl) })
{
var result = client.GetAsync(downloadUrl + latestEpisode.EpisodeId).Result;
var fileName = WebUtility.UrlDecode(result.Content.Headers.ContentDisposition.FileName);
using (
Stream contentStream = result.Content.ReadAsStreamAsync().Result,
stream = new FileStream(downloadPath + fileName, FileMode.Create, FileAccess.Write, FileShare.None, 3 * 1024 * 1024, true))
{
contentStream.CopyToAsync(stream);
}
Console.WriteLine("New file : " + fileName);
result.EnsureSuccessStatusCode();
}
}
最终的运行效果如下:
代码参见commit:
https://github.com/xiaoxin01/Supperxin.EpisodeMonitor/tree/91c1f269c62897647967589e4ac8617a0e941e73