| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CS1591, SA1300 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Net; |
| | | 10 | | using System.Net.Http; |
| | | 11 | | using System.Net.Http.Json; |
| | | 12 | | using System.Text; |
| | | 13 | | using System.Text.Json; |
| | | 14 | | using System.Threading; |
| | | 15 | | using System.Threading.Tasks; |
| | | 16 | | using MediaBrowser.Common.Net; |
| | | 17 | | using MediaBrowser.Controller.Configuration; |
| | | 18 | | using MediaBrowser.Controller.Entities; |
| | | 19 | | using MediaBrowser.Controller.Entities.Movies; |
| | | 20 | | using MediaBrowser.Controller.Entities.TV; |
| | | 21 | | using MediaBrowser.Controller.Library; |
| | | 22 | | using MediaBrowser.Controller.Providers; |
| | | 23 | | using MediaBrowser.Model.Entities; |
| | | 24 | | using MediaBrowser.Model.IO; |
| | | 25 | | using MediaBrowser.Model.Providers; |
| | | 26 | | |
| | | 27 | | namespace MediaBrowser.Providers.Plugins.Omdb |
| | | 28 | | { |
| | | 29 | | public class OmdbItemProvider : IRemoteMetadataProvider<Series, SeriesInfo>, |
| | | 30 | | IRemoteMetadataProvider<Movie, MovieInfo>, IRemoteMetadataProvider<Trailer, TrailerInfo>, IHasOrder |
| | | 31 | | { |
| | | 32 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 33 | | private readonly ILibraryManager _libraryManager; |
| | | 34 | | private readonly JsonSerializerOptions _jsonOptions; |
| | | 35 | | private readonly OmdbProvider _omdbProvider; |
| | | 36 | | |
| | | 37 | | public OmdbItemProvider( |
| | | 38 | | IHttpClientFactory httpClientFactory, |
| | | 39 | | ILibraryManager libraryManager, |
| | | 40 | | IFileSystem fileSystem, |
| | | 41 | | IServerConfigurationManager configurationManager) |
| | | 42 | | { |
| | 54 | 43 | | _httpClientFactory = httpClientFactory; |
| | 54 | 44 | | _libraryManager = libraryManager; |
| | 54 | 45 | | _omdbProvider = new OmdbProvider(_httpClientFactory, fileSystem, configurationManager); |
| | | 46 | | |
| | 54 | 47 | | _jsonOptions = OmdbProvider.CreateJsonOptions(); |
| | 54 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | public string Name => "The Open Movie Database"; |
| | | 51 | | |
| | | 52 | | // After primary option |
| | 0 | 53 | | public int Order => 2; |
| | | 54 | | |
| | | 55 | | public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(TrailerInfo searchInfo, CancellationToken cancella |
| | | 56 | | { |
| | 0 | 57 | | return GetSearchResultsInternal(searchInfo, true, cancellationToken); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellat |
| | | 61 | | { |
| | 0 | 62 | | return GetSearchResultsInternal(searchInfo, true, cancellationToken); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellati |
| | | 66 | | { |
| | 0 | 67 | | return GetSearchResultsInternal(searchInfo, true, cancellationToken); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancella |
| | | 71 | | { |
| | 0 | 72 | | return GetSearchResultsInternal(searchInfo, true, cancellationToken); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | private async Task<IEnumerable<RemoteSearchResult>> GetSearchResultsInternal(ItemLookupInfo searchInfo, bool isS |
| | | 76 | | { |
| | 0 | 77 | | var type = searchInfo switch |
| | 0 | 78 | | { |
| | 0 | 79 | | EpisodeInfo => "episode", |
| | 0 | 80 | | SeriesInfo => "series", |
| | 0 | 81 | | _ => "movie" |
| | 0 | 82 | | }; |
| | | 83 | | |
| | | 84 | | // This is a bit hacky? |
| | 0 | 85 | | var episodeSearchInfo = searchInfo as EpisodeInfo; |
| | 0 | 86 | | var indexNumberEnd = episodeSearchInfo?.IndexNumberEnd; |
| | | 87 | | |
| | 0 | 88 | | var imdbId = searchInfo.GetProviderId(MetadataProvider.Imdb); |
| | | 89 | | |
| | 0 | 90 | | var urlQuery = new StringBuilder("plot=full&r=json"); |
| | 0 | 91 | | if (episodeSearchInfo is not null) |
| | | 92 | | { |
| | 0 | 93 | | episodeSearchInfo.SeriesProviderIds.TryGetValue(MetadataProvider.Imdb.ToString(), out imdbId); |
| | 0 | 94 | | if (searchInfo.IndexNumber.HasValue) |
| | | 95 | | { |
| | 0 | 96 | | urlQuery.Append("&Episode=").Append(searchInfo.IndexNumber.Value); |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | if (searchInfo.ParentIndexNumber.HasValue) |
| | | 100 | | { |
| | 0 | 101 | | urlQuery.Append("&Season=").Append(searchInfo.ParentIndexNumber.Value); |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | if (string.IsNullOrWhiteSpace(imdbId)) |
| | | 106 | | { |
| | 0 | 107 | | var name = searchInfo.Name; |
| | 0 | 108 | | var year = searchInfo.Year; |
| | 0 | 109 | | if (!string.IsNullOrWhiteSpace(name)) |
| | | 110 | | { |
| | 0 | 111 | | var parsedName = _libraryManager.ParseName(name); |
| | 0 | 112 | | var yearInName = parsedName.Year; |
| | 0 | 113 | | name = parsedName.Name; |
| | 0 | 114 | | year ??= yearInName; |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | if (year.HasValue) |
| | | 118 | | { |
| | 0 | 119 | | urlQuery.Append("&y=").Append(year); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | // &s means search and returns a list of results as opposed to t |
| | 0 | 123 | | urlQuery.Append(isSearch ? "&s=" : "&t="); |
| | 0 | 124 | | urlQuery.Append(WebUtility.UrlEncode(name)); |
| | 0 | 125 | | urlQuery.Append("&type=") |
| | 0 | 126 | | .Append(type); |
| | | 127 | | } |
| | | 128 | | else |
| | | 129 | | { |
| | 0 | 130 | | urlQuery.Append("&i=") |
| | 0 | 131 | | .Append(imdbId); |
| | 0 | 132 | | isSearch = false; |
| | | 133 | | } |
| | | 134 | | |
| | 0 | 135 | | var url = OmdbProvider.GetOmdbUrl(urlQuery.ToString()); |
| | | 136 | | |
| | 0 | 137 | | using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationTo |
| | 0 | 138 | | if (isSearch) |
| | | 139 | | { |
| | 0 | 140 | | var searchResultList = await response.Content.ReadFromJsonAsync<SearchResultList>(_jsonOptions, cancella |
| | 0 | 141 | | if (searchResultList?.Search is not null) |
| | | 142 | | { |
| | 0 | 143 | | var resultCount = searchResultList.Search.Count; |
| | 0 | 144 | | var result = new RemoteSearchResult[resultCount]; |
| | 0 | 145 | | for (var i = 0; i < resultCount; i++) |
| | | 146 | | { |
| | 0 | 147 | | result[i] = ResultToMetadataResult(searchResultList.Search[i], searchInfo, indexNumberEnd); |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | return result; |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | else |
| | | 154 | | { |
| | 0 | 155 | | var result = await response.Content.ReadFromJsonAsync<SearchResult>(_jsonOptions, cancellationToken).Con |
| | 0 | 156 | | if (string.Equals(result?.Response, "true", StringComparison.OrdinalIgnoreCase)) |
| | | 157 | | { |
| | 0 | 158 | | return new[] { ResultToMetadataResult(result, searchInfo, indexNumberEnd) }; |
| | | 159 | | } |
| | | 160 | | } |
| | | 161 | | |
| | 0 | 162 | | return Enumerable.Empty<RemoteSearchResult>(); |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | public Task<MetadataResult<Trailer>> GetMetadata(TrailerInfo info, CancellationToken cancellationToken) |
| | | 166 | | { |
| | 0 | 167 | | return GetResult<Trailer>(info, cancellationToken); |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | public Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken) |
| | | 171 | | { |
| | 0 | 172 | | return GetResult<Series>(info, cancellationToken); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | public Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken) |
| | | 176 | | { |
| | 0 | 177 | | return GetResult<Movie>(info, cancellationToken); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | private RemoteSearchResult ResultToMetadataResult(SearchResult result, ItemLookupInfo searchInfo, int? indexNumb |
| | | 181 | | { |
| | 0 | 182 | | var item = new RemoteSearchResult |
| | 0 | 183 | | { |
| | 0 | 184 | | IndexNumber = searchInfo.IndexNumber, |
| | 0 | 185 | | Name = result.Title, |
| | 0 | 186 | | ParentIndexNumber = searchInfo.ParentIndexNumber, |
| | 0 | 187 | | SearchProviderName = Name, |
| | 0 | 188 | | IndexNumberEnd = indexNumberEnd |
| | 0 | 189 | | }; |
| | | 190 | | |
| | 0 | 191 | | item.SetProviderId(MetadataProvider.Imdb, result.imdbID); |
| | | 192 | | |
| | 0 | 193 | | if (OmdbProvider.TryParseYear(result.Year, out var parsedYear)) |
| | | 194 | | { |
| | 0 | 195 | | item.ProductionYear = parsedYear; |
| | | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | if (!string.IsNullOrEmpty(result.Released) |
| | 0 | 199 | | && DateTime.TryParse(result.Released, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out |
| | | 200 | | { |
| | 0 | 201 | | item.PremiereDate = released; |
| | | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | if (!string.IsNullOrWhiteSpace(result.Poster)) |
| | | 205 | | { |
| | 0 | 206 | | item.ImageUrl = result.Poster; |
| | | 207 | | } |
| | | 208 | | |
| | 0 | 209 | | return item; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | private async Task<MetadataResult<T>> GetResult<T>(ItemLookupInfo info, CancellationToken cancellationToken) |
| | | 213 | | where T : BaseItem, new() |
| | | 214 | | { |
| | 0 | 215 | | var result = new MetadataResult<T> |
| | 0 | 216 | | { |
| | 0 | 217 | | Item = new T(), |
| | 0 | 218 | | QueriedById = true, |
| | 0 | 219 | | // OMDb is not localized, everything it returns is English |
| | 0 | 220 | | ResultLanguage = "en" |
| | 0 | 221 | | }; |
| | | 222 | | |
| | 0 | 223 | | var imdbId = info.GetProviderId(MetadataProvider.Imdb); |
| | 0 | 224 | | if (string.IsNullOrWhiteSpace(imdbId)) |
| | | 225 | | { |
| | 0 | 226 | | imdbId = await GetImdbId(info, cancellationToken).ConfigureAwait(false); |
| | 0 | 227 | | result.QueriedById = false; |
| | | 228 | | } |
| | | 229 | | |
| | 0 | 230 | | if (!string.IsNullOrEmpty(imdbId)) |
| | | 231 | | { |
| | 0 | 232 | | result.Item.SetProviderId(MetadataProvider.Imdb, imdbId); |
| | 0 | 233 | | result.HasMetadata = true; |
| | | 234 | | |
| | 0 | 235 | | await _omdbProvider.Fetch(result, imdbId, info.MetadataLanguage, info.MetadataCountryCode, cancellationT |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | return result; |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | private async Task<string> GetImdbId(ItemLookupInfo info, CancellationToken cancellationToken) |
| | | 242 | | { |
| | 0 | 243 | | var results = await GetSearchResultsInternal(info, false, cancellationToken).ConfigureAwait(false); |
| | 0 | 244 | | var first = results.FirstOrDefault(); |
| | 0 | 245 | | return first?.GetProviderId(MetadataProvider.Imdb); |
| | 0 | 246 | | } |
| | | 247 | | |
| | | 248 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 249 | | { |
| | 0 | 250 | | return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken); |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | private class SearchResult |
| | | 254 | | { |
| | | 255 | | public string Title { get; set; } |
| | | 256 | | |
| | | 257 | | public string Year { get; set; } |
| | | 258 | | |
| | | 259 | | public string Rated { get; set; } |
| | | 260 | | |
| | | 261 | | public string Released { get; set; } |
| | | 262 | | |
| | | 263 | | public string Season { get; set; } |
| | | 264 | | |
| | | 265 | | public string Episode { get; set; } |
| | | 266 | | |
| | | 267 | | public string Runtime { get; set; } |
| | | 268 | | |
| | | 269 | | public string Genre { get; set; } |
| | | 270 | | |
| | | 271 | | public string Director { get; set; } |
| | | 272 | | |
| | | 273 | | public string Writer { get; set; } |
| | | 274 | | |
| | | 275 | | public string Actors { get; set; } |
| | | 276 | | |
| | | 277 | | public string Plot { get; set; } |
| | | 278 | | |
| | | 279 | | public string Language { get; set; } |
| | | 280 | | |
| | | 281 | | public string Country { get; set; } |
| | | 282 | | |
| | | 283 | | public string Awards { get; set; } |
| | | 284 | | |
| | | 285 | | public string Poster { get; set; } |
| | | 286 | | |
| | | 287 | | public string Metascore { get; set; } |
| | | 288 | | |
| | | 289 | | public string imdbRating { get; set; } |
| | | 290 | | |
| | | 291 | | public string imdbVotes { get; set; } |
| | | 292 | | |
| | | 293 | | public string imdbID { get; set; } |
| | | 294 | | |
| | | 295 | | public string seriesID { get; set; } |
| | | 296 | | |
| | | 297 | | public string Type { get; set; } |
| | | 298 | | |
| | | 299 | | public string Response { get; set; } |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | private class SearchResultList |
| | | 303 | | { |
| | | 304 | | /// <summary> |
| | | 305 | | /// Gets or sets the results. |
| | | 306 | | /// </summary> |
| | | 307 | | /// <value>The results.</value> |
| | | 308 | | public List<SearchResult> Search { get; set; } |
| | | 309 | | } |
| | | 310 | | } |
| | | 311 | | } |