-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMovieDb.cs
92 lines (74 loc) · 3.33 KB
/
MovieDb.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using n0tFlix.Subtitles.Subscene.Models;
using MediaBrowser.Common;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Serialization;
namespace n0tFlix.Subtitles.Subscene
{
public class MovieDb
{
private const string token = "d9d7bb04fb2c52c2b594c5e30065c23c";// Get https://www.themoviedb.org/ API token
private readonly string _movieUrl = "https://api.themoviedb.org/3/movie/{0}?api_key={1}";
private readonly string _tvUrl = "https://api.themoviedb.org/3/tv/{0}?api_key={1}";
private readonly string _searchMovie = "https://api.themoviedb.org/3/find/{0}?api_key={1}&external_source={2}";
private readonly IJsonSerializer _jsonSerializer;
private readonly IHttpClient _httpClient;
private readonly IApplicationHost _appHost;
public MovieDb(IJsonSerializer jsonSerializer, IHttpClient httpClient, IApplicationHost appHost)
{
_jsonSerializer = jsonSerializer;
_httpClient = httpClient;
_appHost = appHost;
}
public async Task<MovieInformation> GetMovieInfo(string id)
{
var opts = BaseRequestOptions;
opts.Url = string.Format(_movieUrl, id, token);
/*var searchResults = await Tools.RequestUrl<MovieInformation>(opts.Url, "", HttpMethod.Get);
return searchResults;*/
using (var response = await _httpClient.GetResponse(opts)/*.ConfigureAwait(false)*/)
{
if (response.ContentLength < 0)
return null;
var searchResults = _jsonSerializer.DeserializeFromStream<MovieInformation>(response.Content);
return searchResults;
}
}
public async Task<FindMovie> SearchMovie(string id)
{
var opts = BaseRequestOptions;
var type = id.StartsWith("tt") ? MovieSourceType.imdb_id : MovieSourceType.tvdb_id;
opts.Url = string.Format(_searchMovie, id, token, type.ToString());
using (var response = await _httpClient.GetResponse(opts).ConfigureAwait(false))
{
if (response.ContentLength < 0)
return null;
var searchResults = _jsonSerializer.DeserializeFromStream<FindMovie>(response.Content);
return searchResults;
}
}
public async Task<TvInformation> GetTvInfo(string id)
{
var movie = await SearchMovie(id);
if (movie?.tv_episode_results == null || !movie.tv_episode_results.Any())
return null;
var opts = BaseRequestOptions;
opts.Url = string.Format(_tvUrl, movie.tv_episode_results.First().show_id, token);
using (var response = await _httpClient.GetResponse(opts).ConfigureAwait(false))
{
if (response.ContentLength < 0)
return null;
var searchResults = _jsonSerializer.DeserializeFromStream<TvInformation>(response.Content);
return searchResults;
}
}
private HttpRequestOptions BaseRequestOptions => new HttpRequestOptions
{
UserAgent = $"Jellyfin/{_appHost?.ApplicationVersion}"
};
}
}