forked from esxbr/ustvnow-m3u-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ustvnow.py
213 lines (161 loc) · 5.9 KB
/
ustvnow.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'''
ustvnow
'''
import cookielib
import os
import re
import urllib, urllib2
import json
from xml.dom import minidom
from time import time
from datetime import datetime, timedelta
class Ustvnow:
__BASE_URL = 'http://m.ustvnow.com'
def __init__(self, user, password):
self.user = user
self.password = password
def get_channels(self, dologin=True):
if dologin:
self._login()
content = self._get_json('gtv/1/live/listchannels', {'token': self.token})
channels = []
#print json.dumps(content);
results = content['results']['streamnames'];
for i in results:
channels.append({
'name': i['sname'],
'sname' : i['callsign'],
'icon': self.__BASE_URL + '/' + i['img']
})
return channels
def get_guide(self):
self._login()
content = self._get_json('gtv/1/live/channelguide', {'token': self.token})
results = content['results'];
now = time();
doc = minidom.Document();
base = doc.createElement('tv');
base.setAttribute("cache-version", str(now));
base.setAttribute("cache-time", str(now));
base.setAttribute("generator-info-name", "IPTV Plugin");
base.setAttribute("generator-info-url", "http://www.xmltv.org/");
doc.appendChild(base)
channels = self.get_channels(dologin=False);
for channel in channels:
name = channel['name'];
id = channel['sname'];
c_entry = doc.createElement('channel');
c_entry.setAttribute("id", id);
base.appendChild(c_entry)
dn_entry = doc.createElement('display-name');
dn_entry_content = doc.createTextNode(name);
dn_entry.appendChild(dn_entry_content);
c_entry.appendChild(dn_entry);
dn_entry = doc.createElement('display-name');
dn_entry_content = doc.createTextNode(id);
dn_entry.appendChild(dn_entry_content);
c_entry.appendChild(dn_entry);
icon_entry = doc.createElement('icon');
icon_entry.setAttribute("src", channel['icon']);
c_entry.appendChild(icon_entry);
for programme in results:
start_time = datetime.fromtimestamp(float(programme['ut_start']));
stop_time = start_time + timedelta(seconds=int(programme['guideremainingtime']));
pg_entry = doc.createElement('programme');
pg_entry.setAttribute("start", start_time.strftime('%Y%m%d%H%M%S 0'));
pg_entry.setAttribute("stop", stop_time.strftime('%Y%m%d%H%M%S 0'));
pg_entry.setAttribute("channel", programme['callsign']);
base.appendChild(pg_entry);
t_entry = doc.createElement('title');
t_entry.setAttribute("lang", "en");
t_entry_content = doc.createTextNode(programme['title']);
t_entry.appendChild(t_entry_content);
pg_entry.appendChild(t_entry);
st_entry = doc.createElement('sub-title');
st_entry.setAttribute("lang", "en");
st_entry_content = doc.createTextNode(programme['episode_title']);
st_entry.appendChild(st_entry_content);
pg_entry.appendChild(st_entry);
d_entry = doc.createElement('desc');
d_entry.setAttribute("lang", "en");
d_entry_content = doc.createTextNode(programme['synopsis']);
d_entry.appendChild(d_entry_content);
pg_entry.appendChild(d_entry);
dt_entry = doc.createElement('date');
dt_entry_content = doc.createTextNode(start_time.strftime('%Y%m%d'));
dt_entry.appendChild(dt_entry_content);
pg_entry.appendChild(dt_entry);
c_entry = doc.createElement('category');
c_entry_content = doc.createTextNode(programme['xcdrappname']);
c_entry.appendChild(c_entry_content);
pg_entry.appendChild(c_entry);
en_entry = doc.createElement('episode-num');
en_entry.setAttribute('system', 'dd_progid');
en_entry_content = doc.createTextNode(programme['content_id']);
en_entry.appendChild(en_entry_content);
pg_entry.appendChild(en_entry);
i_entry = doc.createElement('icon');
i_entry.setAttribute("src", self.__BASE_URL + '/' + programme['img']);
pg_entry.appendChild(i_entry);
return doc
def _build_url(self, path, queries={}):
if queries:
query = urllib.urlencode(queries)
return '%s/%s?%s' % (self.__BASE_URL, path, query)
else:
return '%s/%s' % (self.__BASE_URL, path)
def _fetch(self, url, form_data=False):
if form_data:
req = urllib2.Request(url, form_data)
else:
req = url
try:
response = urllib2.urlopen(url)
return response
except urllib2.URLError, e:
return False
def _get_json(self, path, queries={}):
content = False
url = self._build_url(path, queries)
response = self._fetch(url)
if response:
content = json.loads(response.read())
else:
content = False
return content
def _get_html(self, path, queries={}):
html = False
url = self._build_url(path, queries)
response = self._fetch(url)
if response:
html = response.read()
else:
html = False
return html
#TODO this func
def get_link(self, sname, quality=1, stream_type='rtmp'):
self._login()
self.__BASE_URL = 'http://lv2.ustvnow.com';
html = self._get_html('iphone_ajax', {'tab': 'iphone_playingnow',
'token': self.token})
channel = re.search('class="panel".+?images\/' + sname + '.+?class="nowplaying_itemdesc".+?<.+?href="rtmp(.+?)">View<\/a>', html, re.DOTALL);
if channel == None:
return None;
url = channel.group(1);
url = '%s%s%d' % (stream_type, url[:-1], quality)
return url
def _login(self):
self.token = None
self.cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(opener)
url = self._build_url('gtv/1/live/login', {'username': self.user,
'password': self.password,
'device':'gtv',
'redir':'0'})
response = self._fetch(url)
#response = opener.open(url)
for cookie in self.cj:
print '%s: %s' % (cookie.name, cookie.value)
if cookie.name == 'token':
self.token = cookie.value