-
Notifications
You must be signed in to change notification settings - Fork 20
/
Database.cs
1010 lines (840 loc) · 33.3 KB
/
Database.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace RoliSoft.TVShowTracker
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Text;
using Newtonsoft.Json;
using SharpCompress.Archive.Zip;
using SharpCompress.Common;
using SharpCompress.Writer;
using RoliSoft.TVShowTracker.Parsers.ForeignTitles;
using RoliSoft.TVShowTracker.Parsers.Guides;
/// <summary>
/// Provides access to the default database.
/// </summary>
public static class Database
{
/// <summary>
/// Gets or sets the date when the data was last changed. This field is used for caching purposes, and it's not automatically updated by <c>Execute()</c>.
/// </summary>
/// <value>The date of last change.</value>
public static DateTime DataChange { get; set; }
/// <summary>
/// Gets or sets the contents of the TV shows table in the database.
/// </summary>
/// <value>
/// The contents of the TV shows table in the database.
/// </value>
public static Dictionary<int, TVShow> TVShows { get; set; }
/// <summary>
/// Gets or sets the key-value store associated with this database.
/// </summary>
/// <value>The data.</value>
public static Dictionary<string, string> Data { get; set; }
/// <summary>
/// Gets or sets the database directory.
/// </summary>
/// <value>
/// The database directory.
/// </value>
public static readonly string DataPath = Path.Combine(Signature.InstallPath, "db");
/// <summary>
/// Initializes the <see cref="Database"/> class.
/// </summary>
static Database()
{
if (string.IsNullOrWhiteSpace(Signature.InstallPath))
{
Log.Fatal("Stopping database initialization because $InstallPath is null or empty.");
return;
}
if (!Directory.Exists(DataPath))
{
Log.Info("Creating database folder: " + DataPath);
Directory.CreateDirectory(DataPath);
}
var tmp = Path.Combine(Signature.UACVirtualPath, "db");
if (Directory.Exists(tmp))
{
Log.Info("$UACVirtualPath\\db exists, initiating migration to $DataPath...");
foreach (var dir in Directory.EnumerateDirectories(tmp))
{
var fn = Path.GetFileName(dir);
var nir = Path.Combine(DataPath, fn);
Log.Info("Migrating " + fn + "...");
if (Directory.Exists(nir) || !File.Exists(Path.Combine(dir, "info")) || !File.Exists(Path.Combine(dir, "conf")))
{
Log.Info(fn + " already exists in the target directory or is not a valid TV show directory.");
continue;
}
try
{
var show = TVShow.Load(dir);
show.ID += 1000;
show.Directory = nir;
Directory.CreateDirectory(show.Directory);
show.Save();
show.SaveTracking();
Directory.Delete(dir, true);
}
catch (Exception ex)
{
Log.Warn("Exception while migrating data.", ex);
continue;
}
}
Log.Info("Migration finished.");
}
LoadDatabase();
}
/// <summary>
/// Loads the database files.
/// </summary>
public static void LoadDatabase()
{
Log.Info("Loading database...");
DataChange = DateTime.Now;
TVShows = new Dictionary<int, TVShow>();
foreach (var dir in Directory.EnumerateDirectories(DataPath))
{
var fn = Path.GetFileName(dir);
if (Log.IsTraceEnabled) Log.Trace("Reading " + fn + "...");
if (!File.Exists(Path.Combine(dir, "info")) || !File.Exists(Path.Combine(dir, "conf")))
{
Log.Warn(fn + " is not a valid TV show directory.");
continue;
}
try
{
var show = TVShow.Load(dir);
TVShows[show.ID] = show;
}
catch (Exception ex)
{
Log.Error("Error while loading TV show data " + fn + ".", ex);
continue;
//MainWindow.HandleUnexpectedException(new Exception("Couldn't load db\\" + fn + " due to an error.", ex));
}
}
Log.Debug("Database loaded in " + (DateTime.Now - DataChange).TotalSeconds + "s, containing " + TVShows.Count + " shows.");
Log.Info("UUID is " + Utils.GetUUID());
}
/// <summary>
/// Loads the data from the database.
/// </summary>
/// <returns>
/// <c>true</c> on success; otherwise, <c>false</c>.
/// </returns>
public static bool LoadData()
{
if (!File.Exists(Path.Combine(DataPath, "conf")))
{
Log.Debug("$DataPath\\conf doesn't exist.");
return false;
}
using (var fs = File.OpenRead(Path.Combine(DataPath, "conf")))
using (var br = new BinaryReader(fs))
{
var dver = br.ReadByte();
var dupd = br.ReadUInt32();
var dcnt = br.ReadUInt16();
Data = new Dictionary<string, string>();
for (var i = 0; i < dcnt; i++)
{
Data[br.ReadString()] = br.ReadString();
}
}
return true;
}
/// <summary>
/// Retrieves the key from the SQL settings.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>Stored value or empty string.</returns>
public static string Setting(string key)
{
if (Data == null && !LoadData())
{
return string.Empty;
}
string value;
if (Data.TryGetValue(key, out value))
{
return value;
}
return string.Empty;
}
/// <summary>
/// Stores the key and value into the SQL settings.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public static void Setting(string key, string value)
{
if (Data == null && !LoadData())
{
Data = new Dictionary<string, string>();
}
Data[key] = value;
using (var fs = File.OpenWrite(Path.Combine(DataPath, "conf")))
using (var bw = new BinaryWriter(fs))
{
bw.Write((byte)1);
bw.Write((uint)DateTime.Now.ToUnixTimestamp());
bw.Write((ushort)Data.Count);
foreach (var kv in Data)
{
bw.Write(kv.Key);
bw.Write(kv.Value);
}
}
}
/// <summary>
/// Saves a custom dictionary to the specified file in the database.
/// </summary>
/// <param name="name">The name in the database.</param>
/// <param name="dict">The dictionary to serialize.</param>
public static void SaveDict(string name, Dictionary<int, string> dict)
{
var path = Path.Combine(Signature.InstallPath, name);
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
using (var fs = File.OpenWrite(path))
using (var bw = new BinaryWriter(fs))
{
bw.Write((byte)1);
bw.Write((uint)DateTime.Now.ToUnixTimestamp());
bw.Write((uint)dict.Count);
foreach (var kv in dict)
{
bw.Write(kv.Key);
bw.Write(kv.Value ?? string.Empty);
}
}
}
/// <summary>
/// Saves a custom dictionary to the specified file in the database.
/// </summary>
/// <param name="name">The name in the database.</param>
/// <param name="dict">The dictionary to serialize.</param>
public static void SaveDict(string name, Dictionary<string, int> dict)
{
var path = Path.Combine(Signature.InstallPath, name);
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
using (var fs = File.OpenWrite(path))
using (var bw = new BinaryWriter(fs))
{
bw.Write((byte)1);
bw.Write((uint)DateTime.Now.ToUnixTimestamp());
bw.Write((uint)dict.Count);
foreach (var kv in dict)
{
bw.Write(kv.Key ?? string.Empty);
bw.Write(kv.Value);
}
}
}
/// <summary>
/// Saves a custom dictionary to the specified file in the database.
/// </summary>
/// <param name="name">The name in the database.</param>
/// <param name="dict">The dictionary to serialize.</param>
public static void SaveDict(string name, Dictionary<string, string> dict)
{
var path = Path.Combine(Signature.InstallPath, name);
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
using (var fs = File.OpenWrite(path))
using (var bw = new BinaryWriter(fs))
{
bw.Write((byte)1);
bw.Write((uint)DateTime.Now.ToUnixTimestamp());
bw.Write((uint)dict.Count);
foreach (var kv in dict)
{
bw.Write(kv.Key ?? string.Empty);
bw.Write(kv.Value ?? string.Empty);
}
}
}
/// <summary>
/// Loads a custom dictionary from the specified file in the database.
/// </summary>
/// <param name="name">The name in the database.</param>
/// <returns>
/// Deserialized dictionary.
/// </returns>
public static Dictionary<string, string> LoadDictStrStr(string name)
{
using (var fs = File.OpenRead(Path.Combine(Path.GetDirectoryName(DataPath), name)))
using (var br = new BinaryReader(fs))
{
var ver = br.ReadByte();
var upd = br.ReadUInt32();
var cnt = br.ReadUInt32();
var dat = new Dictionary<string, string>();
for (var i = 0; i < cnt; i++)
{
dat[br.ReadString()] = br.ReadString();
}
return dat;
}
}
/// <summary>
/// Loads a custom dictionary from the specified file in the database.
/// </summary>
/// <param name="name">The name in the database.</param>
/// <returns>
/// Deserialized dictionary.
/// </returns>
public static Dictionary<int, string> LoadDictIntStr(string name)
{
using (var fs = File.OpenRead(Path.Combine(Path.GetDirectoryName(DataPath), name)))
using (var br = new BinaryReader(fs))
{
var ver = br.ReadByte();
var upd = br.ReadUInt32();
var cnt = br.ReadUInt32();
var dat = new Dictionary<int, string>();
for (var i = 0; i < cnt; i++)
{
dat[br.ReadInt32()] = br.ReadString();
}
return dat;
}
}
/// <summary>
/// Loads a custom dictionary from the specified file in the database.
/// </summary>
/// <param name="name">The name in the database.</param>
/// <returns>
/// Deserialized dictionary.
/// </returns>
public static Dictionary<string, int> LoadDictStrInt(string name)
{
using (var fs = File.OpenRead(Path.Combine(Path.GetDirectoryName(DataPath), name)))
using (var br = new BinaryReader(fs))
{
var ver = br.ReadByte();
var upd = br.ReadUInt32();
var cnt = br.ReadUInt32();
var dat = new Dictionary<string, int>();
for (var i = 0; i < cnt; i++)
{
dat[br.ReadString()] = br.ReadInt32();
}
return dat;
}
}
/// <summary>
/// Adds the specified TV show to the database.
/// </summary>
/// <param name="sid">The show ID to add to the database.</param>
/// <param name="callback">The status callback.</param>
/// <returns>Added TV show or <c>null</c>.</returns>
public static TVShow Add(ShowID sid, Action<int, string> callback = null)
{
Log.Info("Adding " + sid.Guide.Name + "/" + sid.ID + "...");
var st = DateTime.Now;
if (callback != null)
{
callback(0, "Downloading guide from " + sid.Guide.Name + "...");
}
TVShow tv;
try
{
tv = sid.Guide.GetData(sid.ID, sid.Language);
if (tv.Episodes.Count == 0)
{
Log.Error("Downloaded guide for " + tv.Title + " (" + sid.Guide.Name + "/" + sid.ID + ") has no episodes.");
if (callback != null)
{
callback(-1, "No episodes listed for this show on " + sid.Guide.Name + ".");
}
return null;
}
}
catch (Exception ex)
{
if (ex is ThreadAbortException)
{
Log.Warn("Thread aborted while downloading data from guide for " + sid.Guide.Name + "/" + sid.ID + ".", ex);
return null;
}
Log.Error("Error while downloading data from guide for " + sid.Guide.Name + "/" + sid.ID + ".", ex);
if (callback != null)
{
callback(-1, "Error while downloading data: " + ex.Message);
}
return null;
}
if (TVShows.Values.FirstOrDefault(x => x.Title == tv.Title) != null)
{
Log.Error("Duplicate entry detected for " + tv.Title + " (" + sid.Guide.Name + "/" + sid.ID + ").");
if (callback != null)
{
callback(-1, tv.Title + " is already in your database.");
}
return null;
}
foreach (var tvs in TVShows.Values)
{
tvs.RowID++;
tvs.SaveData();
}
tv.RowID = 0;
tv.ID = TVShows.Values.Count > 0 ? TVShows.Values.Max(x => x.ID) + 1 : 1;
tv.Data = new Dictionary<string, string>();
tv.Directory = Path.Combine(DataPath, Utils.CreateSlug(tv.Title, false));
tv.EpisodeByID = new Dictionary<int, Episode>();
if (Directory.Exists(tv.Directory))
{
tv.Directory += "-" + tv.Source.ToLower();
}
if (Directory.Exists(tv.Directory))
{
tv.Directory += "-" + Utils.Rand.Next();
}
try
{
Directory.CreateDirectory(tv.Directory);
}
catch (Exception ex)
{
Log.Error("Error while creating directory db\\" + Path.GetFileName(tv.Directory) + " for " + tv.Title + " (" + sid.Guide.Name + "/" + sid.ID + ").", ex);
if (callback != null)
{
callback(-1, "Error while creating database.");
}
return null;
}
foreach (var ep in tv.Episodes)
{
ep.Show = tv;
ep.ID = ep.Number + (ep.Season * 1000) + (tv.ID * 1000 * 1000);
tv.EpisodeByID[ep.Number + (ep.Season * 1000)] = ep;
if (!string.IsNullOrWhiteSpace(tv.AirTime) && ep.Airdate != Utils.UnixEpoch)
{
try { ep.Airdate = DateTime.Parse(ep.Airdate.ToString("yyyy-MM-dd ") + tv.AirTime).ToLocalTimeZone(tv.TimeZone); } catch { }
}
}
try
{
tv.Save();
tv.SaveTracking();
}
catch (Exception ex)
{
Log.Error("Error while saving database to db\\" + Path.GetFileName(tv.Directory) + " for " + tv.Title + " (" + sid.Guide.Name + "/" + sid.ID + ").", ex);
if (callback != null)
{
callback(-1, "Error while saving to database.");
}
return null;
}
TVShows[tv.ID] = tv;
DataChange = DateTime.Now;
if (tv.Language == "en")
{
Updater.UpdateRemoteCache(tv);
}
if (callback != null)
{
callback(1, "Show added successfully.");
}
Log.Debug("Successfully added " + tv.Title + " (" + sid.Guide.Name + "/" + sid.ID + ") in " + (DateTime.Now - st).TotalSeconds + "s.");
return tv;
}
/// <summary>
/// Updates the specified TV show in the database.
/// </summary>
/// <param name="show">The TV show to update.</param>
/// <param name="callback">The status callback.</param>
/// <returns>
/// Updated TV show or <c>null</c>.
/// </returns>
public static TVShow Update(TVShow show, Action<int, string> callback = null)
{
Log.Info("Updating " + show.Title + "...");
var st = DateTime.Now;
if (callback != null)
{
callback(0, "Updating " + show.Title + "...");
}
Guide guide;
try
{
guide = Updater.CreateGuide(show.Source);
}
catch (Exception ex)
{
Log.Error("Error while creating guide object for " + show.Title + ".", ex);
if (callback != null)
{
callback(-1, "Could not get guide object of type " + show.Source + " for " + show.Title + ".");
}
return null;
}
TVShow tv;
try
{
tv = guide.GetData(show.SourceID, show.Language);
}
catch (Exception ex)
{
Log.Error("Error while downloading data from guide for " + show.Title + ".", ex);
if (callback != null)
{
callback(-1, "Could not get guide data for " + show.Source + "#" + show.SourceID + ".");
}
return null;
}
tv.ID = show.ID;
tv.Data = show.Data;
tv.Directory = show.Directory;
tv.EpisodeByID = new Dictionary<int, Episode>();
if (tv.Title != show.Title)
{
tv.Title = show.Title;
}
foreach (var ep in tv.Episodes)
{
ep.Show = tv;
ep.ID = ep.Number + (ep.Season * 1000) + (tv.ID * 1000 * 1000);
tv.EpisodeByID[ep.Number + (ep.Season * 1000)] = ep;
Episode op;
if (show.EpisodeByID.TryGetValue(ep.Number + (ep.Season * 1000), out op) && op.Watched)
{
ep.Watched = true;
}
if (!string.IsNullOrWhiteSpace(tv.AirTime) && ep.Airdate != Utils.UnixEpoch)
{
ep.Airdate = DateTime.Parse(ep.Airdate.ToString("yyyy-MM-dd ") + tv.AirTime).ToLocalTimeZone(tv.TimeZone);
}
}
try
{
tv.Save();
}
catch (Exception ex)
{
Log.Error("Error while saving updated database for " + show.Title + ".", ex);
if (callback != null)
{
callback(-1, "Could not save database for " + show.Title + ".");
}
return null;
}
TVShows[tv.ID] = tv;
DataChange = DateTime.Now;
if (callback != null)
{
callback(1, "Updated " + show.Title + ".");
}
Log.Debug("Successfully updated " + show.Title + " in " + (DateTime.Now - st).TotalSeconds + "s.");
return tv;
}
/// <summary>
/// Removes the specified TV show from the database.
/// </summary>
/// <param name="show">The TV show to remove.</param>
/// <param name="callback">The status callback.</param>
/// <returns>
/// <c>true</c> on success; otherwise, <c>false</c>.
/// </returns>
public static bool Remove(TVShow show, Action<int, string> callback = null)
{
Log.Info("Removing " + show.Title + "...");
if (callback != null)
{
callback(0, "Removing " + show.Title + "...");
}
try
{
Directory.Delete(show.Directory, true);
}
catch (Exception ex)
{
Log.Error("Error while removing " + show.Title + ".", ex);
if (callback != null)
{
callback(-1, "Could not remove database for " + show.Title + ".");
}
return false;
}
TVShows.Remove(show.ID);
DataChange = DateTime.Now;
if (Library.Files != null && Library.Files.Count != 0)
{
foreach (var ep in Library.Files)
{
if (Math.Floor((double)ep.Key / 1000 / 1000) == show.ID)
{
ep.Value.Clear();
}
}
Library.SaveList();
}
if (callback != null)
{
callback(1, "Removed " + show.Title + ".");
}
return true;
}
/// <summary>
/// Gets the name of the show used in scene releases.
/// </summary>
/// <param name="show">The name of the show.</param>
/// <returns>Name of the show used in scene releases.</returns>
public static Regex GetReleaseName(string show)
{
var release = TVShows.Values.Where(s => s.Title == show).Take(1).ToList();
if (release.Count != 0 && !string.IsNullOrWhiteSpace(release[0].Data.Get("regex")))
{
return new Regex(release[0].Data["regex"]);
}
return ShowNames.Parser.GenerateTitleRegex(show, release.FirstOrDefault());
}
/// <summary>
/// Gets the foreign title of the specified show.
/// </summary>
/// <param name="id">The ID of the show.</param>
/// <param name="language">The ISO 639-1 code of the language.</param>
/// <param name="askRemote">if set to <c>true</c> lab.rolisoft.net's API will be asked then a foreign title provider engine.</param>
/// <param name="statusCallback">The method to call to report a status change.</param>
/// <returns>Foreign title or <c>null</c>.</returns>
public static string GetForeignTitle(int id, string language, bool askRemote = false, Action<string> statusCallback = null)
{
string title;
if (TVShows[id].Data.TryGetValue("title." + language, out title))
{
if (Regex.IsMatch(title, @"^!\d{10}$"))
{
if ((DateTime.Now.ToUnixTimestamp() - int.Parse(title.Substring(1))) < 2629743)
{
// don't search again if the not-found-tag is not older than a month
return null;
}
}
else
{
return title;
}
}
if (!askRemote)
{
return null;
}
if (statusCallback != null)
{
statusCallback("Searching for the " + Languages.List[language] + " title of " + TVShows[id].Title +" on lab.rolisoft.net...");
}
var api = Remote.API.GetForeignTitle(TVShows[id].Title, language);
if (api.Success && !string.IsNullOrWhiteSpace(api.Result))
{
if (api.Result == "!")
{
TVShows[id].Data["title." + language] = "!" + DateTime.Now.ToUnixTimestamp();
TVShows[id].SaveData();
return null;
}
TVShows[id].Data["title." + language] = api.Result;
TVShows[id].SaveData();
return api.Result;
}
var engine = Extensibility.GetNewInstances<ForeignTitleEngine>().FirstOrDefault(x => x.Language == language);
if (engine != null)
{
if (statusCallback != null)
{
statusCallback("Searching for the " + Languages.List[language] + " title of " + TVShows[id].Title + " on " + engine.Name + "...");
}
var search = engine.Search(TVShows[id].Title);
if (!string.IsNullOrWhiteSpace(search))
{
TVShows[id].Data["title." + language] = search;
TVShows[id].SaveData();
new Thread(() => Remote.API.SetForeignTitle(TVShows[id].Title, search, language)).Start();
return search;
}
}
TVShows[id].Data["title." + language] = "!" + DateTime.Now.ToUnixTimestamp();
TVShows[id].SaveData();
new Thread(() => Remote.API.SetForeignTitle(TVShows[id].Title, "!", language)).Start();
return null;
}
/// <summary>
/// Backups the database to the specified file.
/// </summary>
/// <param name="archive">The archive.</param>
/// <param name="callback">The status callback.</param>
/// <param name="covers">if set to <c>true</c> covers will be saved as well.</param>
/// <param name="settings">if set to <c>true</c> settings will be saved as well.</param>
public static void Backup(string archive, Action<int, string> callback = null, bool covers = false, bool settings = false)
{
if (File.Exists(archive))
{
try
{
File.Delete(archive);
}
catch (Exception ex)
{
if (callback != null)
{
callback(-1, "Unable to delete existing destination file.");
}
Log.Error("Error while deleting existing archive file.", ex);
return;
}
}
if (callback != null)
{
callback(0, "Creating archive...");
}
try
{
using (var fs = File.OpenWrite(archive))
using (var zip = WriterFactory.Open(fs, ArchiveType.Zip, CompressionType.Deflate))
{
if (File.Exists(Path.Combine(DataPath, "conf")))
{
zip.Write("conf", Path.Combine(DataPath, "conf"));
}
foreach (var show in TVShows.Values)
{
if (callback != null)
{
callback(0, "Saving " + show.Title + "...");
}
var dir = Path.GetFileName(show.Directory);
zip.Write(Path.Combine(dir, "info"), Path.Combine(show.Directory, "info"));
zip.Write(Path.Combine(dir, "conf"), Path.Combine(show.Directory, "conf"));
if (File.Exists(Path.Combine(show.Directory, "seen")))
{
zip.Write(Path.Combine(dir, "seen"), Path.Combine(show.Directory, "seen"));
}
if (covers)
{
var cover = CoverManager.GetCoverLocation(show.Title);
if (File.Exists(cover))
{
zip.Write(Path.Combine(dir, "cover"), cover);
}
}
}
if (settings)
{
if (callback != null)
{
callback(0, "Saving settings...");
}
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Settings.Keys, Formatting.None))))
{
ms.Position = 0;
zip.Write("json", ms);
}
}
}
}
catch (Exception ex)
{
if (callback != null)
{
callback(-1, "Error while archiving database.");
}
Log.Error("Error while writing files into archive.", ex);
return;
}
if (callback != null)
{
callback(1, "Database successfully backed up.");
}
}
/// <summary>
/// Inspects the specified backup archive.
/// </summary>
/// <param name="archive">The archive.</param>
/// <returns>Summary of the archive.</returns>
public static ArchiveInfo Inspect(string archive)
{
try
{
var arc = new ArchiveInfo();
var zip = ZipArchive.Open(archive);
if (zip.Entries.Any(e => e.FilePath == "json"))
{
arc.Settings = true;
}
if (zip.Entries.Any(e => e.FilePath.EndsWith("/cover")))
{
arc.Covers = true;
}
foreach (var file in zip.Entries.Where(e => e.FilePath.EndsWith("/info")))
{
var dir = Path.GetFileName(Path.GetDirectoryName(file.FilePath));
try
{
using (var fs = file.OpenEntryStream())
using (var br = new BinaryReader(fs))
{
var ver = br.ReadByte();
var upd = br.ReadUInt32();
var title = br.ReadString();
var guide = br.ReadString();
arc.Shows.Add(new[] { dir, title, guide });
}
}
catch (Exception ex)
{
Log.Warn("Unable to process " + dir + "/info in backup due to error.", ex);
}
}
return arc;
}
catch (Exception ex)
{
Log.Error("Error while processing archive.", ex);
}
return null;
}
/// <summary>
/// Represents a database backup archive's attributes.
/// </summary>
public class ArchiveInfo
{
/// <summary>
/// Gets or sets a value indicating whether this archive has settings.
/// </summary>
/// <value>
/// <c>true</c> if has settings; otherwise, <c>false</c>.
/// </value>
public bool Settings { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this archive has covers.
/// </summary>
/// <value>
/// <c>true</c> if has covers; otherwise, <c>false</c>.
/// </value>
public bool Covers { get; set; }