-
Notifications
You must be signed in to change notification settings - Fork 8
/
api.py
135 lines (128 loc) · 5.2 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
from xml.etree import ElementTree as et
from BeautifulSoup import BeautifulSoup
import requests
import json
import re
class SimpleTV:
def __init__(self, username, password):
self.remote = None
self.date = '2014%2F1%2F16+1%3A56%3A5'
self.s = requests.Session()
self._login(username, password)
def _login(self, username, password):
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']
# Retrieve streaming urls
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']
self.sid = self.media_server_id = info['data-mediaserverid']
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_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)
# 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