From 4ac73f7a03d8152c857067522308349f58ef3fe0 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 19 Apr 2016 21:25:24 +0200 Subject: [PATCH] Added progressbar and stuff --- DEIFR/Form1.Designer.cs | 16 +++++++-- DEIFR/Form1.cs | 1 + DEIFR/ImageDownloader.cs | 74 +++++++++++++++++++++++++++------------- DEIFR/Program.cs | 4 ++- 4 files changed, 69 insertions(+), 26 deletions(-) diff --git a/DEIFR/Form1.Designer.cs b/DEIFR/Form1.Designer.cs index e50e6d0..6032b6e 100644 --- a/DEIFR/Form1.Designer.cs +++ b/DEIFR/Form1.Designer.cs @@ -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; } } diff --git a/DEIFR/Form1.cs b/DEIFR/Form1.cs index d947856..584b8de 100644 --- a/DEIFR/Form1.cs +++ b/DEIFR/Form1.cs @@ -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) diff --git a/DEIFR/ImageDownloader.cs b/DEIFR/ImageDownloader.cs index 2761279..b56c7a5 100644 --- a/DEIFR/ImageDownloader.cs +++ b/DEIFR/ImageDownloader.cs @@ -14,41 +14,63 @@ namespace DEIFR { public static class ImageDownloader { + private static List data = null; + private static int i = 0; + private static List files = null; + private static IEnumerator 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 data = obj["data"]["children"].Children().ToList(); - int i = 0; - List 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; } } } diff --git a/DEIFR/Program.cs b/DEIFR/Program.cs index 5a218f3..0358bb2 100644 --- a/DEIFR/Program.cs +++ b/DEIFR/Program.cs @@ -11,6 +11,8 @@ namespace DEIFR { public static bool KeepImages; public static int MaxImages; + public static ProgressBar Progress; + public static Form1 Form; /// /// 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 sw = new List();