This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_show.rb
121 lines (99 loc) · 3.8 KB
/
api_show.rb
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def search_show(title)
#Look up tvdbId of all already-downloaded shows
#Used for checking if the searched show is already downloaded or not
tvdbid_have_list = Array.new
JSON.parse(api_query("series", ""), :symbolize_names => true).each do |show|
tvdbid_have_list.push(show[:tvdbId])
end
results = JSON.parse(api_query("series/lookup", "term=#{title}"), :symbolize_names => true)
return if results.count == 0
shows = Array.new
results.each do |r|
if ! r[:tvdbId].nil?
shows.insert(-1, { :tvdbid => r[:tvdbId], :title => r[:title], :year => r[:year], :downloaded => search_show_local(r[:tvdbId]),:season_count => r[:seasons].count})
end
end
return shows
end
def add_show(tvdbid)
#Search for this show via tvdbid to get it as the only result
#Sonarr needs the JSON from the result as the submission to actually download the show
search_results = JSON.parse(api_query("series/lookup", "term=tvdb:#{tvdbid}"))[0]
#Copy over only the objects that Sonarr requires from the search result JSON
search_json = Hash.new
search_json['tvdbId'] = search_results['tvdbId']
search_json['title'] = search_results['title']
search_json['qualityProfileId'] = search_results['qualityProfileId']
search_json['titleSlug'] = search_results['titleSlug']
search_json['images'] = search_results['images']
search_json['seasons'] = search_results['seasons']
search_json['rootFolderPath'] = JSON.parse(api_query("rootfolder", ""))[0]['path']
search_json['ProfileId'] = @show_profile_id
search_json['addOptions'] = {
"ignoreEpisodesWithFiles": false,
"ignoreEpisodesWithoutFiles": false,
"searchForMissingEpisodes": true
}
#Get album art from search_results
album_art_url = search_results['remotePoster']
#POST the 'search_json.to_json' text to Sonarr to start the download
add_results = JSON.parse(api_query('series', '', search_json.to_json))
#success message is a hash {}. errors are an array [] of hashes {}
#extract the hash at index 0 of error array so that it's just a hash
if add_results.is_a? Array then
add_results = add_results[0]
end
if add_results['tvdbId'].to_s == tvdbid
return true, add_results['title'], album_art_url
else
puts "Failure adding show! tvdbID: #{tvdbid}. Dumping response object..."
pp add_results
return false, search_results['title'], nil, add_results['errorMessage']
end
end
def get_show_profile_list
endpoint = "profile"
results = JSON.parse(api_query(endpoint, ""))
profiles = Array.new
results.each {|r| profiles.insert(-1, { "name" => r["name"], "profile_id" => r["id"]})}
# [ {"1080p" => "z9y8x7w6v5u4..."}, {"720p" => "a1b2c3d4e5..."} ]
return profiles
end
def search_show_local(imdbid)
JSON.parse(api_query("series", ""), :symbolize_names => true).each do |show|
return true if show[:tvdbId] == imdbid
end
return false
end
def api_query(endpoint, query, postdata = "")
url = URI.parse("#{@show_url}:#{@show_port}#{@show_context}/api/#{endpoint}?#{URI.escape(query)}&apikey=#{@show_api_key}")
if postdata == "" then
#Request being made without POST data
request = Net::HTTP.new(url.host, url.port)
request.use_ssl = url.scheme == 'https'
request.read_timeout = @show_timeout
request.open_timeout = @show_timeout
begin
#Make the request
response = request.start {|req| req.get(url) }
rescue
raise $!.message
end
return response.body
else
#Request being made via POST
request = Net::HTTP::Post.new(url)
request.body = postdata
#request.read_timeout = @show_timeout
#request.open_timeout = @show_timeout
#Make the request
begin
response = Net::HTTP.start(url.hostname, url.port) do |http|
http.request(request)
end
rescue
raise $!.message
end
return response.body
end
end