From 261c67fe9b9becafc34b9d44a574884d95d61233 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 10 Jan 2015 21:59:14 +0000 Subject: [PATCH] Initial upload. :P --- DEIFR/App.config | 6 ++ DEIFR/DEIFR.csproj | 93 ++++++++++++++++++ DEIFR/Form1.Designer.cs | 131 +++++++++++++++++++++++++ DEIFR/Form1.cs | 33 +++++++ DEIFR/Form1.resx | 120 ++++++++++++++++++++++ DEIFR/ImageDownloader.cs | 71 ++++++++++++++ DEIFR/Program.cs | 51 ++++++++++ DEIFR/Properties/AssemblyInfo.cs | 36 +++++++ DEIFR/Properties/Resources.Designer.cs | 71 ++++++++++++++ DEIFR/Properties/Resources.resx | 117 ++++++++++++++++++++++ DEIFR/Properties/Settings.Designer.cs | 30 ++++++ DEIFR/Properties/Settings.settings | 7 ++ 12 files changed, 766 insertions(+) create mode 100644 DEIFR/App.config create mode 100644 DEIFR/DEIFR.csproj create mode 100644 DEIFR/Form1.Designer.cs create mode 100644 DEIFR/Form1.cs create mode 100644 DEIFR/Form1.resx create mode 100644 DEIFR/ImageDownloader.cs create mode 100644 DEIFR/Program.cs create mode 100644 DEIFR/Properties/AssemblyInfo.cs create mode 100644 DEIFR/Properties/Resources.Designer.cs create mode 100644 DEIFR/Properties/Resources.resx create mode 100644 DEIFR/Properties/Settings.Designer.cs create mode 100644 DEIFR/Properties/Settings.settings diff --git a/DEIFR/App.config b/DEIFR/App.config new file mode 100644 index 0000000..fad249e --- /dev/null +++ b/DEIFR/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DEIFR/DEIFR.csproj b/DEIFR/DEIFR.csproj new file mode 100644 index 0000000..0196ce8 --- /dev/null +++ b/DEIFR/DEIFR.csproj @@ -0,0 +1,93 @@ + + + + + Debug + AnyCPU + {885CF010-4AC8-407C-97F7-1542411709E5} + WinExe + Properties + DEIFR + DEIFR + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + bin\Debug\Newtonsoft.Json.dll + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + \ No newline at end of file diff --git a/DEIFR/Form1.Designer.cs b/DEIFR/Form1.Designer.cs new file mode 100644 index 0000000..feb650e --- /dev/null +++ b/DEIFR/Form1.Designer.cs @@ -0,0 +1,131 @@ +namespace DEIFR +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); + this.label1 = new System.Windows.Forms.Label(); + this.checkBox1 = new System.Windows.Forms.CheckBox(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.label4 = new System.Windows.Forms.Label(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(); + this.SuspendLayout(); + // + // numericUpDown1 + // + this.numericUpDown1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.numericUpDown1.Location = new System.Drawing.Point(304, 13); + this.numericUpDown1.Name = "numericUpDown1"; + this.numericUpDown1.Size = new System.Drawing.Size(120, 20); + this.numericUpDown1.TabIndex = 0; + this.numericUpDown1.Value = new decimal(new int[] { + 15, + 0, + 0, + 0}); + this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(13, 13); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(140, 13); + this.label1.TabIndex = 1; + this.label1.Text = "Maximum number of images:"; + // + // checkBox1 + // + this.checkBox1.AutoSize = true; + this.checkBox1.Location = new System.Drawing.Point(16, 33); + this.checkBox1.Name = "checkBox1"; + this.checkBox1.Size = new System.Drawing.Size(123, 17); + this.checkBox1.TabIndex = 2; + this.checkBox1.Text = "Keep current images"; + this.checkBox1.UseVisualStyleBackColor = true; + this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged); + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(13, 53); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(279, 13); + this.label2.TabIndex = 3; + this.label2.Text = "It will remove all unrelated files too if you uncheck this box"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(13, 66); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(196, 13); + this.label3.TabIndex = 4; + this.label3.Text = "Settings are saved automatically on exit."; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(13, 79); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(405, 13); + this.label4.TabIndex = 5; + this.label4.Text = "If you want to automatically start the program on boot, provide the \"silent\" pare" + + "meter."; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(440, 118); + this.Controls.Add(this.label4); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.checkBox1); + this.Controls.Add(this.label1); + this.Controls.Add(this.numericUpDown1); + this.Name = "Form1"; + this.Text = "DEIFR - Download Earth Images From Reddit"; + ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.NumericUpDown numericUpDown1; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.CheckBox checkBox1; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Label label4; + } +} + diff --git a/DEIFR/Form1.cs b/DEIFR/Form1.cs new file mode 100644 index 0000000..d947856 --- /dev/null +++ b/DEIFR/Form1.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace DEIFR +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + numericUpDown1.Value = Program.MaxImages; + checkBox1.Checked = Program.KeepImages; + } + + private void checkBox1_CheckedChanged(object sender, EventArgs e) + { + Program.KeepImages = checkBox1.Checked; + } + + private void numericUpDown1_ValueChanged(object sender, EventArgs e) + { + Program.MaxImages = (int)numericUpDown1.Value; + numericUpDown1.Value = Program.MaxImages; //A way to disallow non-integer inputs + } + } +} diff --git a/DEIFR/Form1.resx b/DEIFR/Form1.resx new file mode 100644 index 0000000..29dcb1b --- /dev/null +++ b/DEIFR/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/DEIFR/ImageDownloader.cs b/DEIFR/ImageDownloader.cs new file mode 100644 index 0000000..4dabde4 --- /dev/null +++ b/DEIFR/ImageDownloader.cs @@ -0,0 +1,71 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace DEIFR +{ + public static class ImageDownloader + { + 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) + { + 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 + } + 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 + } + //Image.FromStream(respstr).Save(i + "." + s.Split('/').Last().Split('.')[1]); + string path = id + "." + s.Split('/').Last().Split('.')[1]; + using (WebClient client = new WebClient()) + { + client.DownloadFile(s, "wallpapers" + Path.DirectorySeparatorChar + path); + } + files.Remove(path); + i++; + } + if (!Program.KeepImages) + { + foreach (string file in files) + { + File.Delete(file); + } + } + } + } +} diff --git a/DEIFR/Program.cs b/DEIFR/Program.cs new file mode 100644 index 0000000..355710b --- /dev/null +++ b/DEIFR/Program.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace DEIFR +{ + static class Program + { + public static bool KeepImages; + public static int MaxImages; + + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main(string[] args) + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + if (File.Exists("DEIFRSettings.ini")) + { + string[] ss = File.ReadAllLines("DEIFRSettings.ini"); + foreach (var s in ss) + { + string[] x = s.Split('='); + switch (x[0]) + { + case "keepimages": + KeepImages = bool.Parse(x[1]); + break; + case "maximages": + MaxImages = int.Parse(x[1]); + break; + } + } + } + ImageDownloader.Update(); + if (args.Length == 0) + Application.Run(new Form1()); + else if (args[0] != "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(); + sw.Add("maximages=" + MaxImages); + sw.Add("keepimages=" + KeepImages); + File.WriteAllLines("DEIFRSettings.ini", sw); + } + } +} diff --git a/DEIFR/Properties/AssemblyInfo.cs b/DEIFR/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ed4d1ae --- /dev/null +++ b/DEIFR/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("DEIFR")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("DEIFR")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e45015ee-fc55-40d5-b34d-ad96a7511aa3")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DEIFR/Properties/Resources.Designer.cs b/DEIFR/Properties/Resources.Designer.cs new file mode 100644 index 0000000..37c9fbc --- /dev/null +++ b/DEIFR/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.18444 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace DEIFR.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DEIFR.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/DEIFR/Properties/Resources.resx b/DEIFR/Properties/Resources.resx new file mode 100644 index 0000000..ffecec8 --- /dev/null +++ b/DEIFR/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/DEIFR/Properties/Settings.Designer.cs b/DEIFR/Properties/Settings.Designer.cs new file mode 100644 index 0000000..342d20d --- /dev/null +++ b/DEIFR/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.18444 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace DEIFR.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/DEIFR/Properties/Settings.settings b/DEIFR/Properties/Settings.settings new file mode 100644 index 0000000..abf36c5 --- /dev/null +++ b/DEIFR/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + +