-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathAaxExporter.cs
More file actions
328 lines (263 loc) · 10.4 KB
/
Copy pathAaxExporter.cs
File metadata and controls
328 lines (263 loc) · 10.4 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using core.audiamus.aux;
using core.audiamus.aux.ex;
using core.audiamus.booksdb;
using core.audiamus.booksdb.ex;
using core.audiamus.connect.ex;
using core.audiamus.util;
using R = core.audiamus.connect.Properties.Resources;
using static core.audiamus.aux.Logging;
namespace core.audiamus.connect {
public class AaxExporter {
const string JSON = ".json";
const string CONTENT_METADATA = "content_metadata_";
const string SERIES_TITLES = "series_titles_";
private static readonly object __lockable = new object ();
private IExportSettings ExportSettings { get; }
private IMultiPartSettings MultipartSettings { get; }
public IBookLibrary BookLibrary { private get; set; }
public AaxExporter (IExportSettings exportSettings, IMultiPartSettings multipartSettings) {
ExportSettings = exportSettings;
MultipartSettings = multipartSettings;
}
public void Export (Book book, SimpleConversionContext context, Action<Conversion> onNewStateCallback) {
using var _ = new LogGuard (3, this, () => book.ToString ());
var accuChapters = new List<List<ChapterExtract>> ();
bool useComponentExport = book.Components.Count > 0
&& (MultipartSettings.MultiPartDownload || book.DeliveryType == EDeliveryType.Periodical);
if (!useComponentExport)
exportSinglePart (book, context, onNewStateCallback, accuChapters);
else
exportMultiPart (book, context, onNewStateCallback, accuChapters);
}
private void exportSinglePart (
IBookCommon book,
SimpleConversionContext context,
Action<Conversion> onNewStateCallback,
List<List<ChapterExtract>> accuChapters,
bool skipSeries = false
) {
Log (3, this, () => book.ToString ());
book.Conversion.State = EConversionState.converting;
onNewStateCallback?.Invoke (book.Conversion);
bool succ = copyFile (book, context);
if (!succ) {
book.Conversion.State = EConversionState.conversion_error;
onNewStateCallback?.Invoke (book.Conversion);
return;
}
exportChapters (book, accuChapters);
exportProduct (book);
if (!skipSeries)
exportSeries (book);
BookLibrary.SavePersistentState (book.Conversion, EConversionState.exported);
onNewStateCallback?.Invoke (book.Conversion);
}
private void exportMultiPart (
Book book,
SimpleConversionContext context,
Action<Conversion> onNewStateCallback,
List<List<ChapterExtract>> accuChapters
) {
Log (3, this, () => book.ToString ());
bool skipSeries = false;
foreach (var comp in book.Components) {
exportSinglePart (comp, context, onNewStateCallback, accuChapters, skipSeries);
skipSeries = true;
}
}
private bool copyFile (IBookCommon book, SimpleConversionContext context) {
Log (3, this, () => book.ToString ());
Conversion conv = book.Conversion;
string sourcefile = (conv.DownloadFileName + R.DecryptedFileExt).AsUncIfLong();
if (!File.Exists (sourcefile))
return false;
string filename = conv.DownloadFileName.GetDownloadFileNameWithoutExtension ();
string destfile = Path.Combine (ExportSettings.ExportDirectory, filename + R.ExportedFileExt).AsUncIfLong();
try {
lock (__lockable) {
bool succ = FileEx.Copy (sourcefile, destfile, true,
pm => context.Progress?.Report (pm),
() => context.CancellationToken.IsCancellationRequested
);
return succ;
}
} catch (Exception exc) {
Log (1, this, () => exc.Summary ());
}
return false;
}
// internal instead of private for testing only
internal string exportChapters (IBookCommon book, List<List<ChapterExtract>> accuChapters) {
if (book.ChapterInfo is null)
BookLibrary?.GetChapters (book);
if (book.ChapterInfo is null)
return null;
Log (3, this, () => book.ToString ());
var chapterInfo = book.ChapterInfo;
var cr = new adb.json.ContentReference {
asin = book.Asin,
content_size_in_bytes = book.FileSizeBytes ?? 0,
sku = book.Sku
};
var ci = new adb.json.ChapterInfo ();
var metadata = new adb.json.ContentMetadata {
chapter_info = ci,
content_reference = cr
};
var container = new adb.json.MetadataContainer {
content_metadata = metadata
};
ci.brandIntroDurationMs = chapterInfo.BrandIntroDurationMs;
ci.brandOutroDurationMs = chapterInfo.BrandOutroDurationMs;
ci.is_accurate = chapterInfo.IsAccurate ?? false;
ci.runtime_length_ms = chapterInfo.RuntimeLengthMs;
ci.runtime_length_sec = chapterInfo.RuntimeLengthMs / 1000;
var partAccuChapters = new List<List<ChapterExtract>> ();
var flattenedChapters = BookLibrary?.GetChaptersFlattened (book, partAccuChapters);
if (!flattenedChapters.IsNullOrEmpty()) {
var chapters = new List<adb.json.Chapter> ();
foreach (var chapter in flattenedChapters) {
if (chapters.Count == 0 && skipChapter (chapter, accuChapters))
continue;
var ch = new adb.json.Chapter {
length_ms = chapter.LengthMs,
start_offset_ms = chapter.StartOffsetMs,
start_offset_sec = chapter.StartOffsetMs / 1000,
title = chapter.Title
};
chapters.Add (ch);
}
ci.chapters = chapters.ToArray ();
}
string json = container.Serialize ();
json = json.CompactJson ();
string filename = CONTENT_METADATA + chapterInfo.BookMeta.Asin + JSON;
string outpath = Path.Combine (ExportSettings.ExportDirectory, filename).AsUncIfLong();
File.WriteAllText (outpath, json);
updateAccuChapters (accuChapters, partAccuChapters);
return outpath;
}
private bool skipChapter (Chapter ch, List<List<ChapterExtract>> accuChapters) {
if (accuChapters.Count < 2)
return false;
for (int i = 0; i < accuChapters.Count - 1; i++) {
var chextr = accuChapters[i].FirstOrDefault (ce =>
string.Equals (ce.Title, ch.Title) &&
Math.Abs (ce.Length - ch.LengthMs) < 1500 && ch.LengthMs < 25000);
if (chextr is not null)
return true;
}
return false;
}
private void updateAccuChapters (List<List<ChapterExtract>> accuChapters, List<List<ChapterExtract>> accuPart) {
if (accuPart.IsNullOrEmpty ())
return;
for (int i = 0; i < accuPart.Count; i++) {
if (accuChapters.Count < i + 1)
accuChapters.Add (new List<ChapterExtract> ());
if (!accuPart[i].IsNullOrEmpty ())
accuChapters[i].AddRange (accuPart[i]);
}
}
private void exportProduct (IBookCommon book) {
Log (3, this, () => book.ToString ());
var product = makeProduct (book);
var container = new adb.json.ProductResponse {
product = product
};
string json = container.Serialize ();
json = json.CompactJson ();
string filename = book.Asin + JSON;
string outpath = Path.Combine (ExportSettings.ExportDirectory, filename).AsUncIfLong();
File.WriteAllText (outpath, json);
}
private void exportSeries (IBookCommon prod) {
Book book = prod.GetBook ();
if (book.Series.IsNullOrEmpty ())
return;
Log (3, this, () => book.ToString ());
foreach (var serbook in book.Series) {
var series = serbook.Series;
string asin = series.Asin;
var products = new List<adb.json.Product> ();
// sort by sort/num+sub/sequence
IOrderedEnumerable<SeriesBook> sbks;
if (!series.Books.Where (b => b.Sort is null).Any())
sbks = series.Books.OrderBy (b => b.Sort);
else if (!series.Books.Where (b => b.BookNumber == 0).Any())
sbks = series.Books.OrderBy (b => b.BookNumber).ThenBy (b => b.SubNumber);
else
sbks = series.Books.OrderBy (b => b.Sequence);
foreach (var sbk in sbks) {
var p = makeProduct (sbk.Book);
products.Add (p);
}
var container = new adb.json.SimsBySeriesResponse {
similar_products = products.ToArray ()
};
string json = container.Serialize ();
json = json.CompactJson ();
string filename = SERIES_TITLES + asin + JSON;
string outpath = Path.Combine (ExportSettings.ExportDirectory, filename).AsUncIfLong();
File.WriteAllText (outpath, json);
}
}
private adb.json.Product makeProduct (IBookCommon prod) {
Book book = prod.GetBook ();
Log (3, this, () => book.ToString ());
// has_children;is_adult_product;is_listenable
// asin
// authors:name
// title
// series:title,sequence
// sku; sku_lite
var product = new adb.json.Product {
asin = prod.Asin,
title = prod.Title,
sku = prod.Sku,
sku_lite = prod.SkuLite,
is_listenable = true,
runtime_length_min = prod.RunTimeLengthSeconds / 60,
has_children = prod is Book && book.Components.Count > 0,
is_adult_product = book.AdultProduct ?? false
};
if (!book.Authors.IsNullOrEmpty ()) {
var authors = new List<adb.json.Author> ();
foreach (var author in book.Authors) {
var a = new adb.json.Author {
asin = author.Asin,
name = author.Name
};
authors.Add (a);
}
product.authors = authors.ToArray ();
}
if (!book.Series.IsNullOrEmpty()) {
var series = new List<adb.json.Series> ();
foreach (var serbook in book.Series) {
var s = new adb.json.Series {
asin = serbook.Series.Asin,
title = serbook.Series.Title,
sequence = serbook.SeqString
};
series.Add (s);
}
product.series = series.ToArray ();
} else if (prod is Component comp && !book.Title.IsNullOrWhiteSpace ()) {
// Some podcast episodes arrive without explicit series links. Keep season grouping stable.
product.series = new[] {
new adb.json.Series {
asin = book.Asin,
title = book.Title,
sequence = comp.PartNumber > 0 ? comp.PartNumber.ToString () : null
}
};
}
return product;
}
}
}