forked from vrokolos/VrokSub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Classes.cs
501 lines (462 loc) · 18.3 KB
/
Classes.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
using System.Collections.Generic;
using System.ComponentModel;
using CookComputing.XmlRpc;
using System.IO;
using System;
namespace OSDbClient
{
public class imdbdata
{
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string year { get; set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string id { get; set; }
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string duration { get; set; }
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string[] certification {get; set; }
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string cover {get; set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public XmlRpcStruct cast {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string[] genres {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string[] aka {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string plot {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string title {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string trivia {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string rating {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public XmlRpcStruct directors {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string tagline {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string votes {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string[] country {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string goofs {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string[] language {get;set;}
[XmlRpcMissingMapping(MappingAction.Ignore)]
public XmlRpcStruct writers {get;set;}
}
public class subRes
{
public string IDSubtitle { get; set; }
public string IDSubtitleFile { get; set; }
public string SubAuthorComment { get; set; }
public string LanguageName { get; set; }
public string UserID { get; set; }
public string MovieNameEng { get; set; }
public string MovieByteSize { get; set; }
public string IDMovie { get; set; }
public string MovieYear { get; set; }
public string SubHash { get; set; }
public string MovieHash { get; set; }
public string SubFormat { get; set; }
public string SubBad { get; set; }
public string SubDownloadsCnt { get; set; }
public string SubLanguageID { get; set; }
public string IDMovieImdb { get; set; }
public string MovieTimeMS { get; set; }
public string SubActualCD { get; set; }
public string MovieReleaseName { get; set; }
public string SubRating { get; set; }
public string SubDownloadLink { get; set; }
public string ZipDownloadLink { get; set; }
public string IDSubMovieFile { get; set; }
public string ISO639 { get; set; }
public string SubSumCD { get; set; }
public string SubSize { get; set; }
public string UserNickName { get; set; }
public string SubAddDate { get; set; }
public string MovieName { get; set; }
public string MovieImdbRating { get; set; }
public string SubFileName { get; set; }
}
public class subrt
{
public subRes[] data { get; set; }
public double seconds { get; set; }
}
public class subInfo
{
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string sublanguageid { get; set; }
public string moviehash { get; set; }
public string moviebytesize { get; set; }
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string imdbid { get; set; }
}
public class imdbheader
{
[XmlRpcMissingMapping(MappingAction.Ignore)]
public double seconds { get; set; }
[XmlRpcMissingMapping(MappingAction.Ignore)]
public imdbdata data;
}
public class subdata
{
public string status { get; set; }
[XmlRpcMissingMapping(MappingAction.Ignore)]
public subtitle[] data;
public double seconds { get; set; }
}
public class subtitle
{
public string idsubtitlefile { get; set; }
public string data { get; set; }
}
public class MovieFile
{
public byte[] cover { get; set; }
public string hash { get; set; }
public string filename { get; set; }
public string originalfilename { get; set; }
public string originalfolder { get; set; }
public subRes subRes { get; set; }
public byte[] subtitle { get; set; }
public string subtitleId { get; set; }
public string oldSubtitle { get; set; }
public imdbdata imdbinfo { get; set; }
public string oldSubtitleLang { get; set; }
public string GetSubtitleFileName()
{
return Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + "." + subRes.ISO639 + "." + subRes.SubFormat;
}
public string GetName()
{
return Path.GetFileNameWithoutExtension(filename);
}
public void saveSubtitle(bool overwrite)
{
string filenameToWrite = GetSubtitleFileName();
if (File.Exists(filenameToWrite))
{
if (overwrite)
{
File.Delete(filenameToWrite);
}
else
{
return;
}
}
FileStream fStream = new FileStream(GetSubtitleFileName(), FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fStream);
bw.Write(subtitle);
bw.Close();
fStream.Close();
}
public void getOldSubtitle(List<string> subtitleFormats, List<langMap> theLangMap, string langSeq)
{
List<string> langSeqSplit = new List<string>(langSeq.Split(','));
FileInfo myf = new FileInfo(filename);
string[] theFiles = Directory.GetFiles(myf.DirectoryName, Path.GetFileNameWithoutExtension(filename) + ".*");
string bestOldSubFile = "";
string bestOldSubFileLang = "";
foreach (string file in theFiles)
{
if (file == filename) { continue; }
string ext = Path.GetExtension(file);
if (subtitleFormats.Contains(ext.Replace(".", "")))
{
if ((langSeqSplit.Count == 1) || (langSeqSplit.Contains("el") && (langSeqSplit.Count == 2)))
{
if (file.Contains(langSeqSplit[0]))
{
bestOldSubFile = file;
}
}
else
{
string l = Utils.parseLangCodeFromFile(file, theLangMap);
if (bestOldSubFile == "")
{
bestOldSubFile = file;
if (l != "") { bestOldSubFileLang = l; }
continue;
}
if ((l != "") && (langSeqSplit.IndexOf(l) != -1))
{
if (bestOldSubFileLang == "")
{
bestOldSubFileLang = l;
bestOldSubFile = file;
continue;
}
if (langSeqSplit.IndexOf(l) < langSeqSplit.IndexOf(bestOldSubFileLang))
{
bestOldSubFile = file;
bestOldSubFileLang = l;
}
}
}
}
}
oldSubtitle = bestOldSubFile;
oldSubtitleLang = bestOldSubFileLang;
if ((langSeqSplit.Count == 1) || (langSeqSplit.Contains("el") && (langSeqSplit.Count == 2)))
{
if (!oldSubtitle.Contains(langSeqSplit[0]))
{
oldSubtitle = "";
oldSubtitleLang = "";
}
}
}
public bool SelectBestSubtitle(BindingList<subRes> subs, List<string> ls)
{
subRes best = null;
foreach (subRes tr in subs)
{
if (hash == tr.MovieHash)
{
if (best == null) { best = tr; continue; }
if (ls.IndexOf(tr.SubLanguageID) < ls.IndexOf(best.SubLanguageID))
{
best = tr;
}
}
}
if (best != null)
{
foreach (subRes tr in subs)
{
if (hash == tr.MovieHash)
{
if (best.SubLanguageID == tr.SubLanguageID)
{
try
{
if (float.Parse(tr.SubRating) > float.Parse(best.SubRating))
{
best = tr;
}
}
catch (Exception ex)
{
}
}
}
}
}
if (best != null)
{
subtitleId = best.IDSubtitleFile;
subRes = best;
return true;
}
return false;
}
public void rename(string FileFormat, string CDFormat)
{
if (subRes != null)
{
string cdStr = "";
if (subRes.SubActualCD == "1")
{
if (subRes.SubSumCD != "1")
{
cdStr = CDFormat.Replace("%C", "1");
}
}
else if (subRes.SubActualCD != "")
{
cdStr = CDFormat.Replace("%C", subRes.SubActualCD);
}
// }
/* if (subRes.SubActualCD == "1")
{
if (subRes.SubSumCD != "1")
{
cdStr = "-CD1";
}
}
else if (subRes.SubActualCD != "")
{
cdStr = "-CD" + subRes.SubActualCD;
}*/
if (subRes.MovieName != "")
{
string newName = Path.GetDirectoryName(filename) + "\\" + FileFormat + Path.GetExtension(filename);
newName = newName.Replace("%M", Utils.FilenameFromTitle(subRes.MovieName));
newName = newName.Replace("%Y", subRes.MovieYear);
newName = newName.Replace("%C", cdStr);
// string newName = Path.GetDirectoryName(filename) + "\\" + FilenameFromTitle(subRes.MovieName) + "(" + subRes.MovieYear + ")" + cdStr + Path.GetExtension(filename);
if (!File.Exists(newName)) {
try
{
File.Move(filename, newName);
originalfilename = Path.GetFileName( filename );
filename = newName;
} catch (Exception ex) {
Console.WriteLine("Error renaming movie: " + filename + "\n" + ex.Message);
}
}
}
}
}
public void newFolder(string rootFolder, string FolderFormat)
{
if (subRes != null)
{
if (subRes.MovieName != "")
{
string newName = rootFolder + "\\" + FolderFormat;
newName = newName.Replace("%M", Utils.FilenameFromTitle(subRes.MovieName));
newName = newName.Replace("%Y", subRes.MovieYear);
if (!Directory.Exists(newName))
{
try
{
Directory.CreateDirectory(newName);
}
catch (Exception ex)
{
Console.WriteLine("Could not create directory: " + filename + "\n" + ex.Message);
}
}
if (Path.GetDirectoryName(filename) != newName)
{
try
{
originalfolder = Path.GetDirectoryName(filename);
File.Move(filename, newName + "\\" + Path.GetFileName(filename));
filename = newName + "\\" + Path.GetFileName(filename);
}
catch (Exception ex)
{
Console.WriteLine("Could not move file to new directory: " + filename + "\n" + ex.Message);
}
}
}
}
}
public void saveNfo()
{
try
{
if (subRes != null)
{
List<string> lstNfo = new List<string>();
lstNfo.Add("Name: " + subRes.MovieName);
lstNfo.Add("Year: " + subRes.MovieYear);
lstNfo.Add("URL: " + "http://www.imdb.com/title/tt0" + subRes.IDMovieImdb);
lstNfo.Add("Rating: " + subRes.MovieImdbRating);
if (imdbinfo != null)
{
addCSV("Directors", imdbinfo.directors.Values, lstNfo);
addCSV("Cast", imdbinfo.cast.Values, lstNfo);
addCSV("Genres", imdbinfo.genres, lstNfo);
addString("Duration", imdbinfo.duration, lstNfo);
addString("Plot", imdbinfo.plot, lstNfo);
addCSV("Languages", imdbinfo.language, lstNfo);
addCSV("Countries", imdbinfo.country, lstNfo);
addCSV("Aka", imdbinfo.aka, lstNfo);
addCSV("Writers", imdbinfo.writers.Values, lstNfo);
addString("Tagline", imdbinfo.tagline, lstNfo);
addString("Goofs", imdbinfo.goofs, lstNfo);
addString("Trivia", imdbinfo.trivia, lstNfo);
addCSV("Certification", imdbinfo.certification, lstNfo);
}
if ((originalfilename != null) && (originalfilename != filename))
{
lstNfo.Add("OriginalFile: " + originalfilename);
}
if ((originalfolder != null) && (originalfolder != Path.GetDirectoryName(filename)))
{
lstNfo.Add("OriginalFolder: " + originalfolder);
}
lstNfo.Add("ImdbID: " + subRes.IDMovieImdb);
lstNfo.Add("Hash: " + subRes.MovieHash);
lstNfo.Add("Release: " + subRes.MovieReleaseName);
lstNfo.Add("CD: " + subRes.SubActualCD + "/" + subRes.SubSumCD);
lstNfo.Add("SubtitleUsed: " + subRes.ZipDownloadLink);
lstNfo.Add("SubtitleHash: " + subRes.SubHash);
string nfoFileName = Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + ".nfo";
if (File.Exists(nfoFileName)) { File.Delete(nfoFileName); }
using (StreamWriter writer = File.AppendText(nfoFileName))
{
foreach (string line in lstNfo)
{
writer.WriteLine(line);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error writing Nfo file: " + filename + "\n" + ex.Message);
}
}
private void addString(string Title, string Value, List<string> lstNfo )
{
if ((Value != null) && (Value != ""))
{
lstNfo.Add(Title + ": " + Value);
}
}
private string addCSV(string title, System.Collections.ICollection array, List<string> lstNfo)
{
string s = "";
if (array != null)
{
foreach (string a in array)
{
s = s + "," + a;
}
s = s.Substring(1);
lstNfo.Add(title + ": " + s);
}
return s;
}
private string addCSV(string title, string[] array, List<string> lstNfo)
{
string s = "";
if (array != null)
{
foreach (string a in array)
{
s = s + "," + a;
}
s = s.Substring(1);
lstNfo.Add(title + ": " + s);
}
return s;
}
}
public interface IOSDb : IXmlRpcProxy
{
[XmlRpcMethod("ServerInfo")]
XmlRpcStruct ServerInfo();
[XmlRpcMethod("LogIn")]
XmlRpcStruct LogIn(string username, string password, string language, string useragent);
[XmlRpcMethod("LogOut")]
XmlRpcStruct LogOut(string token);
[XmlRpcMethod("SearchSubtitles")]
subrt SearchSubtitles(string token, subInfo[] subs);
[XmlRpcMethod("DownloadSubtitles")]
subdata DownloadSubtitles(string token, string[] subs);
[XmlRpcMethod("GetIMDBMovieDetails")]
imdbheader GetIMDBMovieDetails(string token, string imdbid);
}
public class langMap
{
public string two { get; set; }
public string three { get; set; }
public string desc { get; set; }
public langMap(string ttwo, string tthree, string tdesc)
{
two = ttwo;
three = tthree;
desc = tdesc;
}
}
}