-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.py
45 lines (37 loc) · 2.06 KB
/
parser.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
from http import HTTPStatus
import os
import re
import requests
from urllib.parse import urlparse
class Parser:
def is_url(self, url):
url_parsed = urlparse(url)
return url_parsed.scheme in ('http', 'https')
def parse_m3u(self, m3u_location: str, host: str, port: int, use_https: bool, output_path: str):
"""
Get the m3u from either a file location or through a web request.
After the m3u is retrieved, this will prefix all the URLs to go through the /proxy/stream endpoint.
The m3u file will be saved as /static/iptv.m3u so your IPTV player can access it.
"""
content = str()
if self.is_url(m3u_location):
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'}
response = requests.get(m3u_location, headers=headers)
if response.status_code != HTTPStatus.OK:
raise Exception(response.text)
os.makedirs(os.path.dirname(output_path), exist_ok=True)
content = response.text
else:
with open(m3u_location, 'r') as input:
os.makedirs(os.path.dirname(output_path), exist_ok=True)
content = input.read()
with open(output_path, 'w') as output:
port_str = f':{str(port)}' if port != 0 else ''
# Prefix all URLs in the m3u file with the proxy endpoints.
# Logos and EPG data should go to /proxy/data and the streams should go to /proxy/stream.
content = re.sub(r'(EXTM3U.*url-tvg=")(http)', rf'\1http://{host}{port_str}/proxy/data/\2', content, flags=re.M)
content = re.sub(r'(EXTINF.*tvg-logo=")(http)', rf'\1http://{host}{port_str}/proxy/data/\2', content, flags=re.M)
content = re.sub(r'^(http)', rf'http://{host}{port_str}/proxy/stream/\1', content, flags=re.M)
if use_https:
content = re.sub(rf'(http://{host})',rf'https://{host}', content, flags=re.M)
output.write(content)