-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (98 loc) · 3.8 KB
/
Program.cs
File metadata and controls
117 lines (98 loc) · 3.8 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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace SeriesCapture
{
class Program
{
const string DefaultConfigPath = "./config.json";
const uint IntervalMinutes = 60;
/// <summary>
/// Series url and its download links
/// </summary>
static readonly Dictionary<string, HashSet<string>> _snapshot = new Dictionary<string, HashSet<string>>();
static readonly Zhuixinfan _zhuixinfan = new Zhuixinfan();
static Aria2Helper _aria2Helper = null;
static async Task Main(string[] args)
{
var configPath = ReadConfigPathFromArgs(args);
configPath = string.IsNullOrEmpty(configPath) ? DefaultConfigPath : configPath;
var configData = ReadData(configPath);
if (configData != null)
{
_aria2Helper = new Aria2Helper(configData.aria2Config.host, configData.aria2Config.token);
_snapshot.Clear();
Console.WriteLine("Taking a snapshot of all series start.");
foreach (var seriesData in configData.seriesData)
{
_snapshot.Add(seriesData.seriesUrl, new HashSet<string>(await _zhuixinfan.GetAllMagnetLinks(seriesData.seriesUrl)));
}
Console.WriteLine("Taking a snapshot of all series complete.");
}
while (true)
{
Console.WriteLine($"Schedule to grab series at {DateTime.Now + TimeSpan.FromMinutes(IntervalMinutes)}");
await Task.Delay(TimeSpan.FromMinutes(IntervalMinutes));
foreach (var seriesData in configData.seriesData)
{
var currentLinks = await _zhuixinfan.GetAllMagnetLinks(seriesData.seriesUrl);
var dirtyLinks = GetDirtyLinks(_snapshot[seriesData.seriesUrl], currentLinks);
if (dirtyLinks.Count > 0)
{
Console.WriteLine("Series update found!");
foreach (var dlink in dirtyLinks)
{
await _aria2Helper.AddDownload(dlink, seriesData.directory);
Console.WriteLine($"Added download task to {seriesData.directory}");
_snapshot[seriesData.seriesUrl].Add(dlink);
}
}
else
{
Console.WriteLine("No series update found.");
}
}
}
}
static string ReadConfigPathFromArgs(string[] args)
{
if (args.Length >= 1 && !string.IsNullOrEmpty(args[0]))
{
return args[0];
}
return null;
}
static Config ReadData(string path)
{
Console.WriteLine($"Reading config from {path}");
if (File.Exists(path))
{
string fileContents;
using (StreamReader sr = File.OpenText(path))
{
fileContents = sr.ReadToEnd();
}
return JsonConvert.DeserializeObject<Config>(fileContents);
}
else
{
Console.WriteLine("Config file not found");
return null;
}
}
static List<string> GetDirtyLinks(HashSet<string> oldLinks, List<string> newLinks)
{
var ret = new List<string>();
foreach (var newLink in newLinks)
{
if (!oldLinks.Contains(newLink))
{
ret.Add(newLink);
}
}
return ret;
}
}
}