-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapod.linq
185 lines (134 loc) · 3.96 KB
/
apod.linq
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
<Query Kind="Program">
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>System.Drawing</Namespace>
<Namespace>System.Globalization</Namespace>
<Namespace>System.Security.Principal</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
#load ".\GetEntries"
#load ".\GetImageStream"
#load ".\GetImageUrl"
#load ".\SetWallpaper"
const string HISTORY_FILE_NAME = "history.json";
const int MINIMUM_HEIGHT = 1200;
const int MINIMUM_WIDTH = 1920;
async Task Main()
{
Util.AutoScrollResults = true;
if (!IsAdmin())
{
"Not admin.".Dump();
return;
}
var history = LoadHistory();
Directory.CreateDirectory(GetImageDirectoryPath());
var entries = await GetEntriesAsync();
entries.Dump();
foreach(var pageUrl in entries)
{
$"Page URL: {pageUrl}".Dump();
var historyEntry = GetOrCreateHistoryEntry(history, pageUrl);
if (historyEntry.Processed)
{
"Already processed.".Dump();
OutputBreak();
continue;
}
var imageUrl = await GetImageUrl(pageUrl);
if (imageUrl == null)
{
OutputBreak();
continue;
}
$"Image URL: {imageUrl}".Dump();
historyEntry.ImageURL = imageUrl;
var imageStream = await GetImageStream(imageUrl);
var image = Image.FromStream(imageStream);
image.Dump(Util.ScaleMode.ResizeTo(null, 400));
$"{image.Width} x {image.Height}".Dump();
if (image.Width < MINIMUM_WIDTH || image.Height < MINIMUM_HEIGHT)
{
$"Skipping. Too small.".Dump();
OutputBreakAndSetProcessed(historyEntry);
continue;
}
var shouldSaveAndSetInput = Util.ReadLine<string>("Save and set?");
historyEntry.Processed = true;
if (!shouldSaveAndSetInput.StartsWith("y", ignoreCase: true, CultureInfo.InvariantCulture))
{
OutputBreakAndSetProcessed(historyEntry);
continue;
}
var imageFilePath = SaveImage(imageUrl, imageStream);
SetWallpaper(imageFilePath);
OutputBreakAndSetProcessed(historyEntry);
break;
}
SaveHistory(history);
}
string GetFullHistoryPath()
=> Path.Combine(GetScriptDirectoryPath(), HISTORY_FILE_NAME);
string GetImageDirectoryPath()
=> Path.Combine(GetScriptDirectoryPath(), "images");
HistoryEntry GetOrCreateHistoryEntry(List<HistoryEntry> history, string pageUrl)
{
var historyEntry = history.SingleOrDefault(e => e.PageURL == pageUrl);
if (historyEntry == null)
{
historyEntry = new HistoryEntry
{
PageURL = pageUrl
};
history.Add(historyEntry);
}
return historyEntry;
}
string GetScriptDirectoryPath() => Path.GetDirectoryName(Util.CurrentQueryPath);
public static bool IsAdmin()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
List<HistoryEntry> LoadHistory()
{
var historyPath = GetFullHistoryPath();
if (!File.Exists(historyPath))
return new List<HistoryEntry>();
var historyJson = File.ReadAllText(historyPath);
return JsonConvert.DeserializeObject<List<HistoryEntry>>(historyJson);
}
void OutputBreak()
{
new String('-', 10).Dump();
}
void OutputBreakAndSetProcessed(HistoryEntry historyEntry)
{
OutputBreak();
historyEntry.Processed = true;
}
void SaveHistory(List<HistoryEntry> history)
{
var historyPath = GetFullHistoryPath();
var historyJson = JsonConvert.SerializeObject(history, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(historyPath, historyJson);
}
string SaveImage(string imageUrl, Stream imageStream)
{
var imageFileName = new Flurl.Url(imageUrl).PathSegments[^1];
imageFileName.Dump();
var imageFullPath = Path.Combine(GetImageDirectoryPath(), imageFileName);
using (var fileStream = new FileStream(imageFullPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
{
imageStream.Seek(0, SeekOrigin.Begin);
imageStream.CopyTo(fileStream);
}
return imageFullPath;
}
class HistoryEntry
{
public string PageURL { get; set; }
public string ImageURL { get; set; }
public bool Processed { get; set; }
}