forked from tdickman/simple-tv-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
184 lines (167 loc) · 6.88 KB
/
api.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
from xml.etree import ElementTree as et
from BeautifulSoup import BeautifulSoup
import requests
import json
import re
class SimpleTV:
def __init__(self, username, password, dvr):
self.remote = None
self.date = '2014%2F1%2F16+1%3A56%3A5'
self.s = requests.Session()
self._login(username, password, dvr)
def _login(self, username, password, dvr):
url = 'https://us.simple.tv/Auth/SignIn'
data = {
'UserName': username,
'Password': password,
'RememberMe': 'true'
}
r = self.s.post(url, params=data)
resp = json.loads(r.text)
if 'SignInError' in resp:
print "Error logging in"
raise('Invalid login information')
# self.sid = resp['MediaServerID']
r = self.s.get('https://us-my.simple.tv/')
soup = BeautifulSoup(r.text)
info = soup.find('section', {'id': 'watchShow'})
self.account_id = info['data-accountid']
# Get list of DVRs, compare against selected, default to first if
# none found, or none provided
dvr_list, dvr_id_list = self.get_dvr_list(soup)
self.sid = self.media_server_id = info['data-mediaserverid']
if dvr_list:
#find the dvr_unit selected
dvr_found = False
for x in range(0,len(dvr_list)):
if dvr_list[x].text == dvr:
self.sid = self.media_server_id = dvr_id_list[x]['data-value']
print "Selecting DVR with DeviceID: " + self.sid
dvr_found=True
if not dvr_found:
print "Can't find selected DVR, falling back to DeviceID " + self.sid
self.sid = self.media_server_id = info['data-mediaserverid']
#set the mediserver ID to correct STV
self.set_dvr()
#Retrieve streaming urls
r = self.s.get("https://us-my.simple.tv/Data/RealTimeData"
"?accountId={}&mediaServerId={}"
"&playerAlternativeAvailable=false".format(self.account_id, self.media_server_id))
resp = json.loads(r.text)
self.local_base = resp['LocalStreamBaseURL']
self.remote_base = resp['RemoteStreamBaseURL']
return True
def get_dvr_list(self, page):
dvr_list = []
dvr_id_list = []
uls = page.findAll('ul',{'class':'switch-dvr-list'})
if uls:
for ul in uls:
for dvrli in ul.findAll('li'):
dvr_list.append(dvrli)
for dvr_id in dvrli.findAll('a'):
dvr_id_list.append(dvr_id)
for x in range(0, len(dvr_list)):
print "Found: " + dvr_list[x].text.encode("utf-8") + "| DeviceID: " + dvr_id_list[x]['data-value']
return (dvr_list, dvr_id_list)
def set_dvr(self):
#set the mediaserver
url="https://us-my.simple.tv/Account/MediaServers"
data = {
'defaultMediaServerID': self.sid
}
r = self.s.post(url, params=data)
return
def get_shows(self):
url = 'https://us-my.simple.tv/Library/MyShows'
url += '?browserDateTimeUTC=' + self.date
url += '&mediaServerID=' + self.sid
url += '&browserUTCOffsetMinutes=-300'
r = self.s.get(url)
root = et.fromstring(r.text)
shows = []
for show in root:
data = {}
div = show.find('div')
info = show.find('figcaption')
data['group_id'] = show.attrib['data-groupid']
data['image'] = div.find('img').attrib['src']
data['name'] = info.find('b').text
data['recordings'] = info.find('span').text
shows.append(data)
return shows
def get_episodes(self, group_id):
url = 'https://us-my.simple.tv/Library/ShowDetail'
url += '?browserDateTimeUTC=' + self.date
url += '&browserUTCOffsetMinutes=-300'
url += '&groupID=' + group_id
r = self.s.get(url)
soup = BeautifulSoup(r.text)
e = soup.find('div', {'id': 'recorded'}).findAll('article')
episodes = []
for episode in e:
data = {}
# Skip failed episodes for now
try:
epiList = episode.findAll('b')
if len(epiList) == 3: # Figure out if it's a Show or a Movie
data['season'] = int(epiList[1].text)
data['episode'] = int(epiList[2].text)
else:
data['season'] = 0
data['episode'] = 0
data['channel'] = str(epiList[0].text)
links = episode.find('a', {'class': 'button-standard-watch'})
data['item_id'] = links['data-itemid']
data['instance_id'] = links['data-instanceid']
data['title'] = episode.h3.find(
text=True,
recursive=False
).rstrip()
except:
continue
episodes.append(data)
return episodes
def _get_stream_urls(self, group_id, instance_id, item_id):
url = 'https://us-my.simple.tv/Library/Player'
url += '?browserUTCOffsetMinutes=-300'
url += '&groupID=' + group_id
url += '&instanceID=' + instance_id
url += '&itemID=' + item_id
url += '&isReachedLocally=' + ("False" if self.remote else "True")
r = self.s.get(url)
soup = BeautifulSoup(r.text)
s = soup.find('div', {'id': 'video-player-large'})
if self.remote:
base = self.remote_base
else:
base = self.local_base
req_url = base + s['data-streamlocation']
stream_base = "/".join(req_url.split('/')[:-1]) + "/"
# Get urls for different qualities
# First time through, autodetect if remote
if self.remote is None:
try:
r = self.s.get(req_url, timeout=5)
self.remote = False
except:
self.remote = True
return self._get_stream_urls(group_id, instance_id, item_id)
r = self.s.get(req_url)
urls = []
for url in r.text.split('\n'):
if url[-3:] == "3u8":
urls.append(url)
return {'base': stream_base, 'urls': urls}
def retrieve_episode_mp4(self, group_id, instance_id, item_id, quality):
'''Specify quality using int for entry into m3u8. Typically:
0 = 500000, 1 = 1500000, 2 = 4500000
'''
s_info = self._get_stream_urls(group_id, instance_id, item_id)
if not s_info['urls']:
return
# Modify url for h264 mp4 :)
url_m3u8 = s_info['base'] + s_info['urls'][int(quality)]
m = re.match(".*hls-(?P<number>\d)\Wm3u8", url_m3u8)
url = re.sub('hls-\d.m3u8', "10" + m.group("number"), url_m3u8)
return url