diff --git a/DanEngine.cs b/DanEngine.cs new file mode 100644 index 0000000..2663d6c --- /dev/null +++ b/DanEngine.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using Microsoft.Win32; +using System.Runtime.InteropServices; +using System.IO; +using System.Text.RegularExpressions; +using System.Xml; + +namespace GelDownloader +{ + class DanEngine + { + XmlDocument doc = new XmlDocument(); + XmlElement root; + string url; + + [DllImport("kernel32.dll", SetLastError = true)] + static extern bool AllocConsole(); + + string ExtFromURL(string line) + { + var ext = ""; + var match = Regex.Match(line, "(?:)\\.[\\d\\w]+$"); + if (match.Success) + ext = match.Value; + return ext; + } + string FnameFromURL(string line) + { + var fname = ""; + var match = Regex.Match(line, "(?:)[\\d\\w]+\\.[\\d\\w]+$"); + if (match.Success) + fname = match.Value; + return fname; + } + + private void downloadImage(string url, string tags, bool keepOriginalNames) + { + using (WebClient wc = new WebClient()) + { + if (keepOriginalNames) + wc.DownloadFileAsync(new System.Uri(url), "./out/" + FnameFromURL(url)); + else + wc.DownloadFileAsync(new System.Uri(url), "./out/" + tags + ExtFromURL(url)); + } + } + + public string downloadPosts(string domain, string tags, int page, bool keepOriginalNames) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(domain + "/index.php?page=dapi&s=post&q=index&limit=1&tags=" + tags + "&pid=" + page); + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + using (Stream stream = response.GetResponseStream()) + { + StreamReader reader = new StreamReader(stream, Encoding.UTF8); + String responseString = reader.ReadToEnd(); + + doc.LoadXml(responseString); + root = doc.DocumentElement; + XmlNode node = (XmlNode)doc.DocumentElement; + XmlNode sourceNode = node.SelectSingleNode("post/file-url"); + XmlNode tagsNode = node.SelectSingleNode("post/tag-string"); + if (sourceNode == null) + { + return ""; + } + url = sourceNode.InnerXml; + var tagstring = tagsNode.InnerXml; + Console.WriteLine(url); + downloadImage(url, tagstring, keepOriginalNames); + } + return url; + } + + public int getPostCount(string domain, string tags) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(domain + "/posts.xml?page=dapi&s=post&q=index&tags=" + tags); + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + AllocConsole(); + using (Stream stream = response.GetResponseStream()) + { + StreamReader reader = new StreamReader(stream, Encoding.UTF8); + String responseString = reader.ReadToEnd(); + doc.LoadXml(responseString); + root = doc.DocumentElement; + + XmlNodeList elemList = root.GetElementsByTagName("post"); + int count = 0; + for (int i = 0; i < elemList.Count; i++) + { + count++; + } + return Convert.ToInt32(count); + } + } + } +} diff --git a/Form1.Designer.cs b/Form1.Designer.cs index e179383..0dff249 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -36,6 +36,7 @@ private void InitializeComponent() this.label3 = new System.Windows.Forms.Label(); this.statusLabel = new System.Windows.Forms.Label(); this.checkBox1 = new System.Windows.Forms.CheckBox(); + this.isDanbooruSite = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // tagsBox @@ -59,7 +60,7 @@ private void InitializeComponent() // this.domainBox.Location = new System.Drawing.Point(74, 19); this.domainBox.Name = "domainBox"; - this.domainBox.Size = new System.Drawing.Size(320, 20); + this.domainBox.Size = new System.Drawing.Size(202, 20); this.domainBox.TabIndex = 2; this.domainBox.Text = "http://gelbooru.com"; // @@ -112,11 +113,22 @@ private void InitializeComponent() this.checkBox1.Text = "Keep original names"; this.checkBox1.UseVisualStyleBackColor = true; // + // isDanbooruSite + // + this.isDanbooruSite.AutoSize = true; + this.isDanbooruSite.Location = new System.Drawing.Point(283, 21); + this.isDanbooruSite.Name = "isDanbooruSite"; + this.isDanbooruSite.Size = new System.Drawing.Size(73, 17); + this.isDanbooruSite.TabIndex = 9; + this.isDanbooruSite.Text = "Danbooru"; + this.isDanbooruSite.UseVisualStyleBackColor = true; + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(624, 137); + this.Controls.Add(this.isDanbooruSite); this.Controls.Add(this.checkBox1); this.Controls.Add(this.statusLabel); this.Controls.Add(this.label3); @@ -143,6 +155,7 @@ private void InitializeComponent() private System.Windows.Forms.Label label3; private System.Windows.Forms.Label statusLabel; private System.Windows.Forms.CheckBox checkBox1; + private System.Windows.Forms.CheckBox isDanbooruSite; } } diff --git a/Form1.cs b/Form1.cs index 977eba0..27d4b6d 100644 --- a/Form1.cs +++ b/Form1.cs @@ -59,25 +59,52 @@ public void TagsLostFocus(object sender, EventArgs e) private async void downloadButton_Click(object sender, EventArgs e) { - GelEngine engine = new GelEngine(); - int postCount = engine.getPostCount(domainBox.Text, tagsBox.Text); - if (postCount == 0) - { - Console.WriteLine("No posts found by tag " + tagsBox.Text); - statusLabel.ForeColor = Color.Red; - statusLabel.Text = "No posts found."; + if (isDanbooruSite.Checked){//If it's a danbooru site + DanEngine engine = new DanEngine(); + int postCount = engine.getPostCount(domainBox.Text, tagsBox.Text); + if (postCount == 0) + { + Console.WriteLine("No posts found by tag " + tagsBox.Text); + statusLabel.ForeColor = Color.Red; + statusLabel.Text = "No posts found."; + } + else + { + statusLabel.ForeColor = Color.Blue; + statusLabel.Text = "Downloading..."; + for (int i = 0; i < postCount; i++) + { + await Task.Run(() => engine.downloadPosts(domainBox.Text, tagsBox.Text, i, checkBox1.Checked)); + } + statusLabel.ForeColor = Color.Green; + statusLabel.Text = "Ready."; + MessageBox.Show("Download compelete!", "GelDownloader", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); + } } else - { - statusLabel.ForeColor = Color.Blue; - statusLabel.Text = "Downloading..."; - for (int i = 0; i < postCount; i++) + {//If it's a gelbooru site + + GelEngine engine = new GelEngine(); + int postCount = engine.getPostCount(domainBox.Text, tagsBox.Text); + if (postCount == 0) { - await Task.Run(() => engine.downloadPosts(domainBox.Text, tagsBox.Text, i, checkBox1.Checked)); + Console.WriteLine("No posts found by tag " + tagsBox.Text); + statusLabel.ForeColor = Color.Red; + statusLabel.Text = "No posts found."; } - statusLabel.ForeColor = Color.Green; - statusLabel.Text = "Ready."; - MessageBox.Show("Download compelete!", "GelDownloader", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); + else + { + statusLabel.ForeColor = Color.Blue; + statusLabel.Text = "Downloading..."; + for (int i = 0; i < postCount; i++) + { + await Task.Run(() => engine.downloadPosts(domainBox.Text, tagsBox.Text, i, checkBox1.Checked)); + } + statusLabel.ForeColor = Color.Green; + statusLabel.Text = "Ready."; + MessageBox.Show("Download compelete!", "GelDownloader", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); + } + } } } diff --git a/GelDownloader.csproj b/GelDownloader.csproj index ea1d4d6..b9b661b 100644 --- a/GelDownloader.csproj +++ b/GelDownloader.csproj @@ -62,6 +62,7 @@ + Form