public static string BuildSearchUrl(ShowData showData) { var url = "http://www.nyaa.se/?term=" + "%5B" + showData.Subber + "%5D+" + showData.ShowName.Replace(' ', '+'); if (showData.Resolution != string.Empty) { url += "+" + showData.Resolution; } return(url); }
public static List <string> GetDownloadedEpisodes(string downloadDirectory, ShowData showData) { var downloadedEpisodes = new List <string>(); try { downloadedEpisodes = Directory.GetFiles(downloadDirectory) .Select(Path.GetFileName) .Where(x => x.ToLower().EndsWith(".mp4") || x.ToLower().EndsWith(".mkv")) .Select(PrettyPrintName) .Where(x => x.Contains(showData.ShowName)).ToList(); } catch (Exception exception) { Console.WriteLine("Error: exception encountered when reading download directory ({0}), exiting...", exception.Message); Environment.Exit(1); } return(downloadedEpisodes); }
static void Main(string[] args) { // This prevents the framework from trying to autodetect proxy settings which can make the request // extremely slow (up to an additional 30 seconds). // http://www.nullskull.com/a/848/webclient-class-gotchas-and-basics.aspx WebRequest.DefaultWebProxy = null; if (args.Length < 3) { Console.WriteLine(UsageString); Environment.Exit(0); } var options = new Options(); var parser = new Parser(); if (!parser.ParseArguments(new List <string>(args).GetRange(0, args.Length - 3).ToArray(), options)) { Console.WriteLine(UsageString); Environment.Exit(0); } var showName = args[args.Length - 3].Trim(); var subber = args[args.Length - 2].Trim(); var downloadDirectory = args[args.Length - 1].Trim(); if (showName == string.Empty || subber == string.Empty || downloadDirectory == string.Empty) { Console.WriteLine("Error: none of the arguments can be empty, exiting..."); Environment.Exit(0); } if (!Directory.Exists(downloadDirectory)) { Console.WriteLine("Error: the specified directory does not exist, exiting..."); Environment.Exit(0); } var showData = new ShowData( showName.Trim(), subber.Trim(), options.Resolution != null ? options.Resolution.Trim() : string.Empty); Console.WriteLine("\nWill now monitor and download for..."); Console.WriteLine(" Show: {0}", showData.ShowName); Console.WriteLine(" Subber: {0}", showData.Subber); if (showData.Resolution != string.Empty) { Console.WriteLine(" Resolution: {0}", showData.Resolution); } Console.WriteLine("Press any key to continue"); Console.ReadKey(true); Console.WriteLine("\nLink Start!\n"); var url = BuildSearchUrl(showData); var previousCheckFoundNothingNew = false; while (true) { var downloadedEpisodes = GetDownloadedEpisodes(downloadDirectory, showData); if (Debug) { Console.WriteLine("Downloaded Episodes:"); downloadedEpisodes.ForEach(x => Console.WriteLine(" " + x)); } GetOnlineEpisodes(GetWebPageContents(url), showData); if (Debug) { Console.WriteLine("Online Episodes:"); showData.Episodes.ForEach(x => Console.WriteLine(" " + x.Name + ", " + x.TorrentUrl)); } var episodesToDownload = showData.Episodes.Where(episode => !downloadedEpisodes.Contains(episode.Name)).ToList(); if (Debug) { Console.WriteLine("Episodes to Download:"); episodesToDownload.ForEach(x => Console.WriteLine(" " + x.Name + ", " + x.TorrentUrl)); } if (episodesToDownload.Count > 0) { Console.WriteLine("\nFound {0} episodes to download! Starting...\n", episodesToDownload.Count); previousCheckFoundNothingNew = false; foreach (var episode in episodesToDownload) { DownloadTorrent(episode.TorrentUrl, downloadDirectory, 25); if (!String.IsNullOrEmpty(options.SmsReceiver)) { Console.WriteLine("Sending SMS...\n"); Process.Start("SMSUtil.exe", "\"" + options.SmsReceiver + "\" \"'" + episode.Name + "' has finished downloading\""); } } } else { if (previousCheckFoundNothingNew) { Console.SetCursorPosition(0, Console.CursorTop - 1); } Console.WriteLine("Nothing to download at this time, waiting..."); previousCheckFoundNothingNew = true; // monitor every minute var waitDuration = DateTime.Now.AddMinutes(1) - DateTime.Now; Thread.Sleep(waitDuration); } } }
public static void GetOnlineEpisodes(string pageContents, ShowData showData) { showData.Episodes.Clear(); var tlistnameIndices = Regex.Matches(pageContents, "<td class=\"tlistname\">") .Cast <Match>().Select(match => match.Index).OrderBy(x => x).ToArray(); var tlistdownloadIndices = Regex.Matches(pageContents, "<td class=\"tlistdownload\">") .Cast <Match>().Select(match => match.Index).OrderBy(x => x).ToArray(); if (tlistnameIndices.Length != tlistdownloadIndices.Length) { Console.WriteLine("Error: parser error - number of tlistname and tlistdownload matches not equal ({0} vs {1})", tlistnameIndices.Count(), tlistdownloadIndices.Count()); return; } for (var i = 0; i < tlistnameIndices.Length; i++) { if (tlistnameIndices[i] >= tlistdownloadIndices[i]) { Console.WriteLine("Error: parser error - tlistname index greater than tlistdownload index ({0} vs {1})", tlistnameIndices[i], tlistdownloadIndices[i]); continue; } var rawNameBeginIndex = pageContents.IndexOf(">", tlistnameIndices[i] + "<td class=\"tlistname\">".Length + 1, tlistdownloadIndices[i] - tlistnameIndices[i], StringComparison.Ordinal) + 1; var rawNameEndIndex = pageContents.IndexOf("</a>", tlistnameIndices[i], tlistdownloadIndices[i] - tlistnameIndices[i], StringComparison.Ordinal); if (rawNameBeginIndex >= rawNameEndIndex || rawNameBeginIndex < 0 || rawNameEndIndex < 0) { Console.WriteLine("Error: parser error - rawNameBeginIndex and/or rawNameEndIndex incorrect ({0}, {1})", rawNameBeginIndex, rawNameEndIndex); continue; } var rawName = pageContents.Substring(rawNameBeginIndex, rawNameEndIndex - rawNameBeginIndex); if (!rawName.Contains(showData.Subber) || !rawName.Contains(showData.Resolution)) { continue; } if (!rawName.ToLower().EndsWith(".mp4") && !rawName.ToLower().EndsWith(".mkv")) { continue; // avoid stuff like TV batches } var prettyName = PrettyPrintName(rawName); if (!prettyName.Contains(showData.ShowName)) { continue; } var torrentUrlBeginIndex = pageContents.IndexOf("<a href=\"", tlistdownloadIndices[i], i != tlistnameIndices.Length - 1 ? tlistnameIndices[i + 1] - tlistdownloadIndices[i] : pageContents.Length - tlistdownloadIndices[i], StringComparison.Ordinal) + "<a href=\"".Length; var torrentUrlEndIndex = pageContents.IndexOf("\" title=\"Download\"", tlistdownloadIndices[i], i != tlistnameIndices.Length - 1 ? tlistnameIndices[i + 1] - tlistdownloadIndices[i] : pageContents.Length - tlistdownloadIndices[i], StringComparison.Ordinal); if (torrentUrlBeginIndex >= torrentUrlEndIndex || torrentUrlBeginIndex < 0 || torrentUrlEndIndex < 0) { Console.WriteLine("Error: parser error - torrentUrlBeginIndex and/or torrentLinkEndIndex incorrect ({0}, {1})", torrentUrlBeginIndex, torrentUrlEndIndex); continue; } var torrentUrl = pageContents.Substring(torrentUrlBeginIndex, torrentUrlEndIndex - torrentUrlBeginIndex).Replace("&", "&"); showData.Episodes.Add(new EpisodeData(prettyName, torrentUrl)); } showData.Episodes.Reverse(); // oldest first }