forked from parker1992/iptv-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.py
133 lines (108 loc) · 4.07 KB
/
proxy.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
import os
import logging
from config import Config
import parser
import xmltv
from http import HTTPStatus
from threading import Timer
from flask import Flask, Response
from requests import get
from urllib.parse import urlparse
app = Flask(__name__)
config = Config(app.root_path)
m3u_parser = parser.Parser()
xmltv = xmltv.Xmltv()
@app.route('/proxy/stream/<path:path>')
def stream(path):
"""
Returns a stream of data from the proxied stream.
For example a call to, http://0.0.0.0:8080/proxy/stream/http://iptv-provider.com/stream/token
will make a proxied call to http://iptv-provider.com/stream/token.
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
'Accept': '/',
'Connection': 'Keep-Alive'
}
try:
app.logger.info(f'STREAMING: {path}')
stream = get(path, headers=headers, stream=True, allow_redirects=True, timeout=3.0)
app.logger.info(f'STREAMING END: {path}')
except Exception as err:
app.logger.exception('STREAMING: Error streaming {path}', err)
return Response(response='', headers='', status=HTTPStatus.NOT_FOUND)
response = Response(stream.raw, content_type=stream.headers['Content-Type'])
response.call_on_close(lambda: stream.close())
return response
@app.route('/proxy/data/<path:path>')
def data(path):
"""
Used for fetching logo and EPG data through the proxy.
Unlike stream(), this does not return a streamed response.
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
'Accept': '/'
}
app.logger.info(f'BEGIN: Fetching {path}')
try:
response = get(path, headers=headers, timeout=3.0)
app.logger.info(f'END: Fetched {path}')
except Exception as err:
app.logger.exception('END: Error fetching {path}', err)
return Response(response='', headers='', status=HTTPStatus.NOT_FOUND)
return_headers = {
'Content-Type': response.headers['Content-Type'],
'Content-Disposition': f'attachment; filename="{os.path.basename(urlparse(path).path)}"'
}
return Response(response=response.content, headers=return_headers, status=HTTPStatus.OK)
@app.route('/proxy/reload', methods=['GET'])
def reload():
"""
By default, the m3u file will be reloaded every 60 minutes.
This is used to reload the m3u on an ad-hoc basis.
"""
try:
app.logger.info('BEGIN: Force-reloading m3u file')
reload(config)
app.logger.info('END: Force-reloaded m3u file')
return Response(status=HTTPStatus.OK)
except Exception as err:
app.logger.exception('END: Error force-reloading m3u file', err)
return Response(status=HTTPStatus.INTERNAL_SERVER_ERROR)
def reload_timer():
"""
This will reload the m3u and then subsequently reload it every [by default] 60 minutes afterwards.
"""
try:
app.logger.info('BEGIN: Reloading m3u file')
reload(config)
app.logger.info('END: Reloaded m3u file')
except Exception as err:
app.logger.exception('END: Error reloading m3u file', err)
Timer(60 * config.RELOAD_INTERVAL_MIN, lambda: reload_timer()).start()
def reload(config: Config):
m3u_parser.parse_m3u(
config.M3U_LOCATION,
config.M3U_HOST,
config.M3U_PORT,
config.USE_HTTPS,
config.GROUPS_FILTER,
os.path.join(app.static_folder, 'iptv.m3u')
)
xmltv.fetch_xmltv(
config.XMLTV_LOCATION,
config.M3U_HOST,
config.M3U_PORT,
config.USE_HTTPS,
os.path.join(app.static_folder, 'epg.xml')
)
if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
reload_timer()
if __name__ == '__main__':
host = config.M3U_HOST
reload_timer()
app.run(host='0.0.0.0', port=config.LISTEN_PORT)