/
metadatadownload.cpp
734 lines (633 loc) · 20.4 KB
/
metadatadownload.cpp
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
// qt
#include <QCoreApplication>
#include <QEvent>
#include <QDir>
#include <QUrl>
// myth
#include "mythcorecontext.h"
#include "mythdirs.h"
#include "mythuihelper.h"
#include "mythsystem.h"
#include "storagegroup.h"
#include "metadatadownload.h"
#include "mythmiscutil.h"
#include "remotefile.h"
#include "mythlogging.h"
QEvent::Type MetadataLookupEvent::kEventType =
(QEvent::Type) QEvent::registerEventType();
QEvent::Type MetadataLookupFailure::kEventType =
(QEvent::Type) QEvent::registerEventType();
MetadataDownload::MetadataDownload(QObject *parent) :
MThread("MetadataDownload")
{
m_parent = parent;
}
MetadataDownload::~MetadataDownload()
{
cancel();
wait();
}
void MetadataDownload::addLookup(MetadataLookup *lookup)
{
// Add a lookup to the queue
m_mutex.lock();
m_lookupList.append(lookup);
if (!isRunning())
start();
m_mutex.unlock();
}
void MetadataDownload::prependLookup(MetadataLookup *lookup)
{
// Add a lookup to the queue
m_mutex.lock();
m_lookupList.prepend(lookup);
if (!isRunning())
start();
m_mutex.unlock();
}
void MetadataDownload::cancel()
{
m_mutex.lock();
qDeleteAll(m_lookupList);
m_lookupList.clear();
m_parent = NULL;
m_mutex.unlock();
}
void MetadataDownload::run()
{
RunProlog();
MetadataLookup* lookup;
while ((lookup = moreWork()) != NULL)
{
MetadataLookupList list;
// Go go gadget Metadata Lookup
if (lookup->GetType() == kMetadataVideo)
{
if (lookup->GetSubtype() == kProbableTelevision)
list = handleTelevision(lookup);
else if (lookup->GetSubtype() == kProbableMovie)
list = handleMovie(lookup);
else
list = handleVideoUndetermined(lookup);
if (!list.size() &&
lookup->GetSubtype() == kUnknownVideo)
{
list = handleMovie(lookup);
}
}
else if (lookup->GetType() == kMetadataRecording)
{
if (lookup->GetSubtype() == kProbableTelevision)
{
if (lookup->GetSeason() > 0 || lookup->GetEpisode() > 0)
list = handleTelevision(lookup);
else if (!lookup->GetSubtitle().isEmpty())
list = handleVideoUndetermined(lookup);
if (!list.size())
list = handleRecordingGeneric(lookup);
}
else if (lookup->GetSubtype() == kProbableMovie)
{
list = handleMovie(lookup);
if (lookup->GetInetref().isEmpty())
list.append(handleRecordingGeneric(lookup));
}
else
{
list = handleRecordingGeneric(lookup);
if (lookup->GetInetref().isEmpty())
list.append(handleMovie(lookup));
}
}
else if (lookup->GetType() == kMetadataGame)
list = handleGame(lookup);
// inform parent we have lookup ready for it
if (m_parent && list.count() >= 1)
{
// If there's only one result, don't bother asking
// our parent about it, just add it to the back of
// the queue in kLookupData mode.
if (list.count() == 1 && list.at(0)->GetStep() == kLookupSearch)
{
MetadataLookup *newlookup = list.takeFirst();
newlookup->SetStep(kLookupData);
prependLookup(newlookup);
continue;
}
// If we're in automatic mode, we need to make
// these decisions on our own. Pass to title match.
if (list.at(0)->GetAutomatic() && list.count() > 1)
{
if (!findBestMatch(list, lookup->GetTitle()))
QCoreApplication::postEvent(m_parent,
new MetadataLookupFailure(MetadataLookupList() << lookup));
continue;
}
LOG(VB_GENERAL, LOG_INFO,
QString("Returning Metadata Results: %1 %2 %3")
.arg(lookup->GetTitle()).arg(lookup->GetSeason())
.arg(lookup->GetEpisode()));
QCoreApplication::postEvent(m_parent,
new MetadataLookupEvent(list));
}
else
{
list.append(lookup);
QCoreApplication::postEvent(m_parent,
new MetadataLookupFailure(list));
}
}
RunEpilog();
}
MetadataLookup* MetadataDownload::moreWork()
{
MetadataLookup* result = NULL;
m_mutex.lock();
if (!m_lookupList.isEmpty())
result = m_lookupList.takeFirst();
m_mutex.unlock();
return result;
}
bool MetadataDownload::findBestMatch(MetadataLookupList list,
QString originaltitle)
{
QStringList titles;
// Build a list of all the titles
for (MetadataLookupList::const_iterator i = list.begin();
i != list.end(); ++i)
{
titles.append((*i)->GetTitle());
}
// Apply Levenshtein distance algorithm to determine closest match
QString bestTitle = nearestName(originaltitle, titles);
// If no "best" was chosen, give up.
if (bestTitle.isEmpty())
{
LOG(VB_GENERAL, LOG_ERR,
QString("No adequate match or multiple "
"matches found for %1. Update manually.")
.arg(originaltitle));
return false;
}
LOG(VB_GENERAL, LOG_INFO, QString("Best Title Match For %1: %2")
.arg(originaltitle).arg(bestTitle));
// Grab the one item that matches the besttitle (IMPERFECT)
for (MetadataLookupList::const_iterator i = list.begin();
i != list.end(); ++i)
{
if ((*i)->GetTitle() == bestTitle)
{
MetadataLookup *newlookup = (*i);
newlookup->SetStep(kLookupData);
prependLookup(newlookup);
return true;
}
}
return false;
}
MetadataLookupList MetadataDownload::runGrabber(QString cmd, QStringList args,
MetadataLookup* lookup,
bool passseas)
{
MythSystem grabber(cmd, args, kMSNoRunShell | kMSStdOut | kMSBuffered);
MetadataLookupList list;
LOG(VB_GENERAL, LOG_INFO, QString("Running Grabber: %1 %2")
.arg(cmd).arg(args.join(" ")));
grabber.Run();
grabber.Wait();
QByteArray result = grabber.ReadAll();
if (!result.isEmpty())
{
QDomDocument doc;
doc.setContent(result, true);
QDomElement root = doc.documentElement();
QDomElement item = root.firstChildElement("item");
while (!item.isNull())
{
MetadataLookup *tmp = ParseMetadataItem(item, lookup,
passseas);
list.append(tmp);
item = item.nextSiblingElement("item");
}
}
return list;
}
QString MetadataDownload::GetMovieGrabber()
{
QString def_cmd = "metadata/Movie/tmdb.py";
QString db_cmd = gCoreContext->GetSetting("MovieGrabber", def_cmd);
return QDir::cleanPath(QString("%1/%2")
.arg(GetShareDir())
.arg(db_cmd));
}
QString MetadataDownload::GetTelevisionGrabber()
{
QString def_cmd = "metadata/Television/ttvdb.py";
QString db_cmd = gCoreContext->GetSetting("TelevisionGrabber", def_cmd);
return QDir::cleanPath(QString("%1/%2")
.arg(GetShareDir())
.arg(db_cmd));
}
QString MetadataDownload::GetGameGrabber()
{
QString def_cmd = "metadata/Game/giantbomb.py";
QString db_cmd = gCoreContext->GetSetting("mythgame.MetadataGrabber", def_cmd);
return QDir::cleanPath(QString("%1/%2")
.arg(GetShareDir())
.arg(db_cmd));
}
bool MetadataDownload::runGrabberTest(const QString &grabberpath)
{
QStringList args;
args.append("-t");
MythSystem grabber(grabberpath, args, kMSNoRunShell | kMSStdOut | kMSBuffered);
grabber.Run();
uint exitcode = grabber.Wait();
if (exitcode != 0)
return false;
return true;
}
bool MetadataDownload::MovieGrabberWorks()
{
if (!runGrabberTest(GetMovieGrabber()))
{
LOG(VB_GENERAL, LOG_INFO,
QString("Movie grabber not functional. Aborting this run."));
return false;
}
return true;
}
bool MetadataDownload::TelevisionGrabberWorks()
{
if (!runGrabberTest(GetTelevisionGrabber()))
{
LOG(VB_GENERAL, LOG_INFO,
QString("Television grabber not functional. Aborting this run."));
return false;
}
return true;
}
MetadataLookupList MetadataDownload::readMXML(QString MXMLpath,
MetadataLookup* lookup,
bool passseas)
{
MetadataLookupList list;
LOG(VB_GENERAL, LOG_INFO,
QString("Matching MXML file found. Parsing %1 for metadata...")
.arg(MXMLpath));
if (lookup->GetType() == kMetadataVideo)
{
QByteArray mxmlraw;
QDomElement item;
if (MXMLpath.startsWith("myth://"))
{
RemoteFile *rf = new RemoteFile(MXMLpath);
if (rf && rf->Open())
{
bool loaded = rf->SaveAs(mxmlraw);
if (loaded)
{
QDomDocument doc;
if (doc.setContent(mxmlraw, true))
{
lookup->SetStep(kLookupData);
QDomElement root = doc.documentElement();
item = root.firstChildElement("item");
}
else
LOG(VB_GENERAL, LOG_ERR,
QString("Corrupt or invalid MXML file."));
}
rf->Close();
}
delete rf;
rf = NULL;
}
else
{
QFile file(MXMLpath);
if (file.open(QIODevice::ReadOnly))
{
mxmlraw = file.readAll();
QDomDocument doc;
if (doc.setContent(mxmlraw, true))
{
lookup->SetStep(kLookupData);
QDomElement root = doc.documentElement();
item = root.firstChildElement("item");
}
else
LOG(VB_GENERAL, LOG_ERR,
QString("Corrupt or invalid MXML file."));
file.close();
}
}
MetadataLookup *tmp = ParseMetadataItem(item, lookup, passseas);
list.append(tmp);
}
return list;
}
MetadataLookupList MetadataDownload::readNFO(QString NFOpath,
MetadataLookup* lookup)
{
MetadataLookupList list;
LOG(VB_GENERAL, LOG_INFO,
QString("Matching NFO file found. Parsing %1 for metadata...")
.arg(NFOpath));
if (lookup->GetType() == kMetadataVideo)
{
QByteArray nforaw;
QDomElement item;
if (NFOpath.startsWith("myth://"))
{
RemoteFile *rf = new RemoteFile(NFOpath);
if (rf && rf->Open())
{
bool loaded = rf->SaveAs(nforaw);
if (loaded)
{
QDomDocument doc;
if (doc.setContent(nforaw, true))
{
lookup->SetStep(kLookupData);
item = doc.documentElement();
}
else
LOG(VB_GENERAL, LOG_ERR,
QString("PIRATE ERROR: Invalid NFO file found."));
}
rf->Close();
}
delete rf;
rf = NULL;
}
else
{
QFile file(NFOpath);
if (file.open(QIODevice::ReadOnly))
{
nforaw = file.readAll();
QDomDocument doc;
if (doc.setContent(nforaw, true))
{
lookup->SetStep(kLookupData);
item = doc.documentElement();
}
else
LOG(VB_GENERAL, LOG_ERR,
QString("PIRATE ERROR: Invalid NFO file found."));
file.close();
}
}
MetadataLookup *tmp = ParseMetadataMovieNFO(item, lookup);
list.append(tmp);
}
return list;
}
MetadataLookupList MetadataDownload::handleGame(MetadataLookup* lookup)
{
MetadataLookupList list;
QString cmd = GetGameGrabber();
QStringList args;
args.append(QString("-l")); // Language Flag
args.append(gCoreContext->GetLanguage()); // UI Language
// If the inetref is populated, even in kLookupSearch mode,
// become a kLookupData grab and use that.
if (lookup->GetStep() == kLookupSearch &&
(!lookup->GetInetref().isEmpty() &&
lookup->GetInetref() != "00000000"))
lookup->SetStep(kLookupData);
if (lookup->GetStep() == kLookupSearch)
{
args.append(QString("-M"));
QString title = lookup->GetTitle();
args.append(title);
}
else if (lookup->GetStep() == kLookupData)
{
args.append(QString("-D"));
args.append(lookup->GetInetref());
}
list = runGrabber(cmd, args, lookup);
return list;
}
MetadataLookupList MetadataDownload::handleMovie(MetadataLookup* lookup)
{
MetadataLookupList list;
QString mxml;
QString nfo;
if (!lookup->GetFilename().isEmpty())
{
mxml = getMXMLPath(lookup->GetFilename());
nfo = getNFOPath(lookup->GetFilename());
}
if (mxml.isEmpty() && nfo.isEmpty())
{
QString cmd = GetMovieGrabber();
QStringList args;
args.append(QString("-l")); // Language Flag
args.append(gCoreContext->GetLanguage()); // UI Language
// If the inetref is populated, even in kLookupSearch mode,
// become a kLookupData grab and use that.
if (lookup->GetStep() == kLookupSearch &&
(!lookup->GetInetref().isEmpty() &&
lookup->GetInetref() != "00000000"))
lookup->SetStep(kLookupData);
if (lookup->GetStep() == kLookupSearch)
{
args.append(QString("-M"));
QString title = lookup->GetTitle();
args.append(title);
}
else if (lookup->GetStep() == kLookupData)
{
args.append(QString("-D"));
args.append(lookup->GetInetref());
}
list = runGrabber(cmd, args, lookup);
}
else if (!mxml.isEmpty())
list = readMXML(mxml, lookup);
else if (!nfo.isEmpty())
list = readNFO(nfo, lookup);
return list;
}
MetadataLookupList MetadataDownload::handleTelevision(MetadataLookup* lookup)
{
MetadataLookupList list;
QString cmd = GetTelevisionGrabber();
QStringList args;
args.append(QString("-l")); // Language Flag
args.append(gCoreContext->GetLanguage()); // UI Language
// If the inetref is populated, even in kLookupSearch mode,
// become a kLookupData grab and use that.
if (lookup->GetStep() == kLookupSearch &&
(!lookup->GetInetref().isEmpty() &&
lookup->GetInetref() != "00000000"))
lookup->SetStep(kLookupData);
if (lookup->GetStep() == kLookupSearch)
{
args.append(QString("-M"));
if (lookup->GetInetref().isEmpty() ||
lookup->GetInetref() == "00000000")
{
QString title = lookup->GetTitle();
args.append(title);
}
else
{
QString inetref = lookup->GetInetref();
args.append(inetref);
}
}
else if (lookup->GetStep() == kLookupData)
{
args.append(QString("-D"));
args.append(lookup->GetInetref());
args.append(QString::number(lookup->GetSeason()));
args.append(QString::number(lookup->GetEpisode()));
}
else if (lookup->GetStep() == kLookupCollection)
{
args.append(QString("-C"));
args.append(lookup->GetCollectionref());
}
list = runGrabber(cmd, args, lookup);
// Collection Fallback
// If the lookup allows generic metadata, and the specific
// season and episode are not available, try for series metadata.
if (list.isEmpty() &&
lookup->GetAllowGeneric() &&
lookup->GetStep() == kLookupData)
{
lookup->SetStep(kLookupCollection);
list = handleTelevision(lookup);
}
return list;
}
MetadataLookupList MetadataDownload::handleVideoUndetermined(
MetadataLookup* lookup)
{
MetadataLookupList list;
QString cmd = GetTelevisionGrabber();
// Can't trust the inetref with so little information.
QStringList args;
args.append(QString("-l")); // Language Flag
args.append(gCoreContext->GetLanguage()); // UI Language
args.append(QString("-N"));
if (!lookup->GetInetref().isEmpty())
{
QString inetref = lookup->GetInetref();
args.append(inetref);
}
else
{
QString title = lookup->GetTitle();
args.append(title);
}
QString subtitle = lookup->GetSubtitle();
args.append(subtitle);
// Try to do a title/subtitle lookup
list = runGrabber(cmd, args, lookup, false);
if (list.count() == 1)
list.at(0)->SetStep(kLookupData);
return list;
}
MetadataLookupList MetadataDownload::handleRecordingGeneric(
MetadataLookup* lookup)
{
// We only enter this mode if we are pretty darn sure this is a TV show,
// but we're for some reason looking up a generic, or the title didn't
// exactly match in one of the earlier lookups. This is a total
// hail mary to try to get at least *series* level info and art/inetref.
MetadataLookupList list;
QString cmd = GetTelevisionGrabber();
QStringList args;
args.append(QString("-l")); // Language Flag
args.append(gCoreContext->GetLanguage()); // UI Language
args.append("-M");
QString title = lookup->GetTitle();
args.append(title);
LookupType origtype = lookup->GetSubtype();
int origseason = lookup->GetSeason();
int origepisode = lookup->GetEpisode();
lookup->SetSubtype(kProbableGenericTelevision);
if (origseason == 0 && origepisode == 0)
{
lookup->SetSeason(1);
lookup->SetEpisode(1);
}
list = runGrabber(cmd, args, lookup, true);
if (list.count() == 1)
{
lookup->SetInetref(list.at(0)->GetInetref());
lookup->SetCollectionref(list.at(0)->GetCollectionref());
list = handleTelevision(lookup);
}
lookup->SetSeason(origseason);
lookup->SetEpisode(origepisode);
lookup->SetSubtype(origtype);
return list;
}
QString MetadataDownload::getMXMLPath(QString filename)
{
QString ret;
QString xmlname;
QUrl qurl(filename);
QString ext = QFileInfo(qurl.path()).suffix();
xmlname = filename.left(filename.size() - ext.size()) + "mxml";
QUrl xurl(xmlname);
if (xmlname.startsWith("myth://"))
{
if (qurl.host().toLower() != gCoreContext->GetHostName().toLower() &&
(!gCoreContext->IsThisHost(qurl.host())))
{
if (RemoteFile::Exists(xmlname))
ret = xmlname;
}
else
{
StorageGroup sg;
QString fn = sg.FindFile(xurl.path());
if (!fn.isEmpty() && QFile::exists(fn))
ret = xmlname;
}
}
else
{
if (QFile::exists(xmlname))
ret = xmlname;
}
return ret;
}
QString MetadataDownload::getNFOPath(QString filename)
{
QString ret;
QString nfoname;
QUrl qurl(filename);
QString ext = QFileInfo(qurl.path()).suffix();
nfoname = filename.left(filename.size() - ext.size()) + "nfo";
QUrl nurl(nfoname);
if (nfoname.startsWith("myth://"))
{
if (qurl.host().toLower() != gCoreContext->GetHostName().toLower() &&
(!gCoreContext->IsThisHost(qurl.host())))
{
if (RemoteFile::Exists(nfoname))
ret = nfoname;
}
else
{
StorageGroup sg;
QString fn = sg.FindFile(nurl.path());
if (!fn.isEmpty() && QFile::exists(fn))
ret = nfoname;
}
}
else
{
if (QFile::exists(nfoname))
ret = nfoname;
}
return ret;
}