-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
237 lines (203 loc) · 9.39 KB
/
Program.cs
File metadata and controls
237 lines (203 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using Microsoft.Win32;
using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
using static System.Net.Mime.MediaTypeNames;
namespace DownloadAPOD {
class Program {
//TODO: Download all images within a period
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
/*
* -h or --help: help
* YYMMDD: date in YYMMDD format
* -nw or --no-wallpaper: don't set as wallpaper
* -r or --random: random image
* -d or --default: save image to current directory
* */
static void Main(string[] args) {
//Make sure there are valid arguments
string dateArg = "";
bool shouldChooseRandom = false;
foreach (string arg in args) {
int dateInt = -1;
try {
dateInt = (Int32.Parse(arg));
} catch { }
if(dateInt != -1) {
dateArg += arg;
continue;
}
if (!arg.Equals("-d") && !arg.Equals("--default") && !arg.Equals("-r") && !arg.Equals("--random") && !arg.Equals("-h") && !arg.Equals("--help") && !arg.Equals("-nw") && !arg.Equals("--no-wallpaper")) {
Console.WriteLine("Invalid arguments. Use -h or --help to see available arguments");
Console.ReadKey();
Environment.Exit(0);
}
}
//If -h or --help is used as an argument
if(Array.Exists(args, element => element.Equals("-h") || element.Equals("--help"))) {
Console.WriteLine("Usage: -h or --help Help Menu");
Console.WriteLine(" -nw or --no-wallpaper Do not set as wallpaper");
Console.WriteLine(" -r or --random Random image");
Console.WriteLine(" -d or --default Save in this directory and do not create config file");
Console.WriteLine(" YYMMDD Specify a date in YYMMDD format");
Environment.Exit(0);
return;
}
//If -r or --random is used as an argument
if (Array.Exists(args, element => element.Equals("-r") || element.Equals("--random"))) {
Console.WriteLine("Choosing random image...");
shouldChooseRandom = true;
}
string htmlFileLocation = @"apod.txt";
string configFileLocation = @"config.txt";
string imageFileName = "apod.jpg";
string imageFileLocation = "";
//If the default flags are used
if (Array.Exists(args, element => element.Equals("-d") || element.Equals("--default"))) {
//Use this location and do not create a config file
imageFileLocation = System.Reflection.Assembly.GetExecutingAssembly().Location.ToString();
} else {
//Create config file if it does not exist
if (!File.Exists(configFileLocation)) {
Console.WriteLine("Enter the save location for the image or leave blank to store in program directory.");
File.WriteAllText(configFileLocation, Console.ReadLine());
if (!File.ReadAllText(configFileLocation).EndsWith("\\") && !File.ReadAllText(configFileLocation).Equals("")) {
File.WriteAllText(configFileLocation, File.ReadAllText(configFileLocation) + "\\");
}
}
//Read from config file
imageFileLocation = File.ReadAllText(configFileLocation);
if (imageFileLocation.Equals("")) {
imageFileLocation = System.Reflection.Assembly.GetExecutingAssembly().Location.ToString();
File.WriteAllText(configFileLocation, imageFileLocation);
}
}
//Set up dateString
string dateString = "";
DateTime date;
if (dateArg.Equals("")) {
if (shouldChooseRandom) {
//TODO: Display day that is downloading in file name
Random rand = new Random();
DateTime firstImage = new DateTime(1995, 6, 16);
int range = (DateTime.Today - firstImage).Days;
date = firstImage.AddDays(rand.Next(range));
} else {
date = DateTime.Now;
}
Console.WriteLine("Image Date: " + date.ToString("d"));
dateString = date.Year + "";
dateString = dateString.Substring(2, dateString.Length - 2);
switch (date.Month) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
dateString = dateString + "0" + date.Month;
break;
case 10:
case 11:
case 12:
dateString = dateString + date.Month;
break;
}
if (date.Day >= 1 && date.Day <= 9) {
dateString = dateString + "0" + date.Day;
} else {
dateString = dateString + date.Day;
}
} else {
dateString += dateArg;
}
//Use dateString to make apodImageToDownload
string apodImageToDownloadHTML = "https://apod.nasa.gov/apod/ap" + dateString +".html";
//Download HTML as TXT
using(WebClient client = new WebClient()) {
try {
client.DownloadFile(apodImageToDownloadHTML, htmlFileLocation);
} catch {
Console.WriteLine("Failed to download image. Make sure you choose a valid date.");
Console.ReadKey();
Environment.Exit(0);
}
}
Console.WriteLine("Downloading HTML from: " + apodImageToDownloadHTML);
//Find part of TXT that has the image url
string wantedLine = "";
foreach(var line in File.ReadAllLines(htmlFileLocation)) {
if(line.Contains("<a href=\"image")) {
wantedLine = line;
break;
}
}
//Stop execution if there is no image
if (wantedLine == "") {
Console.WriteLine("\n\nNo image found on " + apodImageToDownloadHTML);
Console.WriteLine("Try again tomorrow!");
Console.ReadLine();
return;
}
//Remove unwanted parts from URL ending
wantedLine = wantedLine.Trim();
int beginningIndex = wantedLine.IndexOf('i');
int endIndex = wantedLine.IndexOf('\"', beginningIndex);
string imageURLEnd = wantedLine.Substring(beginningIndex, endIndex-beginningIndex);
Console.WriteLine("Image Downloading: " + imageURLEnd);
//Download Image
string imageURL = "https://apod.nasa.gov/apod/" + imageURLEnd;
Console.WriteLine("Image URL: " + imageURL);
using (WebClient client = new WebClient()) {
client.DownloadFile(imageURL, imageFileLocation + dateString + imageFileName);
}
//Delete HTML file
File.Delete(htmlFileLocation);
//Console.WriteLine("\nDeleting: " + htmlFileLocation);
Console.WriteLine("Image saved to " + imageFileLocation + dateString + imageFileName);
//Set wallpaper
if (!Array.Exists(args, element => element.Equals("-nw") && !element.Equals("--no-wallpaper"))) {
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
key.SetValue(@"WallpaperStyle", 10.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
SystemParametersInfo(20, 0, imageFileLocation + dateString + imageFileName, 0x01 | 0x02);
Console.WriteLine("Image set as wallpaper");
return;
}
Environment.Exit(0);
}
}
}
/*
if (style == Style.Fill) {
key.SetValue(@"WallpaperStyle", 10.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Fit) {
key.SetValue(@"WallpaperStyle", 6.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Span) // Windows 8 or newer only!
{
key.SetValue(@"WallpaperStyle", 22.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Stretch) {
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Tile) {
key.SetValue(@"WallpaperStyle", 0.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
if (style == Style.Center) {
key.SetValue(@"WallpaperStyle", 0.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
*/