-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheTvDbEpisode.py
117 lines (107 loc) · 3.78 KB
/
TheTvDbEpisode.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
import requests
import json
import boto3
import yaml
from pprint import pprint
APIURL = "https://api.thetvdb.com"
def getCredentials():
# Returns dictionary with credentials
try:
with open("credentials.yml", "r") as ymlfile:
fileContent = yaml.load(ymlfile)
apiKey = fileContent["thetvdb-key"]
assert len(apiKey) == 16, "Invalid API Key"
userName = fileContent["username"]
assert len(userName) == 11, "Invalid username"
userKey = fileContent["user-key"]
assert len(userKey) == 16, "Invalid user key"
ymlfile.close()
return {"apikey": apiKey, "username": userName, "userkey": userKey}
except IOError:
print "The file with API key not found!"
def getContactDetails():
# Returns phone number as string
try:
with open("credentials.yml", "r") as ymlfile:
fileContent = yaml.load(ymlfile)
mobilePhone = str(fileContent["phone-number"]).strip()
assert len(mobilePhone) == 12, "Invalid phone number"
ymlfile.close()
return mobilePhone
except IOError:
print "The file with contact details not found!"
def isError(responseObj):
# Returns True if there is an error, False otherwise
if responseObj.ok and responseObj.status_code == 200:
return False
elif responseObj.status_code == 401:
return True
else:
return True
def authenticate():
"""
Authenticates credentials are returns JWT token as unicode
Returns None if there is an error
"""
data = getCredentials()
response = requests.post(APIURL + "/login", json=data)
if isError(response):
return None
else:
token = json.loads(response.content)["token"]
return token
def getMyFavorites():
"""
Returns the IDs of my favorite series
Returns None if error
"""
token = authenticate()
authorization = {"Authorization" : "Bearer " + token}
userFav = requests.get(APIURL + "/user/favorites", headers=authorization)
if isError(userFav):
return None
else:
favorites = json.loads(userFav.content)["data"]["favorites"]
return favorites
def seriesInfo(seriesName):
"""
Returns response object for 'seriesName'
Returns None if error
"""
token = authenticate()
authorization = {"Authorization": "Bearer " + token}
series = requests.get(APIURL + "/search/series", headers=authorization, params={"name": seriesName})
if isError(series):
return None
return series
def getEpisodeOverview(seriesId, seasonNum, episodeNum):
"""
Returns the overview of a particular episode of a season
Returns None if error
"""
token = authenticate()
authorization = {"Authorization" : "Bearer " + token}
episodeOverview = requests.get(APIURL + "/series/" + str(seriesId) + "/episodes/query", headers=authorization, params={"id": seriesId, "airedSeason": seasonNum, "airedEpisode" :episodeNum})
if isError(episodeOverview):
return None
return json.loads(episodeOverview.content)["data"][0]["overview"]
def getSeriesNameTime(seriesId):
"""
Returns the name of the series, given its ID
Returns None if error
"""
token = authenticate()
authorization = {"Authorization": "Bearer " + token}
seriesName = requests.get(APIURL + "/series/" + str(seriesId), headers=authorization)
if isError(seriesName):
return None
content = json.loads(seriesName.content)["data"]
return content["seriesName"], content["airsTime"]
def sendSMS(message):
"""
Sends 'message' using the AWS SNS service through boto3 library
Does not return anything
"""
sns_client = boto3.client('sns', 'us-west-2')
mobileNumber = getContactDetails()
response = sns_client.publish(PhoneNumber=mobileNumber, Message=message)