Skip to content

Commit

Permalink
Add Danbooru engine. I haven't got it checked yet because danbooru is
Browse files Browse the repository at this point in the history
blocked in my country :(
  • Loading branch information
fgRuslan committed Mar 30, 2019
1 parent 35a99bc commit f6888ba
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 16 deletions.
100 changes: 100 additions & 0 deletions DanEngine.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
15 changes: 14 additions & 1 deletion Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 42 additions & 15 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
}
}
Expand Down
1 change: 1 addition & 0 deletions GelDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DanEngine.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
Expand Down

0 comments on commit f6888ba

Please sign in to comment.