Added progressbar and stuff

This commit is contained in:
Norbi Peti 2016-04-19 21:25:24 +02:00
parent 167002018f
commit 4ac73f7a03
4 changed files with 69 additions and 26 deletions

View file

@ -36,6 +36,7 @@
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
@ -120,15 +121,25 @@
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(402, 197);
this.textBox1.Size = new System.Drawing.Size(402, 199);
this.textBox1.TabIndex = 6;
this.textBox1.Text = resources.GetString("textBox1.Text");
//
// progressBar1
//
this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.progressBar1.Location = new System.Drawing.Point(16, 305);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(402, 23);
this.progressBar1.TabIndex = 7;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(440, 305);
this.ClientSize = new System.Drawing.Size(440, 340);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
@ -153,6 +164,7 @@
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ProgressBar progressBar1;
}
}

View file

@ -17,6 +17,7 @@ namespace DEIFR
InitializeComponent();
numericUpDown1.Value = Program.MaxImages;
checkBox1.Checked = Program.KeepImages;
Program.Progress = progressBar1;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)

View file

@ -14,41 +14,63 @@ namespace DEIFR
{
public static class ImageDownloader
{
private static List<JToken> data = null;
private static int i = 0;
private static List<string> files = null;
private static IEnumerator<JToken> enumerator;
public static void Update()
{
if (!Directory.Exists("wallpapers"))
Directory.CreateDirectory("wallpapers");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.reddit.com/r/ImagesOfEarth/.json?limit=" + Program.MaxImages + "&sort=new");
//string senddata = "?limit=15&sort=new";
//byte[] sendbytes = Encoding.Unicode.GetBytes(senddata);
req.Method = "GET";
//req.ContentLength = sendbytes.Length;
req.ContentType = "text/plain";
/*var sendstr = req.GetRequestStream();
sendstr.Write(sendbytes, 0, sendbytes.Length);
sendstr.Close();*/
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream respstr = resp.GetResponseStream();
string response = new StreamReader(respstr).ReadToEnd();
JObject obj = JObject.Parse(response);
List<JToken> data = obj["data"]["children"].Children().ToList();
int i = 0;
List<string> files = Directory.GetFiles("wallpapers").ToList();
foreach (JToken result in data)
using (WebClient client = new WebClient())
{
client.DownloadStringAsync(new Uri("http://www.reddit.com/r/EarthPorn/.json?limit=" + Program.MaxImages));
JObject obj = JObject.Parse(response);
data = obj["data"]["children"].Children().ToList();
i = 0;
files = Directory.GetFiles("wallpapers").ToList();
enumerator = data.GetEnumerator();
}
Task.Run(() =>
{
while (!Next())
;
});
}
private static void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Program.Form.Invoke(new Action(delegate
{
Program.Progress.Value = e.ProgressPercentage;
}));
}
private static void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
while (!Next())
;
}
private static bool Next()
{
if (enumerator.MoveNext())
{
JToken result = enumerator.Current;
string s = result["data"]["url"].ToString();
string id = result["data"]["id"].ToString();
if (files.Any(entry => entry.Contains(id)))
{
files.RemoveAll(entry => entry.Contains(id)); //Leave only the files that wasn't processed to remove them later
continue; //Don't download twice
return false; //Don't download twice
}
if (!s.Contains(".jpg"))
{
if (s.Contains("imgur.com"))
s += ".jpg"; //On imgur.com it is enough to get image
else
continue; //Otherwise we don't know what is there
return false; //Otherwise we don't know what is there
}
//Image.FromStream(respstr).Save(i + "." + s.Split('/').Last().Split('.')[1]);
string path = id + "." + s.Split('/').Last().Split('.')[1].Split('?')[0]; //?: FB and similar sites
@ -56,20 +78,26 @@ namespace DEIFR
{
try
{
client.DownloadFile(s, "wallpapers" + Path.DirectorySeparatorChar + path);
client.DownloadProgressChanged += Client_DownloadProgressChanged;
client.DownloadFileCompleted += Client_DownloadFileCompleted;
client.DownloadFileAsync(new Uri(s), "wallpapers" + Path.DirectorySeparatorChar + path);
}
catch { } //If the image is removed then don't do anything
}
files.Remove(path);
i++;
}
if (!Program.KeepImages)
else //Finish
{
foreach (string file in files)
if (!Program.KeepImages)
{
File.Delete(file);
foreach (string file in files)
{
File.Delete(file);
}
}
}
return true;
}
}
}

View file

@ -11,6 +11,8 @@ namespace DEIFR
{
public static bool KeepImages;
public static int MaxImages;
public static ProgressBar Progress;
public static Form1 Form;
/// <summary>
/// The main entry point for the application.
@ -45,7 +47,7 @@ namespace DEIFR
}
ImageDownloader.Update();
if (args.Length == 0)
Application.Run(new Form1());
Application.Run(Form = new Form1());
else if (args[0].ToLower() != "silent")
Console.WriteLine("Error: Unknown parameter(s). Use \"silent\" to open in background, otherwise don't give any parameters to show settings.");
List<string> sw = new List<string>();