6
6
#include < QEvent>
7
7
#include < QDir>
8
8
#include < QUrl>
9
+ #include < QRegularExpression>
9
10
10
11
// myth
11
12
#include " libmythbase/mythcorecontext.h"
@@ -199,7 +200,7 @@ void MetadataDownload::run()
199
200
MetadataLookup *newlookup = list.takeFirst ();
200
201
201
202
// pass through automatic type
202
- newlookup->SetAutomatic (true ); // ### XXX RER
203
+ newlookup->SetAutomatic (true );
203
204
newlookup->SetStep (kLookupData );
204
205
// Type may have changed
205
206
LookupType ret = GuessLookupType (newlookup);
@@ -252,11 +253,20 @@ unsigned int MetadataDownload::findExactMatchCount(MetadataLookupList list,
252
253
{
253
254
unsigned int exactMatches = 0 ;
254
255
unsigned int exactMatchesWithArt = 0 ;
256
+ static const QRegularExpression year { R"( \(\d{4}\)$)" };
255
257
256
258
for (const auto & lkup : std::as_const (list))
257
259
{
258
- // Consider exact title matches (ignoring case)
259
- if ((QString::compare (lkup->GetTitle (), originaltitle, Qt::CaseInsensitive) == 0 ))
260
+ // Consider exact title matches with or without trailing '(year)' (ignoring case)
261
+ QString titlewoyear = originaltitle;
262
+ auto match = year.match (titlewoyear);
263
+ if (match.hasMatch ())
264
+ {
265
+ titlewoyear.remove (match.capturedStart (), match.capturedLength ());
266
+ }
267
+
268
+ if ((QString::compare (lkup->GetTitle (), originaltitle, Qt::CaseInsensitive) == 0 ) ||
269
+ (QString::compare (lkup->GetTitle (), titlewoyear, Qt::CaseInsensitive) == 0 ))
260
270
{
261
271
// In lookup by name, the television database tends to only include Banner artwork.
262
272
// In lookup by name, the movie database tends to include only Fan and Cover artwork.
@@ -285,33 +295,63 @@ MetadataLookup* MetadataDownload::findBestMatch(MetadataLookupList list,
285
295
int exactMatches = 0 ;
286
296
int exactMatchesWithArt = 0 ;
287
297
bool foundMatchWithArt = false ;
298
+ bool foundMatchWithYear = false ;
299
+ uint year = 0 ;
300
+
301
+ QString titlewoyear = originaltitle;
302
+
303
+ static const QRegularExpression regexyear { R"( \(\d{4}\)$)" };
304
+
305
+ auto match = regexyear.match (titlewoyear);
306
+ if (match.hasMatch ())
307
+ {
308
+ titlewoyear.remove (match.capturedStart (), match.capturedLength ());
309
+ year = match.captured (0 ).replace (" (" ," " ).replace (" )" ," " ).toUInt ();
310
+ LOG (VB_GENERAL, LOG_DEBUG, QString (" Looking for: '%1' with release year: '%2'" )
311
+ .arg (titlewoyear, QString::number (year)));
312
+ }
288
313
289
314
// Build a list of all the titles
290
315
for (const auto & lkup : std::as_const (list))
291
316
{
292
317
QString title = lkup->GetTitle ();
293
- LOG (VB_GENERAL, LOG_INFO, QString (" Comparing metadata title '%1' [%2] to recording title '%3'" )
294
- .arg (title, lkup->GetReleaseDate ().toString (), originaltitle));
295
- // Consider exact title matches (ignoring case), which have some artwork available.
296
- if (QString::compare (title, originaltitle, Qt::CaseInsensitive) == 0 )
318
+ LOG (VB_GENERAL, LOG_INFO,
319
+ QString (" Comparing metadata title '%1' [%2] to recording title '%3' [%4]" )
320
+ .arg (title, lkup->GetReleaseDate ().toString (), titlewoyear,
321
+ (year == 0 ) ? " N/A" : QString::number (year)));
322
+
323
+ // Consider exact title matches with or without trailing '(year)' (ignoring case),
324
+ // which have some artwork available.
325
+ if ((QString::compare (title, originaltitle, Qt::CaseInsensitive) == 0 ) ||
326
+ (QString::compare (title, titlewoyear, Qt::CaseInsensitive) == 0 ))
297
327
{
298
328
bool hasArtwork = ((!(lkup->GetArtwork (kArtworkFanart )).empty ()) ||
299
329
(!(lkup->GetArtwork (kArtworkCoverart )).empty ()) ||
300
330
(!(lkup->GetArtwork (kArtworkBanner )).empty ()));
301
331
302
- LOG (VB_GENERAL, LOG_INFO, QString (" '%1', popularity = %2, ReleaseDate = %3" )
332
+ if ((lkup->GetYear () != 0 ) && (year == lkup->GetYear ()))
333
+ {
334
+ exactTitleDate = lkup->GetReleaseDate ();
335
+ exactTitlePopularity = lkup->GetPopularity ();
336
+ foundMatchWithYear = true ;
337
+ ret = lkup;
338
+ }
339
+
340
+ LOG (VB_GENERAL, LOG_INFO, QString (" '%1', popularity = %2, ReleaseDate = %3, Year = %4" )
303
341
.arg (title)
304
342
.arg (lkup->GetPopularity ())
305
- .arg (lkup->GetReleaseDate ().toString ()));
343
+ .arg (lkup->GetReleaseDate ().toString ())
344
+ .arg (lkup->GetYear ()));
306
345
307
346
// After the first exact match, prefer any more popular one.
308
347
// Most of the Movie database entries have Popularity fields.
309
348
// The TV series database generally has no Popularity values specified,
310
349
// so if none are found so far in the search, pick the most recently
311
350
// released entry with artwork. Also, if the first exact match had
312
351
// no artwork, prefer any later exact match with artwork.
352
+ // Stop searching if we have already found a match with correct year.
313
353
if ((ret == nullptr ) ||
314
- (hasArtwork &&
354
+ (hasArtwork && !foundMatchWithYear &&
315
355
((!foundMatchWithArt) ||
316
356
((lkup->GetPopularity () > exactTitlePopularity)) ||
317
357
((exactTitlePopularity == 0 .0F ) && (lkup->GetReleaseDate () > exactTitleDate)))))
@@ -320,6 +360,7 @@ MetadataLookup* MetadataDownload::findBestMatch(MetadataLookupList list,
320
360
exactTitlePopularity = lkup->GetPopularity ();
321
361
ret = lkup;
322
362
}
363
+
323
364
exactMatches++;
324
365
if (hasArtwork)
325
366
{
@@ -348,8 +389,10 @@ MetadataLookup* MetadataDownload::findBestMatch(MetadataLookupList list,
348
389
{
349
390
LOG (VB_GENERAL, LOG_INFO,
350
391
QString (" Multiple exact title matches found for '%1'. "
351
- " Selecting most popular or most recent [%2]" )
352
- .arg (originaltitle, exactTitleDate.toString ()));
392
+ " Selecting by exact year [%2] or most popular or most recent [%3]" )
393
+ .arg (originaltitle,
394
+ (year == 0 ) ? " N/A" : QString::number (year),
395
+ exactTitleDate.toString ()));
353
396
}
354
397
return ret;
355
398
}
0 commit comments