예제 #1
0
        protected bool TryMatch(string seriesName, bool cacheOnly, out TvdbSeries seriesDetail)
        {
            seriesDetail = null;
            try
            {
                // Prefer memory cache
                if (_memoryCache.TryGetValue(seriesName, out seriesDetail))
                {
                    return(true);
                }

                // Load cache or create new list
                List <SeriesMatch> matches;
                lock (_syncObj)
                    matches = Settings.Load <List <SeriesMatch> >(MatchesSettingsFile) ?? new List <SeriesMatch>();

                // Init empty
                seriesDetail = null;

                // Use cached values before doing online query
                SeriesMatch match = matches.Find(m => m.ItemName == seriesName || m.TvDBName == seriesName);
                ServiceRegistration.Get <ILogger>().Debug("SeriesTvDbMatcher: Try to lookup series \"{0}\" from cache: {1}", seriesName, match != null && match.Id != 0);

                // Try online lookup
                if (!Init())
                {
                    return(false);
                }

                // If this is a known series, only return the series details (including episodes).
                if (match != null)
                {
                    return(match.Id != 0 && _tv.GetSeries(match.Id, true, out seriesDetail));
                }

                if (cacheOnly)
                {
                    return(false);
                }

                List <TvdbSearchResult> series;
                if (_tv.SearchSeriesUnique(seriesName, out series))
                {
                    TvdbSearchResult matchedSeries = series[0];
                    ServiceRegistration.Get <ILogger>().Debug("SeriesTvDbMatcher: Found unique online match for \"{0}\": \"{1}\" [Lang: {2}]", seriesName, matchedSeries.SeriesName, matchedSeries.Language);

                    if (_tv.GetSeries(matchedSeries.Id, true, out seriesDetail))
                    {
                        ServiceRegistration.Get <ILogger>().Debug("SeriesTvDbMatcher: Loaded details for \"{0}\"", matchedSeries.SeriesName);
                        // Add this match to cache
                        SeriesMatch onlineMatch = new SeriesMatch
                        {
                            ItemName = seriesName,
                            Id       = seriesDetail.Id,
                            TvDBName = seriesDetail.SeriesName
                        };

                        // Save cache
                        _storage.SaveNewMatch(seriesName, onlineMatch);
                        return(true);
                    }
                }
                ServiceRegistration.Get <ILogger>().Debug("SeriesTvDbMatcher: No unique match found for \"{0}\"", seriesName);
                // Also save "non matches" to avoid retrying
                _storage.SaveNewMatch(seriesName, new SeriesMatch {
                    ItemName = seriesName
                });
                return(false);
            }
            catch (Exception ex)
            {
                ServiceRegistration.Get <ILogger>().Debug("SeriesTvDbMatcher: Exception while processing series {0}", ex, seriesName);
                return(false);
            }
            finally
            {
                if (seriesDetail != null && !_memoryCache.ContainsKey(seriesName))
                {
                    _memoryCache.Add(seriesName, seriesDetail);
                }
            }
        }
예제 #2
0
        protected bool TryMatch(string seriesNameOrImdbId, bool isImdbId, bool cacheOnly, out TvdbSeries seriesDetail, int tvdbid = 0)
        {
            seriesDetail = null;
            try
            {
                // Prefer memory cache
                CheckCacheAndRefresh();
                if (_memoryCache.TryGetValue(seriesNameOrImdbId, out seriesDetail))
                {
                    if (tvdbid == 0 || seriesDetail.Id == tvdbid)
                    {
                        return(true);
                    }
                }

                // Load cache or create new list
                List <SeriesMatch> matches;
                lock (_syncObj)
                    matches = Settings.Load <List <SeriesMatch> >(MatchesSettingsFile) ?? new List <SeriesMatch>();

                // Init empty
                seriesDetail = null;

                // Use cached values before doing online query
                SeriesMatch match = matches.Find(m =>
                                                 (
                                                     string.Equals(m.ItemName, seriesNameOrImdbId, StringComparison.OrdinalIgnoreCase) ||
                                                     string.Equals(m.TvDBName, seriesNameOrImdbId, StringComparison.OrdinalIgnoreCase)
                                                 ) && (tvdbid == 0 || m.Id == tvdbid));

                ServiceRegistration.Get <ILogger>().Debug("SeriesTvDbMatcher: Try to lookup series \"{0}\" from cache: {1}", seriesNameOrImdbId, match != null && match.Id != 0);

                // Try online lookup
                if (!Init())
                {
                    return(false);
                }

                // If this is a known series, only return the series details (including episodes).
                if (match != null)
                {
                    return(match.Id != 0 && _tv.GetSeries(match.Id, true, out seriesDetail));
                }

                if (cacheOnly)
                {
                    return(false);
                }

                TvdbSearchResult matchedSeries = null;
                bool             foundResult   = false;
                if (tvdbid != 0)
                {
                    foundResult = _tv.GetSeries(tvdbid, true, out seriesDetail);
                }
                else
                if (isImdbId)
                {
                    // If we got an IMDBID, use it to lookup by key directly
                    _tv.GetSeries(seriesNameOrImdbId, out matchedSeries);
                }
                else
                {
                    // Otherwise we try to find unique series by name
                    List <TvdbSearchResult> series;
                    if (_tv.SearchSeriesUnique(seriesNameOrImdbId, out series))
                    {
                        matchedSeries = series[0];
                    }
                }

                if (matchedSeries != null)
                {
                    ServiceRegistration.Get <ILogger>().Debug("SeriesTvDbMatcher: Found unique online match for \"{0}\": \"{1}\" [Lang: {2}]", seriesNameOrImdbId, matchedSeries.SeriesName, matchedSeries.Language);
                    foundResult = _tv.GetSeries(matchedSeries.Id, true, out seriesDetail);
                }
                if (foundResult)
                {
                    ServiceRegistration.Get <ILogger>().Debug("SeriesTvDbMatcher: Loaded details for \"{0}\"", seriesDetail.SeriesName);
                    // Add this match to cache
                    SeriesMatch onlineMatch = new SeriesMatch
                    {
                        ItemName = seriesNameOrImdbId,
                        Id       = seriesDetail.Id,
                        TvDBName = seriesDetail.SeriesName
                    };

                    // Save cache
                    _storage.TryAddMatch(onlineMatch);
                    return(true);
                }

                ServiceRegistration.Get <ILogger>().Debug("SeriesTvDbMatcher: No unique match found for \"{0}\"", seriesNameOrImdbId);
                // Also save "non matches" to avoid retrying
                _storage.TryAddMatch(new SeriesMatch {
                    ItemName = seriesNameOrImdbId
                });
                return(false);
            }
            catch (Exception ex)
            {
                ServiceRegistration.Get <ILogger>().Debug("SeriesTvDbMatcher: Exception while processing series {0}", ex, seriesNameOrImdbId);
                return(false);
            }
            finally
            {
                if (seriesDetail != null)
                {
                    _memoryCache.TryAdd(seriesNameOrImdbId, seriesDetail);
                }
            }
        }