forked from styxit/HTPC-Manager
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathsickrage.py
246 lines (208 loc) · 9.28 KB
/
sickrage.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cherrypy
import htpc
from urllib import quote, urlencode
import requests
import logging
from cherrypy.lib.auth2 import require, member_of
from htpc.helpers import fix_basepath, get_image, striphttp
class Sickrage(object):
def __init__(self):
self.logger = logging.getLogger('modules.sickrage')
htpc.MODULES.append({
'name': 'Sickrage',
'id': 'sickrage',
'test': htpc.WEBDIR + 'sickrage/ping',
'fields': [
{'type': 'bool', 'label': 'Enable', 'name': 'sickrage_enable'},
{'type': 'text', 'label': 'Menu name', 'name': 'sickrage_name'},
{'type': 'text', 'label': 'IP / Host', 'placeholder': 'localhost', 'name': 'sickrage_host'},
{'type': 'text', 'label': 'Port', 'placeholder': '8081', 'name': 'sickrage_port'},
{'type': 'text', 'label': 'Basepath', 'placeholder': '/sickrage', 'name': 'sickrage_basepath'},
{'type': 'text', 'label': 'API key', 'name': 'sickrage_apikey'},
{'type': 'bool', 'label': 'Use SSL', 'name': 'sickrage_ssl'},
{'type': 'text', 'label': 'Reverse proxy link', 'placeholder': '', 'desc':'Reverse proxy link, e.g. https://sr.domain.com', 'name': 'sickrage_reverse_proxy_link'}
]
})
@cherrypy.expose()
@require()
def index(self):
return htpc.LOOKUP.get_template('sickrage.html').render(scriptname='sickrage', webinterface=self.webinterface())
def webinterface(self):
host = striphttp(htpc.settings.get('sickrage_host', ''))
port = str(htpc.settings.get('sickrage_port', ''))
apikey = htpc.settings.get('sickrage_apikey', '')
ssl = 's' if htpc.settings.get('sickrage_ssl', 0) else ''
sickrage_basepath = fix_basepath(htpc.settings.get('sickrage_basepath', '/'))
url = 'http%s://%s:%s%s' % (ssl, host, port, sickrage_basepath)
if htpc.settings.get('sickrage_reverse_proxy_link'):
url = htpc.settings.get('sickrage_reverse_proxy_link')
return url
@cherrypy.expose()
@require()
def view(self, indexerid):
if not (indexerid.isdigit()):
raise cherrypy.HTTPError('500 Error', 'Invalid show ID.')
self.logger.error('Invalid show ID was supplied: ' + str(indexerid))
return False
return htpc.LOOKUP.get_template('sickrage_view.html').render(scriptname='sickrage_view', indexerid=indexerid)
@cherrypy.expose()
@require(member_of(htpc.role_admin))
@cherrypy.tools.json_out()
def ping(self, sickrage_host, sickrage_port, sickrage_apikey, sickrage_basepath, sickrage_ssl=False, **kwargs):
ssl = 's' if sickrage_ssl else ''
self.logger.debug('Testing connectivity')
try:
sickrage_basepath = fix_basepath(sickrage_basepath)
url = 'http%s://%s:%s%sapi/%s/?cmd=sb.ping' % (ssl, striphttp(sickrage_host), sickrage_port, sickrage_basepath, sickrage_apikey)
self.logger.debug('Trying to contact sickrage via %s' % url)
response = requests.get(url, timeout=10, verify=False)
ret = response.json()
if ret.get('result') == 'success':
self.logger.debug('Sickrage connectivity test success')
return ret
except:
self.logger.error('Unable to contact sickrage via %s' % url)
return
@cherrypy.expose()
@require()
@cherrypy.tools.json_out()
def GetShowList(self):
self.logger.debug('Fetching Show list')
return self.fetch('shows&sort=name', False, 200)
@cherrypy.expose()
@require()
@cherrypy.tools.json_out()
def GetNextAired(self):
self.logger.debug('Fetching Next Aired Episodes')
return self.fetch('future')
@cherrypy.expose()
@require()
def GetBanner(self, indexerid):
self.logger.debug('Fetching Banner')
cherrypy.response.headers['Content-Type'] = 'image/jpeg'
return self.fetch('show.getbanner&indexerid=' + indexerid, True)
@cherrypy.expose()
@require()
def GetPoster(self, indexerid):
self.logger.debug('Fetching Poster')
cherrypy.response.headers['Content-Type'] = 'image/jpeg'
return self.fetch('show.getposter&indexerid=' + indexerid, True)
@cherrypy.expose()
@require()
@cherrypy.tools.json_out()
def GetHistory(self, limit=''):
self.logger.debug('Fetching History')
return self.fetch('history&limit=' + limit)
@cherrypy.expose()
@require()
@cherrypy.tools.json_out()
def GetLogs(self):
self.logger.debug('Fetching Logs')
return self.fetch('logs&min_level=info')
@cherrypy.expose()
@require()
@cherrypy.tools.json_out()
def AddShow(self, indexername='', indexerid='', **kwargs):
# indexername=tvrageid or tvdbid
self.logger.debug('Adding a Show')
return self.fetch('show.addnew&' + urlencode(kwargs))
@cherrypy.expose()
@require()
@cherrypy.tools.json_out()
def GetShow(self, indexerid):
self.logger.debug('Fetching Show')
return self.fetch('show&indexerid=' + indexerid)
@cherrypy.expose()
@require()
@cherrypy.tools.json_out()
def GetEpisode(self, strShowID, strSeason, strEpisode):
return self.fetch('episode&indexerid=' + strShowID + '&season=' + strSeason + '&episode=' + strEpisode + '&full_path=1')
@cherrypy.expose()
@require()
@cherrypy.tools.json_out()
def GetSeason(self, indexerid, season):
self.logger.debug('Fetching Season')
return self.fetch('show.seasons&indexerid=' + indexerid + '&season=' + season)
@cherrypy.expose()
@require(member_of(htpc.role_user))
@cherrypy.tools.json_out()
def Postprocess(self, path='', force_replace=False, return_data=False, is_priority=False, type=False):
self.logger.debug('Postprocess')
if path:
path = '&%s' % path
return self.fetch('postprocess' + path, False, 120)
@cherrypy.expose()
@require(member_of(htpc.role_user))
@cherrypy.tools.json_out()
def Restart(self):
self.logger.debug('Restart sr')
return self.fetch('sb.restart', False, 15)
@cherrypy.expose()
@require(member_of(htpc.role_user))
@cherrypy.tools.json_out()
def SearchEpisodeDownload(self, indexerid, season, episode):
self.logger.debug('Fetching Episode Downloads')
return self.fetch('episode.search&indexerid=' + indexerid + '&season=' + season + '&episode=' + episode, False, 45)
@cherrypy.expose()
@require(member_of(htpc.role_user))
@cherrypy.tools.json_out()
def SearchSubtitle(self, indexerid, season, episode):
self.logger.debug('Fetching subtitle')
return self.fetch('episode.subtitlesearch&indexerid=' + indexerid + '&season=' + season + '&episode=' + episode, False, 45)
@cherrypy.expose()
@require(member_of(htpc.role_user))
@cherrypy.tools.json_out()
def Shutdown(self):
self.logger.debug('Shutdown sickrage')
return self.fetch('sb.shutdown', False, 20)
@cherrypy.expose()
@require(member_of(htpc.role_user))
@cherrypy.tools.json_out()
def ForceFullUpdate(self, indexerid):
self.logger.debug('Force full update for indexerid %s' % indexerid)
return self.fetch('show.update&indexerid=' + indexerid)
@cherrypy.expose()
@require(member_of(htpc.role_user))
@cherrypy.tools.json_out()
def RescanFiles(self, indexerid):
self.logger.debug('Rescan all local files for indexerid %s' % indexerid)
return self.fetch('show.refresh&indexerid=' + indexerid)
@cherrypy.expose()
@cherrypy.tools.json_out()
@require(member_of(htpc.role_user))
def RemoveShow(self, indexerid, show_name=''):
self.logger.debug('Delete %s from Sickrage indexerid %s' % (show_name, indexerid))
return self.fetch('show.delete&indexerid=%s' % indexerid)
@cherrypy.expose()
@cherrypy.tools.json_out()
@require()
def SearchShow(self, query):
self.logger.debug('Searching tvdb and tvrage for %s query')
return self.fetch('sb.searchindexers&indexer=0&name=%s' % quote(query), False, 60)
@cherrypy.expose()
@require()
@cherrypy.tools.json_out()
def ShowsStats(self):
self.logger.debug('Grabbing tvrage statistics')
return self.fetch('shows.stats')
def fetch(self, cmd, img=False, timeout=20):
host = striphttp(htpc.settings.get('sickrage_host', ''))
port = str(htpc.settings.get('sickrage_port', ''))
apikey = htpc.settings.get('sickrage_apikey', '')
ssl = 's' if htpc.settings.get('sickrage_ssl', 0) else ''
sickrage_basepath = fix_basepath(htpc.settings.get('sickrage_basepath', '/'))
url = 'http%s://%s:%s%sapi/%s/?cmd=%s' % (ssl, host, port, sickrage_basepath, apikey, cmd)
self.logger.debug('Fetching information from: %s' % url)
try:
if img is True:
# Cache the images
return get_image(url)
res = requests.get(url, timeout=timeout, verify=False)
return res.json()
except Exception as e:
self.logger.error('Unable to fetch information')
self.logger.error(url)
self.logger.error(e)
return