forked from parker1992/iptv-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmltv.py
41 lines (33 loc) · 1.62 KB
/
xmltv.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
from http import HTTPStatus
import parser
import os
import re
import requests
class Xmltv:
def fetch_xmltv(self, xmltv_location: str, host:str, port: int, use_https: bool, output_path: str):
"""
Get the xmltv from either a file location or through a web request.
The xmltv file will be saved as /static/epg.xml so your IPTV player can access it.
"""
if xmltv_location == '':
return
content = str()
url_parser = parser.Parser()
if url_parser.is_url(xmltv_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(xmltv_location, headers=headers, timeout=3.0)
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(xmltv_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 xml file with the proxy endpoints.
content = re.sub(r'(http|https)', rf'http://{host}{port_str}/proxy/data/\1', content, flags=re.M)
if use_https == True:
content = re.sub(rf'(http://{host})', rf'https://{host}', content, flags=re.M)
output.write(content)