-
Notifications
You must be signed in to change notification settings - Fork 0
/
comicdownloader.csx
55 lines (45 loc) · 1.67 KB
/
comicdownloader.csx
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
#! "netcoreapp2.0"
#load "AppConfig.csx"
#load "FileSystem.csx"
#load "WebClient.csx"
#r "nuget: Newtonsoft.Json, 10.0.3"
using Newtonsoft.Json;
var minify = Args.Contains("m");
var createCBZ = Args.Contains("a");
var config = JsonConvert.DeserializeObject<AppConfig>(File.ReadAllText("config.json"));
var fileSystem = new FileSystem(config.ComicName, minify);
var webClient = new WebClient(config.AddressOfFirstComic, config.LoginCookie);
// TODO: Unify same comic from multiple sources, detect duplicates
// TOOD: Do login as part of script
var progress = fileSystem.LoadProgress(webClient.PathOfFirstComic);
var comicPath = progress.PathOfFirstComic;
var n = progress.Progress;
while (true) {
n++;
var start = DateTime.UtcNow;
var comicHTML = await webClient.GetComicHTML(comicPath);
var imageAddress = comicHTML.GetComicPath();
var comicDate = comicHTML.GetComicDate();
var extension = Path.GetExtension(imageAddress);
var image = await webClient.DownloadComic(imageAddress);
fileSystem.SaveComic(comicDate, extension, image, n);
fileSystem.SaveProgress(comicPath, n);
var nextComic = comicHTML.GetNextComic();
if (!nextComic.Exists) {
break;
}
comicPath = nextComic.Path;
var end = DateTime.UtcNow;
var waitTime = (int)(1000 - (end - start).TotalMilliseconds);
if (waitTime > 0) {
Console.WriteLine($"Sleeping {waitTime}ms");
await Task.Delay(waitTime);
}
}
if (createCBZ) {
fileSystem.CreateCBZ();
}
if (Directory.Exists(config.DropLocation)) {
fileSystem.CopyToDropLocation(config.DropLocation);
}
Console.WriteLine("Fin");