-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathUpdate.cs
181 lines (153 loc) · 6.21 KB
/
Update.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
namespace RoliSoft.TVShowTracker
{
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using RoliSoft.TVShowTracker.Parsers.Guides;
using RoliSoft.TVShowTracker.Parsers.Guides.Engines;
using RoliSoft.TVShowTracker.Remote;
/// <summary>
/// Provides methods to keep the database up-to-date.
/// </summary>
public class Updater
{
/// <summary>
/// Gets or sets a value indicating whether an update is currently in progress.
/// </summary>
/// <value>
/// <c>true</c> if an update is currently in progress; otherwise, <c>false</c>.
/// </value>
public static bool InProgress { get; set; }
/// <summary>
/// Occurs when the update is done.
/// </summary>
public event EventHandler<EventArgs> UpdateDone;
/// <summary>
/// Occurs when the update has encountered an error.
/// </summary>
public event EventHandler<EventArgs<string, Exception, bool, bool>> UpdateError;
/// <summary>
/// Occurs when the progress has changed on the update.
/// </summary>
public event EventHandler<EventArgs<string, double>> UpdateProgressChanged;
/// <summary>
/// Does the update.
/// </summary>
public void Update()
{
InProgress = true;
var i = 0d;
var cnt = Database.TVShows.Values.Count(s => s.Airing);
var ids = Database.TVShows.Values.Where(s => s.Airing).OrderBy(s => s.Title).Select(s => s.ID).ToList();
foreach (var id in ids)
{
UpdateProgressChanged.Fire(this, Database.TVShows[id].Title, ++i / cnt * 100);
var tv = Database.Update(Database.TVShows[id], (e, s) =>
{
if (e == -1)
{
UpdateError.Fire(this, s, null, true, false);
}
});
if (tv != null && tv.Language == "en")
{
UpdateRemoteCache(tv);
}
}
Database.Setting("update", DateTime.Now.ToUnixTimestamp().ToString());
MainWindow.Active.DataChanged();
UpdateDone.Fire(this);
InProgress = false;
}
/// <summary>
/// Does the update asynchronously.
/// </summary>
public void UpdateAsync()
{
Task.Factory.StartNew(() =>
{
try
{
Update();
}
catch (Exception ex)
{
UpdateError.Fire(this, "The update function has quit with an exception.", ex, false, true);
}
});
}
/// <summary>
/// Creates the guide object from name.
/// </summary>
/// <param name="name">The name of the class.</param>
/// <returns>New guide class.</returns>
/// <exception cref="NotSupportedException">When a name is specified which is not supported.</exception>
public static Guide CreateGuide(string name)
{
switch (name)
{
case "TVDB":
return new TVDB();
case "TVmaze":
return new TVmaze();
case "TMDb":
return new TMDb();
case "TVRage":
return new TVRage();
case "Freebase":
return new Freebase();
case "TVcom":
return new TVcom();
case "EPisodeWorld":
return new EPisodeWorld();
case "IMDb":
return new IMDb();
case "EPGuides":
return new EPGuides();
case "AniDB":
return new AniDB();
case "AnimeNewsNetwork":
return new AnimeNewsNetwork();
default:
throw new NotSupportedException();
}
}
/// <summary>
/// Sends metadata information about the TV show into lab.rolisoft.net cache.
/// </summary>
/// <param name="tv">The TV show.</param>
public static void UpdateRemoteCache(TVShow tv)
{
Task.Factory.StartNew(() =>
{
try
{
var info = new Remote.Objects.ShowInfo
{
Title = tv.Title,
Description = tv.Description,
Genre = tv.Genre,
Cover = tv.Cover,
Started = (long)tv.Episodes[0].Airdate.ToUnixTimestamp(),
Airing = tv.Airing,
AirTime = tv.AirTime,
AirDay = tv.AirDay,
Network = tv.Network,
Runtime = tv.Runtime,
Seasons = tv.Episodes.Last().Season,
Episodes = tv.Episodes.Count,
Source = tv.Source,
SourceID = tv.SourceID
};
var hash = BitConverter.ToString(new HMACSHA256(Encoding.ASCII.GetBytes(Utils.GetUUID() + "\0" + Signature.Version)).ComputeHash(Encoding.UTF8.GetBytes(
info.Title + info.Description + string.Join(string.Empty, info.Genre) + info.Cover + info.Started + (info.Airing ? "true" : "false") + info.AirTime + info.AirDay + info.Network + info.Runtime + info.Seasons + info.Episodes + info.Source + info.SourceID
))).ToLower().Replace("-", string.Empty);
API.SetShowInfo(info, hash);
} catch { }
});
}
}
}