Toggle navigation
首页
技术
骑行
羽毛球
资讯
联络我
登录
写个小程序来追剧之三 - 结合uTorrent自动下载
2017-07-09
.NET Core
之前写了一篇文章,见[用 .NET Core 写个小程序来追剧之二 - 添加下载任务到uTorrent客户端](/Coding/Details/add-download-task-to-utorrent-using-net-core),后来发现uTorrent可以监控文件夹的种子文件来自动添加下载任务,这样想要实现自动下载功能就更简单了,只要将最新的种子文件下载到uTorrent监控的目录中即可。 # 设定uTorrent监控文件夹 打开uTorrent,设置 --> 目录: 1. 设置种子文件默认下载位置 2. 设定自动载入 torrent 文件 3. 设定任务完成后将 torrent 文件移入位置  # 确定 chd 种子下载地址格式 打开 chd 种子下载页面,查看到种子下载格式如下: /download.php?id=[id]  id可以从种子节点a标签的href属性得到:  # 修改代码,添加种子下载 解析id代码如下: ```c# var latestEpisode = new Episode() { EpisodeName = aEpisodeNode.Attributes["title"].Value, EpisodeId = Regex.Match(aEpisodeNode.Attributes["href"].Value, @"id=(?<tag>[0-9]+)").Result("$1") }; ``` 下载种子代码如下: ```c# 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
×
本文为博主原创,如需转载,请注明出处:
http://www.supperxin.com
返回博客列表