This repository was archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtvdb_api.py
More file actions
195 lines (133 loc) · 4.99 KB
/
tvdb_api.py
File metadata and controls
195 lines (133 loc) · 4.99 KB
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
# season.py
import requests
import json
import datetime
class Tvdb_api:
def __init__(self, config):
# path to config file
self.config = config
with open(self.config) as api_file:
self.api = json.load(api_file)
self.headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + self.api['token']
}
self.apiMsg = "[TVDB] ERROR: Not a valid request.\n"
self.updateTokenMsg = "[TVDB] Requesting new token\n"
def make_req(self, query):
# make request
res = requests.get(query, headers=self.headers)
if res.status_code not in [200, 401]:
print self.apiMsg
exit()
if res.status_code != 401:
return res.json()
if self.req_new_token() != 401:
print self.updateTokenMsg
res = requests.get(query, headers=self.headers)
if res.status_code not in [200, 401]:
print self.apiMsg
exit()
return res.json()
print '[TVDB] Couldn\'t refresh the token. Making request to the /login page for a new token'
self.authenticate()
return requests.get(query, headers=self.headers).json()
def authenticate(self):
# {api}/login
query = self.api['site'] + '/login'
data = json.dumps({
"apikey": self.api['apikey'].encode('utf-8'),
"userkey": self.api['userkey'].encode('utf-8'),
"username": self.api['username'].encode('utf-8')
})
header = {
"Content-Type": "application/json"
}
# make request
res = requests.post(query, data=data, headers=header)
if res.status_code == 401:
print '[TVDB] ERROR: Couldn\'t ask for a new token in the /login route'
exit()
# updating token
self.update_token(res.json()['token'])
def req_new_token(self):
# {api}/refresh_token
query = self.api['site'] + '/refresh_token'
# make request
res = requests.get(query, headers=self.headers)
if res.status_code != 401:
self.update_token(res.json()['token'])
return res.status_code
def update_token(self, token):
self.api['token'] = token
self.headers['Authorization'] = 'Bearer ' + token
# store the new token in the tvdb_api.config
with open(self.config, 'w') as api_file:
json.dump(self.api, api_file, indent=4)
def getSeriesID(self,name):
# {api}/search/series?name={show_name}
query = self.api['site'] + '/search/series?name=' + name.replace(' ','%20')
# make request
data = self.make_req(query)['data']
if len(data) == 0:
print 'Show not found'
exit()
if len(data) == 1:
return data[0]['id']
# grab the one that is still going on
for show in data:
if show['status'] == 'Continuing':
return show['id']
return data[0]['id']
def getSeriesInfo(self,name):
# get ID
seriesID = str(self.getSeriesID(name))
# {api}/search/series?name={show_name}
query = self.api['site'] + '/series/' + seriesID + '/episodes'
# make request
return self.make_req(query)
def countEpisodesOfSeason(self,name,season):
# get ID
info = self.getSeriesInfo(name)
data = info['data']
counter = 0
for item in data:
# Episode X in the name means it isn't out yet
if item['airedSeason'] == season and 'Episode' not in item['episodeName']:
counter += 1
return counter
def getLastEpisode(self, name):
# get ID
info = self.getSeriesInfo(name)
data = info['data']
season = 0
episode = 0
today = datetime.datetime.today()
for item in data:
# there's not a release date for this episode yet
if len(item['firstAired']) == 0:
continue
# verify if this episode has already been released or not
ep_release_date = datetime.datetime.strptime(item['firstAired'],'%Y-%m-%d')
if today < ep_release_date:
continue
# get the latest season
if item['airedSeason'] > season:
season = item['airedSeason']
episode = item['airedEpisodeNumber']
continue
#get the latest episode
if item['airedEpisodeNumber'] > episode and item['airedSeason'] == season:
episode = item['airedEpisodeNumber']
if season < 10:
wanted = 's0'
else:
wanted = 's'
wanted += str(season)
if episode < 10:
wanted += 'e0'
else:
wanted += 'e'
wanted += str(episode)
return wanted