-
Notifications
You must be signed in to change notification settings - Fork 0
/
libsickbeard.py
153 lines (116 loc) · 5.08 KB
/
libsickbeard.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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 5 14:10:37 2013
@author: shanef
"""
import json
from urllib import urlopen
from fuzzywuzzy import fuzz
from operator import itemgetter
class Sickbeard:
"""
Contains operations used to interact with sickbeard
"""
def __init__(self, settings):
self.__settings = settings
self.__address = settings.sickbeardaddress()
self.__port = settings.sickbeardport()
self.__apikey = settings.sickbeardapikey()
def findshowid(self, showname):
"""
Get the tvdb show id for the show
"""
jsonurl = urlopen(self.__getapiurl()+"?cmd=shows")
result = json.loads(jsonurl.read())
showname = self.__settings.findshownameforalias(showname)
shows = []
for show in result['data']:
shows.append((show, fuzz.partial_ratio(showname.lower(),
result['data'][show]
['show_name'].lower())))
shows = sorted(shows, key=itemgetter(1), reverse=True)
if shows[0][1] > 85:
return shows[0][0]
def findepisodename(self, showid, season, episode):
"""
Get the name of an episode, given it's season and episode numbers
"""
jsonurl = urlopen("{0}?cmd=episode&tvdbid={1}&season={2}"
"&episode={3}".format(self.__getapiurl(), showid,
int(season), int(episode)))
result = json.loads(jsonurl.read())
if result['result'] == 'error':
return ""
else:
return result['data']['name']
def findepisode(self, showid, name=None, description=None):
"""
Find an episode, either by it's name or it's description. This is used
when the season and episode numbers are not known
"""
jsonurl = urlopen("{0}?cmd=show.seasons&tvdbid={1}".format(
self.__getapiurl(), showid))
result = json.loads(jsonurl.read())
for season in result['data']:
for episode in result['data'][season]:
episodename = result['data'][season][episode]['name']
if name is not None and fuzz.ratio(name.lower(),
episodename.lower()) > 85:
return (season, episode, episodename)
elif description is not None:
descriptionqueryresult = \
self.__findepisodebydescription(showid, season,
episode, description)
if descriptionqueryresult is not None:
return descriptionqueryresult
return (0, 0, '')
def fixepisodetitle(self, showname, episodetitle):
"""
Check to see if there is a prefix specified for the show. If there is,
add the prefix to the start of the episode title
"""
sickbeardprefix = \
self.__settings.getshowsickbeardepisodeprefix(showname)
if sickbeardprefix != "":
if not episodetitle.lower().startswith(sickbeardprefix.lower()):
return "{0} {1}".format(sickbeardprefix.rstrip(),
episodetitle.lstrip())
return episodetitle
def __getapiurl(self):
"""
Get the url of the sickbeard api, substituting the values from the
settings
"""
return "http://{0}:{1}/api/{2}/".format(self.__address, self.__port,
self.__apikey)
def __findepisodebydescription(self, showid, season, episode, description):
"""
Find the details of an episode by searching for it's description
"""
jsonepisodeurl = urlopen("{0}?cmd=episode&tvdbid={1}&season={2}"
"&episode={3}".format(self.__getapiurl(),
showid, season,
episode))
episoderesult = json.loads(jsonepisodeurl.read())
sickbearddescription = episoderesult['data']['description']
if fuzzystringcompare(sickbearddescription, description):
return (season, episode, episoderesult['data']['name'])
return None
def fuzzystringcompare(string1, string2, matchvalue=85, casesensitive=False):
"""
Compare two strings to see if they match it first does a straight
comparison. Secondly, it concatenates the longer string to the length of
the shorter one, and tries to compare them again.
"""
if not casesensitive:
string1 = string1.lower()
string2 = string2.lower()
if fuzz.ratio(string1, string2) > matchvalue:
return True
if len(string1) > len(string2):
if fuzz.ratio(string1[:len(string2)], string2) > matchvalue:
return True
elif len(string2) > len(string1):
if fuzz.ratio(string1, string2[:len(string1)]) > matchvalue:
return True
return False