public static bool DownloadImage(string ImageId, bool UseDialog = true) { try { ImageDownloadInfo NewInfo = new ImageDownloadInfo(ImageId); if (NewInfo.UseForm) { frmImageDownloader imageDL = new frmImageDownloader(); imageDL.DownloadInfo = NewInfo; switch (UseDialog) { case true: imageDL.ShowDialog(); break; case false: imageDL.Show(); break; } } else { ImageDownloader imageDL = new ImageDownloader(NewInfo); } return(true); } catch { throw; } }
private void btnDownloadImage_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtImageUrl.Text)) { MessageBox.Show("Please enter a valid image url or id"); return; } ImageDownloadInfo NewInfo = new ImageDownloadInfo(txtImageUrl.Text); NewInfo.SeparateRatings = chkImageSeparateRatings.Checked; NewInfo.SeparateGraylisted = chkImageSeparateGraylisted.Checked; NewInfo.SeparateBlacklisted = chkImageSeparateBlacklisted.Checked; NewInfo.SeparateNonImages = chkImageSeparateNonImages.Checked; NewInfo.SeparateArtists = chkImageSeparateArtists.Checked; NewInfo.UseForm = chkImageUseForm.Checked; NewInfo.OpenAfter = chkImageOpenAfter.Checked; if (chkImageUseForm.Checked) { frmImageDownloader Downloader = new frmImageDownloader(); Downloader.DownloadInfo = NewInfo; Downloader.Show(); } else { ImageDownloader Downloader = new ImageDownloader(NewInfo); } }
public ImageDownloader(ImageDownloadInfo Info) { if (string.IsNullOrWhiteSpace(Info.FileNameSchema)) { Info.FileNameSchema = "%artist%_%md5%"; } DownloadInfo = Info; switch (DownloadImage()) { case true: if (DownloadInfo.OpenAfter) { Process.Start("explorer.exe", "/select, \"" + DownloadInfo.DownloadPath + "\\" + DownloadInfo.FileName + "\""); } break; } }
public ImageInfo(ImageDownloadInfo DownloadInfo) { try { // Set the SaveTo to \\Images. if (!DownloadInfo.DownloadPath.EndsWith("\\Images")) { DownloadInfo.DownloadPath += "\\Images"; } // New varaibles for the API parse. string imageInfo = string.Empty; string blacklistInfo = string.Empty; string url = string.Format("https://e621.net/posts/{0}.json", DownloadInfo.PostId); DownloadPath = DownloadInfo.DownloadPath; // Begin to get the XML string postXML = apiTools.GetJsonToXml(url); // Check the XML. if (postXML == apiTools.EmptyXML || string.IsNullOrWhiteSpace(postXML)) { throw new ApiReturnedNullOrEmptyException(); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(postXML); XmlNodeList xmlID = xmlDoc.DocumentElement.SelectNodes("/root/post/id"); XmlNodeList xmlMD5 = xmlDoc.DocumentElement.SelectNodes("/root/post/file/md5"); XmlNodeList xmlExt = xmlDoc.DocumentElement.SelectNodes("/root/post/file/ext"); XmlNodeList xmlURL = xmlDoc.DocumentElement.SelectNodes("/root/post/file/url"); XmlNodeList xmlTagsGeneral = xmlDoc.DocumentElement.SelectNodes("/root/post/tags/general"); XmlNodeList xmlTagsSpecies = xmlDoc.DocumentElement.SelectNodes("/root/post/tags/species"); XmlNodeList xmlTagsCharacter = xmlDoc.DocumentElement.SelectNodes("/root/post/tags/character"); XmlNodeList xmlTagsCopyright = xmlDoc.DocumentElement.SelectNodes("/root/post/tags/copyright"); XmlNodeList xmlTagsArtist = xmlDoc.DocumentElement.SelectNodes("/root/post/tags/artist"); XmlNodeList xmlTagsInvalid = xmlDoc.DocumentElement.SelectNodes("/root/post/tags/invalid"); XmlNodeList xmlTagsLore = xmlDoc.DocumentElement.SelectNodes("/root/post/tags/lore"); XmlNodeList xmlTagsMeta = xmlDoc.DocumentElement.SelectNodes("/root/post/tags/meta"); XmlNodeList xmlTagsLocked = xmlDoc.DocumentElement.SelectNodes("/root/post/locked_tags"); XmlNodeList xmlScoreUp = xmlDoc.DocumentElement.SelectNodes("/root/post/score/up"); XmlNodeList xmlScoreDown = xmlDoc.DocumentElement.SelectNodes("/root/post/score/down"); XmlNodeList xmlScoreTotal = xmlDoc.DocumentElement.SelectNodes("/root/post/score/total"); XmlNodeList xmlRating = xmlDoc.DocumentElement.SelectNodes("/root/post/rating"); XmlNodeList xmlFavCount = xmlDoc.DocumentElement.SelectNodes("/root/post/fav_count"); XmlNodeList xmlAuthor = xmlDoc.DocumentElement.SelectNodes("/root/post/uploader_id"); XmlNodeList xmlDescription = xmlDoc.DocumentElement.SelectNodes("/root/post/description"); XmlNodeList xmlDeleted = xmlDoc.DocumentElement.SelectNodes("/root/post/flags/deleted"); switch (xmlDeleted[0].InnerText.ToLower() == "true") { case true: throw new PoolOrPostWasDeletedException("Image was deleted"); case false: FileUrl = xmlURL[0].InnerText; if (FileUrl == null) { FileUrl = apiTools.GetBlacklistedImageUrl(xmlMD5[0].InnerText, xmlExt[0].InnerText); if (FileUrl == null) { throw new ImageWasNullAfterBypassingException(DownloadInfo.PostId + " was still null."); } } List <string> PostTags = new List <string>(); string InfoTags = string.Empty; InfoTags += "\r\n General: [ "; switch (xmlTagsGeneral[0].ChildNodes.Count > 0) { case true: for (int x = 0; x < xmlTagsGeneral[0].ChildNodes.Count; x++) { PostTags.Add(xmlTagsGeneral[0].ChildNodes[x].InnerText); InfoTags += xmlTagsGeneral[0].ChildNodes[x].InnerText + ", "; } InfoTags = InfoTags.TrimEnd(' ').TrimEnd(','); break; case false: InfoTags += "none"; break; } InfoTags += " ]\r\n Species: [ "; switch (xmlTagsSpecies[0].ChildNodes.Count > 0) { case true: for (int x = 0; x < xmlTagsSpecies[0].ChildNodes.Count; x++) { PostTags.Add(xmlTagsSpecies[0].ChildNodes[x].InnerText); InfoTags += xmlTagsSpecies[0].ChildNodes[x].InnerText + ", "; } InfoTags = InfoTags.TrimEnd(' ').TrimEnd(','); break; case false: InfoTags += "none"; break; } InfoTags += " ]\r\n Character(s): [ "; switch (xmlTagsCharacter[0].ChildNodes.Count > 0) { case true: for (int x = 0; x < xmlTagsCharacter[0].ChildNodes.Count; x++) { PostTags.Add(xmlTagsCharacter[0].ChildNodes[x].InnerText); InfoTags += xmlTagsCharacter[0].ChildNodes[x].InnerText + ", "; } InfoTags = InfoTags.TrimEnd(' ').TrimEnd(','); break; case false: InfoTags += "none"; break; } InfoTags += " ]\r\n Copyright: [ "; switch (xmlTagsCopyright[0].ChildNodes.Count > 0) { case true: for (int x = 0; x < xmlTagsCopyright[0].ChildNodes.Count; x++) { PostTags.Add(xmlTagsCopyright[0].ChildNodes[x].InnerText); InfoTags += xmlTagsCopyright[0].ChildNodes[x].InnerText + ", "; } InfoTags = InfoTags.TrimEnd(' ').TrimEnd(','); break; case false: InfoTags += "none"; break; } InfoTags += " ]\r\n Artist: [ "; switch (xmlTagsArtist[0].ChildNodes.Count > 0) { case true: for (int x = 0; x < xmlTagsArtist[0].ChildNodes.Count; x++) { PostTags.Add(xmlTagsArtist[0].ChildNodes[x].InnerText); InfoTags += xmlTagsArtist[0].ChildNodes[x].InnerText + ", "; } InfoTags = InfoTags.TrimEnd(' ').TrimEnd(','); break; case false: InfoTags += "none"; break; } InfoTags += " ]\r\n Lore: [ "; switch (xmlTagsLore[0].ChildNodes.Count > 0) { case true: for (int x = 0; x < xmlTagsLore[0].ChildNodes.Count; x++) { PostTags.Add(xmlTagsLore[0].ChildNodes[x].InnerText); InfoTags += xmlTagsLore[0].ChildNodes[x].InnerText + ", "; } InfoTags = InfoTags.TrimEnd(' ').TrimEnd(','); break; case false: InfoTags += "none"; break; } InfoTags += " ]\r\n Meta: [ "; switch (xmlTagsMeta[0].ChildNodes.Count > 0) { case true: for (int x = 0; x < xmlTagsMeta[0].ChildNodes.Count; x++) { PostTags.Add(xmlTagsMeta[0].ChildNodes[x].InnerText); InfoTags += xmlTagsMeta[0].ChildNodes[x].InnerText + ", "; } InfoTags = InfoTags.TrimEnd(' ').TrimEnd(','); break; case false: InfoTags += "none"; break; } InfoTags += " ]\r\n Locked tags: [ "; switch (xmlTagsLocked[0].ChildNodes.Count > 0) { case true: for (int x = 0; x < xmlTagsLocked[0].ChildNodes.Count; x++) { PostTags.Add(xmlTagsLocked[0].ChildNodes[x].InnerText); InfoTags += xmlTagsLocked[0].ChildNodes[x].InnerText + ", "; } InfoTags = InfoTags.TrimEnd(' ').TrimEnd(',') + " ]"; break; case false: InfoTags += "none"; break; } InfoTags += " ]\r\n Invalid: [ "; switch (xmlTagsInvalid[0].ChildNodes.Count > 0) { case true: for (int x = 0; x < xmlTagsInvalid[0].ChildNodes.Count; x++) { PostTags.Add(xmlTagsInvalid[0].ChildNodes[x].InnerText); InfoTags += xmlTagsInvalid[0].ChildNodes[x].InnerText + ", "; } InfoTags = InfoTags.TrimEnd(' ').TrimEnd(','); break; case false: InfoTags += "none"; break; } InfoTags += " ]"; // Get the rating. string rating = xmlRating[0].InnerText; switch (rating.ToLower()) { case "e": case "explicit": rating = "Explicit"; break; case "q": case "questionable": rating = "Questionable"; break; case "s": case "safe": rating = "Safe"; break; } // Set the description string imageDescription = " No description"; if (xmlDescription[0].InnerText != "") { imageDescription = "\n \"" + xmlDescription[0].InnerText + "\""; } // Read blacklists if it's blacklisted. bool isGraylisted = false; bool isBlacklisted = false; string offendingTags = string.Empty; for (int i = 0; i < PostTags.Count; i++) { if (DownloadInfo.Graylist.Length > 0) { for (int j = 0; j < DownloadInfo.Graylist.Length; j++) { if (PostTags[i] == DownloadInfo.Graylist[j]) { offendingTags += PostTags[i]; isGraylisted = true; } } } // Since the image downloader is user specific, the blacklist won't cancel downloading. // Image downloading is basically the user consenting to downloading it, therefore they want it despite the blacklist. // Worst-case, just delete it after downloading. if (DownloadInfo.Blacklist.Length > 0) { for (int j = 0; j < DownloadInfo.Blacklist.Length; j++) { if (PostTags[i] == DownloadInfo.Blacklist[j]) { offendingTags += PostTags[i]; isBlacklisted = true; } } } } // set the .nfo buffer. InfoBuffer += "POST " + DownloadInfo.PostId + ":\r\n" + " MD5: " + xmlMD5[0].InnerText + "\r\n" + " URL: https://e621.net/posts/" + DownloadInfo.PostId + "\r\n" + " TAGS:\r\n" + InfoTags + " SCORE: Up " + xmlScoreUp[0].InnerText + ", Down " + xmlScoreDown[0].InnerText + ", Total" + xmlScoreTotal[0].InnerText + "\r\n" + " RATING: " + rating + "\r\n"; if (isBlacklisted) { InfoBuffer = "BLACKLISTED " + InfoBuffer + " OFFENDING TAGS: " + offendingTags + "\r\n"; } else if (isGraylisted) { InfoBuffer = "GRAYLISTED " + InfoBuffer + " OFFENDING TAGS: " + offendingTags + "\r\n"; } InfoBuffer += " DESCRIPITON:" + imageDescription; // Work on the filename string fileNameArtist = "(none)"; bool useHardcodedFilter = false; if (string.IsNullOrEmpty(Config.Settings.General.undesiredTags)) { useHardcodedFilter = true; } if (xmlTagsArtist.Count > 0) { if (!string.IsNullOrEmpty(xmlTagsArtist[0].InnerText)) { for (int i = 0; i < xmlTagsArtist.Count; i++) { if (useHardcodedFilter) { if (!UndesiredTags.isUndesiredHardcoded(xmlTagsArtist[i].InnerText)) { fileNameArtist = xmlTagsArtist[i].InnerText; break; } } else { if (!UndesiredTags.isUndesired(xmlTagsArtist[i].InnerText)) { fileNameArtist = xmlTagsArtist[i].InnerText; break; } } } } } DownloadInfo.FileName = DownloadInfo.FileNameSchema.Replace("%md5%", xmlMD5[0].InnerText) .Replace("%id%", xmlID[0].InnerText) .Replace("%rating%", rating.ToLower()) .Replace("%rating2%", xmlRating[0].InnerText) .Replace("%artist%", fileNameArtist) .Replace("%ext%", xmlExt[0].InnerText) .Replace("%fav_count%", xmlFavCount[0].InnerText) .Replace("%score%", xmlScoreTotal[0].InnerText) .Replace("%scoreup%", xmlScoreUp[0].InnerText) .Replace("%scoredown%", xmlScoreDown[0].InnerText) .Replace("%author%", xmlAuthor[0].InnerText) + "." + xmlExt[0].InnerText; // Start working on the SaveTo string. if (DownloadInfo.SeparateRatings) { switch (xmlRating[0].InnerText.ToLower()) { case "e": case "explicit": DownloadPath += "\\explicit"; break; case "q": case "questionable": DownloadPath += "\\questionable"; break; case "s": case "safe": DownloadPath += "\\safe"; break; } } if (isBlacklisted && DownloadInfo.SeparateBlacklisted) { DownloadPath += "\\blacklisted"; } else if (isGraylisted && DownloadInfo.SeparateGraylisted) { DownloadPath += "\\graylisted"; } if (DownloadInfo.SeparateArtists) { DownloadPath += "\\" + fileNameArtist; } switch (DownloadInfo.SeparateNonImages) { case true: switch (xmlExt[0].InnerText) { case "gif": DownloadPath += "\\gif"; break; case "apng": DownloadPath += "\\apng"; break; case "webm": DownloadPath += "\\webm"; break; case "swf": DownloadPath += "\\swf"; break; } break; } // Create output directory. if (!Directory.Exists(DownloadPath)) { Directory.CreateDirectory(DownloadPath); } // Set the path to the full path with file. DownloadPath += "\\" + DownloadInfo.FileName; // Check file before continuing. if (File.Exists(DownloadPath)) { Status = DownloadStatus.FileAlreadyExists; return; } // Save image.nfo. if (DownloadInfo.SaveInfo) { if (isBlacklisted) { if (File.Exists(DownloadInfo.DownloadPath + "\\images.blacklisted.nfo")) { InfoBuffer = "\n\n" + InfoBuffer; string readInfo = File.ReadAllText(DownloadInfo.DownloadPath + "\\images.blacklisted.nfo"); if (!readInfo.Contains("MD5: " + xmlMD5[0].InnerText)) { File.AppendAllText(DownloadInfo.DownloadPath + "\\images.blacklisted.nfo", InfoBuffer, Encoding.UTF8); } } else { File.WriteAllText(DownloadInfo.DownloadPath + "\\images.blacklisted.nfo", InfoBuffer, Encoding.UTF8); } } else if (isGraylisted) { if (File.Exists(DownloadInfo.DownloadPath + "\\images.graylisted.nfo")) { InfoBuffer = "\n\n" + InfoBuffer; string readInfo = File.ReadAllText(DownloadInfo.DownloadPath + "\\images.graylisted.nfo"); if (!readInfo.Contains("MD5: " + xmlMD5[0].InnerText)) { File.AppendAllText(DownloadInfo.DownloadPath + "\\images.graylisted.nfo", InfoBuffer, Encoding.UTF8); } } else { File.WriteAllText(DownloadInfo.DownloadPath + "\\images.blacklisted.nfo", InfoBuffer, Encoding.UTF8); } } else { if (File.Exists(DownloadInfo.DownloadPath + "\\images.nfo")) { InfoBuffer = "\n\n" + InfoBuffer; string readInfo = File.ReadAllText(DownloadInfo.DownloadPath + "\\images.nfo"); if (!readInfo.Contains("MD5: " + xmlMD5[0].InnerText)) { File.AppendAllText(DownloadInfo.DownloadPath + "\\images.nfo", InfoBuffer, Encoding.UTF8); } } else { File.WriteAllText(DownloadInfo.DownloadPath + "\\images.nfo", InfoBuffer, Encoding.UTF8); } } } Status = DownloadStatus.ReadyToDownload; break; } } catch { throw; } }