-
Notifications
You must be signed in to change notification settings - Fork 7
/
DataCollectJob.cs
388 lines (309 loc) · 17.3 KB
/
DataCollectJob.cs
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using youtube_dl_viewer.Controller;
using youtube_dl_viewer.Util;
namespace youtube_dl_viewer.Jobs
{
public class DataCollectJob : Job
{
public readonly int Index;
public readonly bool ClearOld;
public string Result = null;
public (string json, Dictionary<string, JObject> obj)? FullResult = null;
private (int, int) _progress = (0, 1);
public override (int, int) Progress => _progress;
public DataCollectJob(AbsJobManager man, int index, bool clearOld) : base(man, "self::"+index)
{
Index = index;
ClearOld = clearOld;
}
public override string Name => $"DataCollect::{Index}::'{((Index>=0 && Index <= Program.Args.DataDirs.Count) ? Program.Args.DataDirs[Index].Name : "ERR")}'";
public override void Abort()
{
Console.Error.WriteLine($"Cannot abort Job [{Name}]");
}
protected override void Run()
{
if (ClearOld)
{
lock (Program.DataCache) Program.DataCache[Index] = (null, null);
}
var (jsonstr, jsonobj) = CreateData(Index);
lock (Program.DataCache)
{
Program.DataRefreshTimestamps[Index] = DateTime.Now;
Program.DataCache[Index] = (jsonstr, jsonobj);
Result = jsonstr;
FullResult = (jsonstr, jsonobj);
}
_progress = (1, 1);
ChangeState(JobState.Finished);
while (ProxyCount != 0) // Wait for proxies
{
if (AbortRequest) { ChangeState(JobState.Aborted); return; }
Thread.Sleep(100);
}
ChangeState(JobState.Success);
Result = null; // Memory cleanup
FullResult = null;
}
public (string jsonstr, Dictionary<string, JObject> jsonobj) CreateData(int index)
{
var ddir = Program.Args.DataDirs[index];
var datafiles = EnumerateMatchingFiles(ddir).OrderBy(p => p.ToLower()).ToList();
var processedFiles = new List<string>();
var filesSubs = datafiles.Where(p => p.EndsWith(".vtt")).ToList();
var filesInfo = datafiles.Where(p => p.EndsWith(".info.json")).ToList();
var resultVideos = new JArray();
var idsAreUnique = true;
var idlist = new HashSet<string>();
var cacheFiles = (Program.Args.CacheDir == null)
? new HashSet<string>()
: Directory.EnumerateFiles(Program.Args.CacheDir).Select(Path.GetFileName).ToHashSet();
var orderFileNeedsUpdate = false;
Dictionary<string, int> orderIndizes = null;
if (ddir.OrderFilename != null)
{
if (!File.Exists(ddir.FullOrderFilename)) throw new Exception($"Order file not found: '{ddir.FullOrderFilename}'");
orderIndizes = File
.ReadLines(ddir.FullOrderFilename)
.Where(p => !string.IsNullOrWhiteSpace(p))
.Select((v,i)=>(v,i))
.ToDictionary(p=>p.v, p=>p.i);
}
var filecount = filesInfo.Count;
int progr = 0;
foreach (var pathJson in filesInfo)
{
_progress = (progr, filecount+1);
progr++;
JObject jinfo;
try
{
jinfo = JObject.Parse(File.ReadAllText(pathJson));
}
catch (Exception e)
{
throw new Exception($"Could not parse file: '{pathJson}'", e);
}
var id = jinfo.Value<string>("id");
if (id == null || idlist.Contains(id)) idsAreUnique = false;
idlist.Add(id);
var dir = Path.GetDirectoryName(pathJson);
if (dir == null) continue;
var filenameJson = Path.GetFileName(pathJson);
var filenameBase = filenameJson.Substring(0, filenameJson.Length - ".info.json".Length);
var pathDesc = Path.Combine(dir, filenameBase + ".description");
if (!datafiles.Contains(pathDesc)) pathDesc = null;
var pathVideo = Program.ExtVideo.Select(ext => Path.Combine(dir, filenameBase + "." + ext)).FirstOrDefault(p => datafiles.Contains(p));
if (pathVideo == null) continue;
var pathThumb = Program.ExtThumbnail.Select(ext => Path.Combine(dir, filenameBase + "." + ext)).FirstOrDefault(p => datafiles.Contains(p));
var pathSubs = filesSubs
.Where(p => dir == Path.GetDirectoryName(p))
.Where(p => Path.GetFileName(p).EndsWith(".vtt"))
.Where(p => Path.GetFileName(p).StartsWith(filenameBase + "."))
.ToList();
processedFiles.Add(pathJson);
if (pathDesc != null) processedFiles.Add(pathDesc);
if (pathThumb != null) processedFiles.Add(pathThumb);
processedFiles.Add(pathVideo);
processedFiles.AddRange(pathSubs);
var vtitle = ddir.UseFilenameAsTitle
? Path.GetFileNameWithoutExtension(pathVideo)
: jinfo.Value<string>("fulltitle") ?? jinfo.Value<string>("title") ?? Path.GetFileName(pathVideo);
var descr = (pathDesc != null) ? File.ReadAllText(pathDesc) : jinfo.Value<string>("description");
int? order_index = null;
if (orderIndizes != null)
{
var key = jinfo.Value<string>("extractor") + " " + id;
if (orderIndizes.ContainsKey(key))
{
order_index = orderIndizes[key];
}
else
{
order_index = orderIndizes.Count;
orderIndizes.Add(key, order_index.Value);
orderFileNeedsUpdate = true;
Console.Out.WriteLine($"Updated Orderfile '{ddir.OrderFilename}': Added [{key}] at {order_index}");
}
}
if (Program.Args.TrimDataJSON) jinfo = TrimJSON(jinfo);
resultVideos.Add(new JObject
(
new JProperty("meta", new JObject
(
new JProperty("uid", id),
new JProperty("datadirindex", index),
new JProperty("directory", dir),
new JProperty("filename_base", filenameBase),
new JProperty("path_json", pathJson),
new JProperty("path_description", pathDesc),
new JProperty("path_video", pathVideo),
new JProperty("path_video_abs", Path.GetFullPath(pathVideo)),
new JProperty("path_thumbnail", pathThumb),
new JProperty("paths_subtitle", new JObject(pathSubs.Select(p => new JProperty(Path.GetFileNameWithoutExtension(p).Substring(filenameBase.Length+1), p)))),
new JProperty("cache_file", VideoController.GetStreamCachePath(pathVideo)),
new JProperty("cached", cacheFiles.Contains(Path.GetFileName(VideoController.GetStreamCachePath(pathVideo)))),
new JProperty("cached_video_fsize", cacheFiles.Contains(Path.GetFileName(VideoController.GetStreamCachePath(pathVideo))) ? new FileInfo(VideoController.GetStreamCachePath(pathVideo)).Length : 0),
new JProperty("previewscache_file", ThumbnailController.GetPreviewCachePath(pathVideo)),
new JProperty("cached_previews", cacheFiles.Contains(Path.GetFileName(ThumbnailController.GetPreviewCachePath(pathVideo)))),
new JProperty("cached_preview_fsize", cacheFiles.Contains(Path.GetFileName(ThumbnailController.GetPreviewCachePath(pathVideo))) ? new FileInfo(ThumbnailController.GetPreviewCachePath(pathVideo)).Length : 0),
new JProperty("ext_order_index", order_index)
)),
new JProperty("data", new JObject
(
new JProperty("title", vtitle),
new JProperty("description", descr),
new JProperty("info", jinfo)
))
));
}
var filesVideo = datafiles.Except(processedFiles).Where(p => Program.ExtVideo.Any(q => string.Equals("." + q, Path.GetExtension(p), StringComparison.CurrentCultureIgnoreCase))).ToList();
foreach (var pathVideo in filesVideo)
{
var id = pathVideo.Sha256();
if (id == null || idlist.Contains(id)) idsAreUnique = false;
idlist.Add(id);
var dir = Path.GetDirectoryName(pathVideo);
if (dir == null) continue;
var filenameVideo = Path.GetFileName(pathVideo);
var filenameBase = Path.GetFileNameWithoutExtension(filenameVideo);
var pathDesc = Path.Combine(dir, filenameBase + ".description");
if (!datafiles.Contains(pathDesc)) pathDesc = null;
var pathThumb = Program.ExtThumbnail.Select(ext => Path.Combine(dir, filenameBase + "." + ext)).FirstOrDefault(p => datafiles.Contains(p));
var pathSubs = filesSubs
.Where(p => dir == Path.GetDirectoryName(p))
.Where(p => Path.GetFileName(p).EndsWith(".vtt"))
.Where(p => Path.GetFileName(p).StartsWith(filenameBase + "."))
.ToList();
if (pathDesc != null) processedFiles.Add(pathDesc);
if (pathThumb != null) processedFiles.Add(pathThumb);
processedFiles.Add(pathVideo);
processedFiles.AddRange(pathSubs);
var vtitle = ddir.UseFilenameAsTitle ? Path.GetFileNameWithoutExtension(pathVideo) : Path.GetFileName(pathVideo);
int? order_index = null;
if (orderIndizes != null)
{
var key = "file" + " " + id;
if (orderIndizes.ContainsKey(key))
{
order_index = orderIndizes[key];
}
else
{
order_index = orderIndizes.Count;
orderIndizes.Add(key, order_index.Value);
orderFileNeedsUpdate = true;
Console.Out.WriteLine($"Updated Orderfile '{ddir.OrderFilename}': Added [{key}] at {order_index}");
}
}
resultVideos.Add(new JObject
(
new JProperty("meta", new JObject
(
new JProperty("uid", id),
new JProperty("directory", dir),
new JProperty("filename_base", filenameBase),
new JProperty("path_json", (object)null),
new JProperty("path_description", pathDesc),
new JProperty("path_video", pathVideo),
new JProperty("path_video_abs", Path.GetFullPath(pathVideo)),
new JProperty("path_thumbnail", pathThumb),
new JProperty("paths_subtitle", new JObject(pathSubs.Select(p => new JProperty(Path.GetFileNameWithoutExtension(p).Substring(filenameBase.Length+1), p)))),
new JProperty("cache_file", VideoController.GetStreamCachePath(pathVideo)),
new JProperty("cached", cacheFiles.Contains(Path.GetFileName(VideoController.GetStreamCachePath(pathVideo)))),
new JProperty("cached_video_fsize", cacheFiles.Contains(Path.GetFileName(VideoController.GetStreamCachePath(pathVideo))) ? new FileInfo(VideoController.GetStreamCachePath(pathVideo)).Length : 0),
new JProperty("previewscache_file", ThumbnailController.GetPreviewCachePath(pathVideo)),
new JProperty("cached_previews", cacheFiles.Contains(Path.GetFileName(ThumbnailController.GetPreviewCachePath(pathVideo)))),
new JProperty("cached_preview_fsize", cacheFiles.Contains(Path.GetFileName(ThumbnailController.GetPreviewCachePath(pathVideo))) ? new FileInfo(ThumbnailController.GetPreviewCachePath(pathVideo)).Length : 0),
new JProperty("ext_order_index", order_index)
)),
new JProperty("data", new JObject
(
new JProperty("title", vtitle),
new JProperty("description", (pathDesc != null) ? File.ReadAllText(pathDesc) : null),
new JProperty("info", new JObject())
))
));
}
if (!idsAreUnique)
{
var uid = 10000;
foreach (var rv in resultVideos)
{
rv["meta"]?["uid"]?.Replace(new JProperty("uid", uid.ToString()));
uid++;
}
}
if (orderFileNeedsUpdate)
{
File.WriteAllLines(ddir.FullOrderFilename, orderIndizes.Select(p => p).OrderBy(p => p.Value).Select(p => p.Key));
Console.Out.WriteLine($"new Orderfile written to '{ddir.FullOrderFilename}'");
}
var result = new JObject
(
new JProperty("meta", new JObject
(
new JProperty("htmltitle", ddir.HTMLTitle ?? Program.Args.HTMLTitle),
new JProperty("has_ext_order", orderIndizes != null),
new JProperty("count_total", filesInfo.Count + filesVideo.Count),
new JProperty("count_info", filesInfo.Count),
new JProperty("count_raw", filesVideo.Count),
new JProperty("display_override", ddir.DisplayOverride),
new JProperty("width_override", ddir.WidthOverride),
new JProperty("order_override", ddir.OrderOverride),
new JProperty("videomode_override", ddir.VideomodeOverride)
)),
new JProperty("videos", resultVideos),
new JProperty("missing", new JArray(datafiles.Except(processedFiles).ToArray<object>()))
);
var jsonstr = result.ToString(Formatting.Indented);
var jsonobj = resultVideos.ToDictionary(rv => rv["meta"]?.Value<string>("uid"), rv => (JObject) rv);
return (jsonstr, jsonobj);
}
private JObject TrimJSON(JObject jinfo)
{
foreach (var key in jinfo.Properties().Select(p => p.Name).ToList())
{
if (key == "upload_date") continue;
if (key == "title") continue;
if (key == "categories") continue;
if (key == "like_count") continue;
if (key == "dislike_count") continue;
if (key == "uploader") continue;
if (key == "channel_url") continue;
if (key == "uploader_url") continue;
if (key == "duration") continue;
if (key == "tags") continue;
if (key == "webpage_url") continue;
if (key == "view_count") continue;
if (key == "extractor") continue;
if (key == "width") continue;
if (key == "height") continue;
jinfo.Remove(key);
}
return jinfo;
}
private List<string> EnumerateMatchingFiles(DataDirSpec dds)
{
var mask = new Regex("^" + Regex.Escape(dds.FilenameFilter).Replace("\\*", ".*").Replace("\\?", ".") + "$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
return DirectoryExtension
.EnumerateDirectoryRecursive(dds.Path, dds.RecursionDepth)
.Where(p => mask.IsMatch(Path.GetFileName(p)))
.ToList();
}
public override JObject AsJson(string managerName, string queue)
{
var obj = base.AsJson(managerName, queue);
obj.Add(new JProperty("Index", Index));
return obj;
}
}
}