forked from esxbr/ustvnow-m3u-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
183 lines (120 loc) · 4.55 KB
/
server.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
import json
import urllib
import urllib2
from urlparse import urlparse, parse_qs
import ustvnow
import SocketServer
import socket
import SimpleHTTPServer
import string,cgi,time
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import base64
port = 8787;
class MyHandler(BaseHTTPRequestHandler):
def do_HEAD(self):
print 'do_HEAD path: ' + self.path;
print 'do_HEAD query: ' + urlparse(self.path).query;
self.send_response(200)
if 'channels.m3u' in self.path:
self.send_header('Content-type', 'application/x-mpegURL')
elif self.path.startswith('/play'):
self.send_header('Content-type', 'application/x-mpegURL')
self.send_header('Accept-Ranges', 'none') # fix kodi bug
elif 'epg.xml' in self.path:
self.send_header('Content-type', 'txt/xml')
elif '/logos' in self.path:
self.send_header('Content-type', 'image/png')
else:
self.send_header('Content-type', 'txt/html')
self.end_headers()
def do_GET(self):
print 'do_GET path: ' + self.path;
print 'do_GET query: ' + urlparse(self.path).query;
args = parse_qs(urlparse(self.path).query);
if 'u' in args and 'p' in args:
username = args['u'][0];
password = args['p'][0];
ustv = ustvnow.Ustvnow(username, password);
elif self.path.startswith('/play'):
args = parse_qs(base64.b64decode(self.path[5:]));
username = args['u'][0];
password = args['p'][0];
ustv = ustvnow.Ustvnow(username, password);
try:
if 'channels.m3u' in self.path:
host = self.headers.get('Host');
EXTM3U = "#EXTM3U\n";
try:
EXTM3U = ustv.get_chan_new();
except Exception as e:
EXTM3U += '#EXTINF:-1, tvg-id="Error" tvg-name="Error" tvg-logo="" group-title="Error", ' + str(e) + '\n';
EXTM3U += 'http://\n\n';
self.send_response(200)
self.send_header('Content-type', 'application/x-mpegURL')
self.send_header('Connection', 'close')
self.send_header('Content-Length', len(EXTM3U))
self.end_headers()
self.wfile.write(EXTM3U.encode('utf-8'))
elif self.path.startswith('/play'):
channel = args['c'][0];
quality = args['q'][0];
icon = args['i'][0];
url = ustv.get_link(channel, int(quality));
print url;
if url != None:
self.send_response(301)
self.send_header('Location', url)
else:
self.send_error(500,'Internal Server Error')
self.end_headers()
elif 'epg.xml' in self.path:
try:
data = ustv.get_guide();
print data
xml = data.toxml(encoding='utf-8');
except Exception as e:
xml = '<?xml version="1.0" encoding="ISO-8859-1"?>'
xml += '<error>' + str(e) + '</error>';
self.send_response(200)
self.send_header('Content-type', 'txt/xml')
self.send_header('Connection', 'close')
self.send_header('Content-Length', len(xml))
self.end_headers()
self.wfile.write(xml)
elif self.path.startswith('/logos'):
print self.path[7:];
self.send_response(301)
self.send_header('Location', self.path[7:])
self.end_headers()
elif 'stop' in self.path:
msg = 'Stopping ...';
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.send_header('Connection', 'close')
self.send_header('Content-Length', len(msg))
self.end_headers()
self.wfile.write(msg.encode('utf-8'))
server.socket.close();
elif 'online' in self.path:
msg = 'Yes. I am.';
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.send_header('Connection', 'close')
self.send_header('Content-Length', len(msg))
self.end_headers()
self.wfile.write(msg.encode('utf-8'))
else:
self.send_error(400,'Bad Request');
except IOError:
self.send_error(500,'Internal Server Error ' + str(IOError))
def startServer():
global port;
try:
server = SocketServer.TCPServer(('', port), MyHandler);
server.serve_forever();
except KeyboardInterrupt:
if server != None:
server.socket.close();
if __name__ == '__main__':
startServer();