-
Notifications
You must be signed in to change notification settings - Fork 3
/
MediaNameParsingUnit.pas
611 lines (547 loc) · 20.5 KB
/
MediaNameParsingUnit.pas
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
{$I SCRAPER_DEFINES.INC}
unit MediaNameParsingUnit;
interface
function FindIMDBIDInNFOFiles(sPath,sMediaFileName : WideString) : Integer;
function ParseMediaName(MediaName : String; MediaPath : WideString; IsFolder : Boolean; CategoryType : Integer; var MediaYear,MediaMonth,MediaDay,MediaSeason,MediaEpisode : Integer; var MediaRes : String) : WideString;
function GetIMDBIDFromTextFile(FileName : WideString) : Integer;
function ExtractFileNameNoExt(FileName : String) : String;
implementation
uses sysutils, tntsysutils, dateutils, tntclasses, TheMovieDB_misc_utils_unit, {$IFDEF LOCALTRACE}msgdlgunit,{$ENDIF} global_consts;
const
// Resolution list
ResCount = 12;
ResList : Array[0..ResCount-1] of String =
('720p','1080p','480p','540p','2160p','240p','360p','480i','576i','576p','1080i','4320p');
// Safe crop words
CropCount = 67;
CropList : Array[0..CropCount-1] of String =
('unrated','rerip','repack','final repack','real proper','readnfo','stv','dircut','remastered', 'criterion', 'colorized version','extended cut',
'theatrical cut', 'theatrical edition', 'directors cut', 'director''s cut', 'directors edition', 'director''s edition', 'director''s cut', 'dircut',
'hdtv','web dl','dvdrip','bdrip','brrip','bluray','blu ray','hdrip','webrip',
'x264','x265','h264','h 264','h 265','xvid','divx','mpeg2','avc',
'pdtv','sdtv','ws pdtv','dvdscr','dvd','hddvd','dsr','ws dsr',
'dts','dd5','dd2','ac3','aac','aac2','truehd','mp3','flac',
'720p','1080p','2160p','4320p',
'hdcam','cam','hd ts','ts','tc','vcd','svcd','xxx');
// Unsafe crop words, must be followed by a safe crop word to be detected
FlagCount = 5;
FlagList : Array[0..FlagCount-1] of String =
('proper','internal','limited','docu','extended');
procedure Split(S : WideString; Ch : Char; sList : TTNTStrings);
var
I : Integer;
begin
While Pos(Ch,S) > 0 do
Begin
I := Pos(Ch,S);
sList.Add(Copy(S,1,I-1));
Delete(S,1,I);
End;
If Length(S) > 0 then sList.Add(S);
end;
procedure Combine(sList : TTNTStrings; Ch : Char; var S : WideString);
var
I : Integer;
begin
S := '';
For I := 0 to sList.Count-1 do
Begin
If I < sList.Count-1 then S := S+sList[I]+Ch else S := S+sList[I];
End;
end;
Function ExtractFileNameNoExt(FileName : String) : String;
var
I : Integer;
begin
If Length(FileName) > 0 then
Begin
Result := ExtractFileName(FileName);
For I := Length(Result) downto 1 do If Result[I] = '.' then
Begin
If I > 1 then Result := Copy(Result,1,I-1);
Break;
End;
End
Else Result := '';
end;
function GetMediaData(sList : TTNTStringList; CategoryType : Integer; var iYear, iMonth, iDay, iSeason, iEpisode : Integer) : Integer;
const
MultiLangSeasonCount = 51;
MultiLangSeasonList : Array[0..MultiLangSeasonCount-1] of String =
(
'season', // 00 English
'сезон', // 01 Russian+Bulgarian+Serbian+Ukrainian+Bellarusian+Macedonian
'выпуск', // 02 Russian
'الموسم', // 02 Arabic
'denboraldi', // 03 Basque
'ঋতু', // 04 Bengali
'temporada', // 05 Catalan
'季节', // 06 Chinese Simplified
'季節', // 07 Chinese Traditional
'stagione', // 08 Corsican
'sezóna', // 09 Czech+Slovak
'sæson', // 10 Danish
'seizoen', // 11 Dutch
'hooaeg', // 12 Estonian
'saison', // 13 French
'panahon', // 14 Filipino
'kausi', // 15 Finnish
'jahreszeit', // 16 German
'სეზონი', // 17 Georgian
'εποχή', // 18 Greek
'עונה', // 19 Hebrew
'ऋतु', // 20 Hindi
'évszak', // 21 Hungarian
'stagione', // 22 Italian
'árstíð', // 23 Icelandic
'musim', // 24 Indonesian
'séasúr', // 25 Irish
'シーズン', // 26 Japanese
'រដូវ', // 27 Khmer
'시즌', // 28 Korean
'demsal', // 29 Kurish
'ລະດູການ', // 30 Lao
'sezonas', // 31 Lithuanian
'मौसम', // 32 Nepali
'årstid', // 33 Norwegian
'موسم', // 34 Pashto+Sindhi
'فصل', // 35 Persian
'sezon', // 36 Polish+Romanian+Turkish+Haitian Creole
'temporada', // 37 Portuguese+Spanish
'ਸੀਜ਼ਨ', // 38 Punjabi
'කන්නයයි', // 39 Sinhala
'sezona', // 40 Slovenian+Croatian+Bosnian+Latvian
'msimu', // 41 Swahili
'säsong', // 42 Swedish
'சீசன்', // 43 Tamil
'ฤดู', // 44 Thai
'موسم', // 45 Urdo
'mavsum', // 46 Uzbek
'Mùa', // 47 Vietnamese
'tymor', // 48 Welsh
'inkathi' // 49 Zulu
);
var
I,I1 : Integer;
Found : Boolean;
// SkipDate : Boolean;
sLen : Integer;
sLen1 : Integer;
sLen2 : Integer;
lS : String;
lSW : WideString;
partFound: Boolean;
begin
Result := -1;
iYear := -1;
iMonth := -1;
iDay := -1;
iSeason := -1;
iEpisode := -1;
partFound:= False;
//we should start the iteration from 0 because the MediaName can contain only the season number ("Season 01")
For I := 0 to sList.Count-1 do
Begin
lS := Lowercase(sList[I]);
sLen := Length(lS);
// the production year/date should not be the first word in the title so we can safely skip the first item in the list
// "2001: A Space Odyssey"
If (I > 0) and ((sLen = 4) or (sLen = 6)) then
Begin
// Find a string containing only numbers, possibly encapsulated in "()" or "[]"
Found := True;
For I1 := 1 to sLen do
If Char(sList[I][I1]) in ['0'..'9','[',']','(',')'] = False then
Begin
Found := False;
Break;
End;
If Found = True then
Begin
If Char(sList[I][1]) in ['[','('] = True then
Begin
iYear := StrToIntDef(Copy(sList[I],2,4),-1);
End
else
Begin
iYear := StrToIntDef(sList[I],-1);
// Check for possible full date
If sList.Count > I+2 then
Begin
//Found := False;
sLen1 := Length(sList[I+1]);
sLen2 := Length(sList[I+2]);
If (sLen1 = 2) and (sLen2 = 2) then
Begin
If (Char(sList[I+1][1]) in ['0'..'9']) and (Char(sList[I+1][2]) in ['0'..'9']) and
(Char(sList[I+2][1]) in ['0'..'9']) and (Char(sList[I+2][2]) in ['0'..'9']) then
Begin
// Found Date
iMonth := StrToIntDef(sList[I+1],-1);
iDay := StrToIntDef(sList[I+2],-1);
End;
End;
End;
End;
// Check that the year is in valid range (from 1895 till next year from now)
If (iYear >= 1895) and (iYear <= YearOf(Now)+1) then
Begin
If Result = -1 then Result := I; // Return list index where we found the year for later cropping
//Break;
End;
End;
End;
If CategoryType = osmTV then
Begin
If ((iSeason = -1) or (iEpisode = -1) or partFound) then
Begin
If (sLen = 3) then // Snn or Enn
Begin
If ((lS[1] = 's') or (ls[1] = 'e')) and (lS[2] in ['0'..'9'] = True) and (lS[3] in ['0'..'9'] = True) then
Begin
if lS[1] = 's' then
iSeason := StrToIntDef(Copy(lS,2,2),-1);
if lS[1] = 'e' then
iEpisode := StrToIntDef(Copy(lS,2,2),-1);
If Result = -1 then Result := I;
//Break;
End;
End
else
If (sLen = 4) then // EPnn or nXnn
Begin
If (lS[1] = 'e') and (ls[2] = 'p') and (lS[3] in ['0'..'9'] = True) and (lS[4] in ['0'..'9'] = True) then
Begin
iEpisode := StrToIntDef(Copy(lS,3,2),-1);
If Result = -1 then Result := I;
//Break;
End;
If (lS[2] = 'x') and (lS[1] in ['0'..'9'] = True) and (lS[3] in ['0'..'9'] = True) and (lS[4] in ['0'..'9'] = True) then
Begin
iSeason := StrToIntDef(Copy(lS,1,1),-1);
iEpisode := StrToIntDef(Copy(lS,3,2),-1);
If Result = -1 then Result := I;
//Break;
End;
End
else
If (sLen = 5) then // nnXnn
Begin
If (lS[3] = 'x') and (lS[1] in ['0'..'9'] = True) and (lS[2] in ['0'..'9'] = True) and (lS[4] in ['0'..'9'] = True) and (lS[5] in ['0'..'9'] = True) then
Begin
iSeason := StrToIntDef(Copy(lS,1,2),-1);
iEpisode := StrToIntDef(Copy(lS,4,2),-1);
If Result = -1 then Result := I;
//Break;
End;
End
else
If (sLen = 6) then // SnnEnn
Begin
If (lS[1] = 's') and (lS[4] = 'e') and (lS[2] in ['0'..'9'] = True) and (lS[3] in ['0'..'9'] = True) and (lS[5] in ['0'..'9'] = True) and (lS[6] in ['0'..'9'] = True) then
Begin
iSeason := StrToIntDef(Copy(lS,2,2),-1);
iEpisode := StrToIntDef(Copy(lS,5,2),-1);
If Result = -1 then Result := I;
//Break;
End;
End
else
If (sLen = 8) then // [SnnEnn] or (SnnEnn)
Begin
If (lS[2] = 's') and (lS[5] = 'e') and (lS[3] in ['0'..'9'] = True) and (lS[4] in ['0'..'9'] = True) and (lS[6] in ['0'..'9'] = True) and (lS[7] in ['0'..'9'] = True) then
Begin
iSeason := StrToIntDef(Copy(lS,2,2),-1);
iEpisode := StrToIntDef(Copy(lS,5,2),-1);
If Result = -1 then Result := I;
//Break;
End;
End;
End;
//the following checks are in a separate If statement because their length may vary and can accidentally match one of the above
//we could add checks like this
// part part1 part01/season season1 season01
// If (sLen = 4) or (sLen = 5) or (sLen = 6) or (sLen = 7) or (sLen = 8) then
//but it might cause issues in the future if someone adds a new "special" word and fogets to check it's possible lenghts
If (Pos('part',lS) = 1) and (iEpisode = -1) then
Begin
If (sLen = 4) and (I < sList.Count-1) then
Begin
// Match "Part XX"
iEpisode := StrToIntDef(sList[I+1],-1);
End
else
If sLen > 4 then
Begin
// Match "PartXX"
iEpisode := StrToIntDef(Copy(sList[I],5,sLen-4),-1);
End;
If iEpisode > -1 then
Begin
partFound := True;
if iSeason = -1 then iSeason := 1;
If Result = -1 then Result := I;
//Break;
End;
End
else
Begin
lSW := TNT_WideLowercase(sList[I]);
For I1 := 0 to MultiLangSeasonCount-1 do
Begin
//ShowMessageW(lSW+'/'+UTF8Decode(MultiLangSeasonList[I1]));
If Pos(UTF8Decode(MultiLangSeasonList[I1]),lSW) = 1 then
Begin
sLen2 := Length(UTF8Decode(MultiLangSeasonList[I1]));
If (sLen = sLen2) and (I < sList.Count-1) then
Begin
// Match "Season XX"
iSeason := StrToIntDef(sList[I+1],-1);
End
else
If sLen > sLen2 then
Begin
// Match "SeasonXX"
iSeason := StrToIntDef(Copy(sList[I],sLen2+1,sLen-sLen2),-1);
End;
If iSeason > -1 then
Begin
If Result = -1 then Result := I;
//Break;
End;
End;
End;
End;
End;
End;
end;
{$WARNINGS OFF}
function ParseMediaName(MediaName : String; MediaPath : WideString; IsFolder : Boolean; CategoryType : Integer; var MediaYear,MediaMonth,MediaDay,MediaSeason,MediaEpisode : Integer; var MediaRes : String) : WideString;
var
I,I1 : Integer;
lS : WideString;
iPos : Integer;
iCropPos : Integer;
iResPos : Integer;
nList : TTNTStringList;
sList : TTNTStringList;
iFlag : Integer;
iFlagPos : Integer;
iDay : Integer;
iMonth : Integer;
iYear : Integer;
iMediaDataIdx : Integer;
parentFolderMediaNameYear : Integer;
parentFolderMediaNameMonth : Integer;
parentFolderMediaNameDay : Integer;
parentFolderMediaNameSeason : Integer;
parentFolderMediaNameEpisode : Integer;
parentFolderMediaNameRes : String;
begin
// Strip file extension if we're dealing with media files, with folders, pass StripFileExt=False
If IsFolder = True then
Begin
// Special folder name processing?
End
Else MediaName := ExtractFileNameNoExt(MediaName); // Remove file extension
MediaYear := -1;
MediaMonth := -1;
MediaDay := -1;
MediaSeason := -1;
MediaEpisode := -1;
MediaRes := '';
iFlag := -1;
// Replace ".", "_" and "-" characters with spaces and UTF8 decode to unicode
Result := ConvertCharsToSpaces(UTF8Decode(MediaName));
// Strip any data after the "^" special character (to allow naming tags to be ignored)
I := Pos('^',Result);
If I > 0 then Result := Copy(Result,1,I-1);
lS := TNT_WideLowercase(Result)+' ';
// Find Resolution
iResPos := MAXINT;
For I := 0 to ResCount-1 do
Begin
iPos := Pos(ResList[I],lS);
If iPos > 1 then
Begin
MediaRes := ResList[I];
iResPos := iPos;
Break;
End;
End;
// Mark first unsafe word
iFlagPos := MAXINT;
For I := 0 to FlagCount-1 do
Begin
iPos := Pos(' '+FlagList[I]+' ',lS);
If iPos = 0 then iPos := Pos('['+FlagList[I]+']',lS); // allow unsafe word encapsulated in '[]'
If iPos = 0 then iPos := Pos('('+FlagList[I]+')',lS); // allow unsafe word encapsulated in '()'
If (iPos > 1) and (iPos < iFlagPos) then
Begin
iFlag := I;
iFlagPos := iPos;
End;
End;
// Find a safe crop word
iCropPos := iResPos;
For I := 0 to CropCount-1 do
Begin
iPos := Pos(' '+CropList[I]+' ',lS);
If iPos = 0 then iPos := Pos('['+CropList[I]+']',lS); // allow safe word encapsulated in '[]'
If iPos = 0 then iPos := Pos('('+CropList[I]+')',lS); // allow safe word encapsulated in '()'
If (iPos > 1) and (iPos < iCropPos) then
Begin
iCropPos := iPos;
End;
End;
If iCropPos < MAXINT then
Begin
If iFlagPos < MAXINT then
Begin
// If an unsafe crop word was detected, ensure it is next to a safe word before using its position to crop.
If iFlagPos+Length(FlagList[iFlag])+1 = iCropPos then
Result := Copy(Result,1,iFlagPos-1) else
Result := Copy(Result,1,iCropPos-1);
End
Else Result := Copy(Result,1,iCropPos-1);
End;
// Parse media for date year from 1895 to Next year from today.
// The year can be encompassed in "()" or "[]".
// The year detection is done after the safe word cropping to avoid errors for
// Media Titles as "Bolt.1080p.BluRay.x264-1920" where 1920 is definitely not the prduction year
nList := TTNTStringList.Create;
Split(Result,' ',nList); // Split words into an array
iMediaDataIdx := GetMediaData(nList,CategoryType,iYear,iMonth,iDay,MediaSeason,MediaEpisode);
{
// This code doesn't work
If iYear = -1 then
Begin
// Try finding a year using brute force
sList := TTNTStringList.Create;
Split(ConvertCharsToSpaces(UTF8Decode(MediaName)),' ',sList); // Split words into an array
For I := 0 to sList.Count do
Begin
iYear := StrToIntDef(sList[I],-1);
If (iYear > 1885) and (iYear < 2200) then Break else iYear := -1;
End;
sList.Free
End;}
// If (iYear > -1) or (MediaSeason > -1) then
If (iMediaDataIdx > -1) then
Begin
MediaYear := iYear;
If iMonth > -1 then
Begin
MediaDay := iDay;
MediaMonth := iMonth;
End;
//crop everything after the Media Data including itself
If iMediaDataIdx = 0 then
Begin
Result := '';
// we should try to extract the Title from the parent folder in the following cases
If ((IsFolder and (MediaSeason > -1)) //we are trying to process a folder whose name only contains the season/part number
or (not IsFolder and (MediaEpisode > -1))) //we are trying to process a file whose name only contains episode and probably season (but not required) number (example: \Breaking Bad\Season 02\E01 - Seven Thirty-Seven.mkv)
and (MediaPath <> '')
then
Begin
// Strip backslash from folder names as needed
If (MediaPath[Length(MediaPath)] = '\') then MediaPath := Copy(MediaPath,1,Length(MediaPath)-1);
//ShowMessageW(MediaPath);
Result := ParseMediaName(UTF8Encode(WideExtractFileName(MediaPath)),WideExtractFilePath(MediaPath),True,CategoryType,parentFolderMediaNameYear,parentFolderMediaNameMonth,parentFolderMediaNameDay,parentFolderMediaNameSeason,parentFolderMediaNameEpisode,parentFolderMediaNameRes);
If MediaYear = -1 then MediaYear := parentFolderMediaNameYear;
If MediaMonth = -1 then MediaMonth := parentFolderMediaNameMonth;
If MediaDay = -1 then MediaDay := parentFolderMediaNameDay;
If MediaSeason = -1 then MediaSeason := parentFolderMediaNameSeason;
If MediaEpisode = -1 then MediaEpisode := parentFolderMediaNameEpisode;
If MediaRes = '' then MediaRes := parentFolderMediaNameRes;
End;
End
else
Begin
If iMediaDataIdx <= nList.Count-1 then
For I1 := nList.Count-1 downto iMediaDataIdx do nList.Delete(I1);
Combine(nList,' ',Result); // Combine the updated array back into a string
End;
End;
// Moved earlier in the function
// Strip any data after the "^" special character (to allow naming tags to be ignored)
//I := Pos('^',Result);
//If I > 0 then Result := Copy(Result,1,I-1);
Result := Trim(Result);
nList.Free;
end;
//{$WARNINGS ON}
function FindIMDBIDInNFOFiles(sPath,sMediaFileName : WideString) : Integer;
var
I : Integer;
fList : TTNTStringList;
iCurrIMDBID : Integer;
bMultipleResults : Boolean;
begin
Result := -1;
bMultipleResults := False;
fList := TTNTStringList.Create;
sPath := AddBackSlash(sPath);
sMediaFileName := ExtractFileNameNoExt(sMediaFileName);
FileExtIntoStringList(sPath,'.nfo',fList,False);
If fList.Count > 0 then For I := 0 to fList.Count-1 do
Begin
iCurrIMDBID := GetIMDBIDFromTextFile(fList[I]);
if iCurrIMDBID > -1 then
Begin
If WideCompareText(ExtractFileNameNoExt(fList[I]),sMediaFileName) = 0 then
Begin
Result := iCurrIMDBID;
bMultipleResults := False;
Break;
End;
If Result > -1 then
Begin
If Result <> iCurrIMDBID then
Begin
bMultipleResults := True;
If sMediaFileName = '' then Break;
End;
End
Else Result := iCurrIMDBID;
End
End;
If bMultipleResults then Result := -1;
fList.Free;
end;
function GetIMDBIDFromTextFile(FileName : WideString) : Integer;
var
I,I1 : Integer;
iPos : Integer;
sList : TTNTStringList;
sResult : String;
begin
Result := -1;
sList := TTNTStringList.Create;
Try
sList.LoadFromFile(FileName);
Except
FreeAndNil(sList);
End;
If sList <> nil then
Begin
For I := 0 to sList.Count-1 do
Begin
iPos := Pos('imdb.com/title/',TNT_WideLowercase(sList[I]));
If iPos > 0 then
Begin
sResult := Copy(sList[I],iPos+17,Length(sList[I])-(iPos+16));
For I1 := 1 to Length(sResult) do If sResult[I1] in ['0'..'9'] = False then
Begin
sResult := Copy(sResult,1,I1-1);
Break;
End;
Result := StrToIntDef(sResult,-1);
If Result > -1 then Break;
End;
End;
sList.Free;
End;
end;
end.