< Summary - Jellyfin

Information
Class: MediaBrowser.Providers.Plugins.Tmdb.People.TmdbPersonProvider
Assembly: MediaBrowser.Providers
File(s): /srv/git/jellyfin/MediaBrowser.Providers/Plugins/Tmdb/People/TmdbPersonProvider.cs
Line coverage
60%
Covered lines: 3
Uncovered lines: 2
Coverable lines: 5
Total lines: 144
Line coverage: 60%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%210%
GetImageResponse(...)100%210%

File(s)

/srv/git/jellyfin/MediaBrowser.Providers/Plugins/Tmdb/People/TmdbPersonProvider.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.Net.Http;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using MediaBrowser.Common.Net;
 8using MediaBrowser.Controller.Entities;
 9using MediaBrowser.Controller.Providers;
 10using MediaBrowser.Model.Entities;
 11using MediaBrowser.Model.Providers;
 12
 13namespace MediaBrowser.Providers.Plugins.Tmdb.People
 14{
 15    /// <summary>
 16    /// Person image provider powered by TMDb.
 17    /// </summary>
 18    public class TmdbPersonProvider : IRemoteMetadataProvider<Person, PersonLookupInfo>
 19    {
 20        private readonly IHttpClientFactory _httpClientFactory;
 21        private readonly TmdbClientManager _tmdbClientManager;
 22
 23        /// <summary>
 24        /// Initializes a new instance of the <see cref="TmdbPersonProvider"/> class.
 25        /// </summary>
 26        /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param>
 27        /// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param>
 28        public TmdbPersonProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
 29        {
 2230            _httpClientFactory = httpClientFactory;
 2231            _tmdbClientManager = tmdbClientManager;
 2232        }
 33
 34        /// <inheritdoc />
 035        public string Name => TmdbUtils.ProviderName;
 36
 37        /// <inheritdoc />
 38        public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(PersonLookupInfo searchInfo, CancellationTok
 39        {
 40            if (searchInfo.TryGetProviderId(MetadataProvider.Tmdb, out var personTmdbId))
 41            {
 42                var personResult = await _tmdbClientManager.GetPersonAsync(int.Parse(personTmdbId, CultureInfo.Invariant
 43
 44                if (personResult is not null)
 45                {
 46                    var result = new RemoteSearchResult
 47                    {
 48                        Name = personResult.Name,
 49                        SearchProviderName = Name,
 50                        Overview = personResult.Biography
 51                    };
 52
 53                    if (personResult.Images?.Profiles is not null && personResult.Images.Profiles.Count > 0)
 54                    {
 55                        result.ImageUrl = _tmdbClientManager.GetProfileUrl(personResult.Images.Profiles[0].FilePath);
 56                    }
 57
 58                    result.SetProviderId(MetadataProvider.Tmdb, personResult.Id.ToString(CultureInfo.InvariantCulture));
 59                    result.TrySetProviderId(MetadataProvider.Imdb, personResult.ExternalIds.ImdbId);
 60
 61                    return new[] { result };
 62                }
 63            }
 64
 65            var personSearchResult = await _tmdbClientManager.SearchPersonAsync(searchInfo.Name, cancellationToken).Conf
 66
 67            var remoteSearchResults = new RemoteSearchResult[personSearchResult.Count];
 68            for (var i = 0; i < personSearchResult.Count; i++)
 69            {
 70                var person = personSearchResult[i];
 71                var remoteSearchResult = new RemoteSearchResult
 72                {
 73                    SearchProviderName = Name,
 74                    Name = person.Name,
 75                    ImageUrl = _tmdbClientManager.GetProfileUrl(person.ProfilePath)
 76                };
 77
 78                remoteSearchResult.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture)
 79                remoteSearchResults[i] = remoteSearchResult;
 80            }
 81
 82            return remoteSearchResults;
 83        }
 84
 85        /// <inheritdoc />
 86        public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo info, CancellationToken cancellationToken
 87        {
 88            var personTmdbId = Convert.ToInt32(info.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
 89
 90            // We don't already have an Id, need to fetch it
 91            if (personTmdbId <= 0)
 92            {
 93                var personSearchResults = await _tmdbClientManager.SearchPersonAsync(info.Name, cancellationToken).Confi
 94                if (personSearchResults.Count > 0)
 95                {
 96                    personTmdbId = personSearchResults[0].Id;
 97                }
 98            }
 99
 100            var result = new MetadataResult<Person>();
 101
 102            if (personTmdbId > 0)
 103            {
 104                var person = await _tmdbClientManager.GetPersonAsync(personTmdbId, info.MetadataLanguage, cancellationTo
 105                if (person is null)
 106                {
 107                    return result;
 108                }
 109
 110                result.HasMetadata = true;
 111
 112                var item = new Person
 113                {
 114                    // Take name from incoming info, don't rename the person
 115                    // TODO: This should go in PersonMetadataService, not each person provider
 116                    Name = info.Name,
 117                    HomePageUrl = person.Homepage,
 118                    Overview = person.Biography,
 119                    PremiereDate = person.Birthday?.ToUniversalTime(),
 120                    EndDate = person.Deathday?.ToUniversalTime()
 121                };
 122
 123                if (!string.IsNullOrWhiteSpace(person.PlaceOfBirth))
 124                {
 125                    item.ProductionLocations = new[] { person.PlaceOfBirth };
 126                }
 127
 128                item.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture));
 129                item.TrySetProviderId(MetadataProvider.Imdb, person.ImdbId);
 130
 131                result.HasMetadata = true;
 132                result.Item = item;
 133            }
 134
 135            return result;
 136        }
 137
 138        /// <inheritdoc />
 139        public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
 140        {
 0141            return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
 142        }
 143    }
 144}