Files
DemonEditor/app/eparser/iptv.py

78 lines
2.8 KiB
Python
Raw Normal View History

2018-03-12 22:47:43 +03:00
""" Module for IPTV and streams support """
2019-04-18 21:43:35 +03:00
import re
2019-01-27 23:59:06 +03:00
import urllib.request
2018-03-12 22:47:43 +03:00
from enum import Enum
2018-02-12 13:34:00 +03:00
from app.properties import Profile
from app.ui.uicommons import IPTV_ICON
2018-01-01 23:42:40 +03:00
from .ecommons import BqServiceType, Service
2017-12-08 18:32:28 +03:00
2018-02-12 13:34:00 +03:00
# url, description, urlkey, account, usrname, psw, s_type, iconsrc, iconsrc_b, group
NEUTRINO_FAV_ID_FORMAT = "{}::{}::{}::{}::{}::{}::{}::{}::{}::{}"
2018-04-10 16:03:36 +03:00
ENIGMA2_FAV_ID_FORMAT = " {}:0:{}:{:X}:{:X}:{:X}:{:X}:0:0:0:{}:{}\n#DESCRIPTION: {}\n"
2018-11-16 23:08:40 +03:00
MARKER_FORMAT = " 1:64:{}:0:0:0:0:0:0:0::{}\n#DESCRIPTION {}\n"
2018-03-12 22:47:43 +03:00
class StreamType(Enum):
DVB_TS = "1"
NONE_TS = "4097"
2019-04-14 00:03:52 +03:00
NONE_REC_1 = "5001"
NONE_REC_2 = "5002"
2017-12-08 18:32:28 +03:00
2018-02-12 13:34:00 +03:00
def parse_m3u(path, profile):
2017-12-08 18:32:28 +03:00
with open(path) as file:
2018-01-31 00:13:42 +03:00
aggr = [None] * 10
2018-11-16 23:08:40 +03:00
services = []
groups = set()
counter = 0
2017-12-08 18:32:28 +03:00
name = None
2018-02-12 13:34:00 +03:00
fav_id = None
2017-12-08 18:32:28 +03:00
for line in file.readlines():
if line.startswith("#EXTINF"):
name = line[1 + line.index(","):].strip()
2018-11-16 23:08:40 +03:00
elif line.startswith("#EXTGRP") and profile is Profile.ENIGMA_2:
grp_name = line.strip("#EXTGRP:").strip()
if grp_name not in groups:
groups.add(grp_name)
fav_id = MARKER_FORMAT.format(counter, grp_name, grp_name)
counter += 1
mr = Service(None, None, None, grp_name, *aggr[0:3], BqServiceType.MARKER.name, *aggr, fav_id, None)
services.append(mr)
2018-11-16 22:35:47 +03:00
elif not line.startswith("#"):
2019-01-27 23:59:06 +03:00
url = line.strip()
2018-02-12 13:34:00 +03:00
if profile is Profile.ENIGMA_2:
2019-01-27 23:59:06 +03:00
url = urllib.request.quote(line.strip())
stream_type = StreamType.NONE_TS.value
fav_id = ENIGMA2_FAV_ID_FORMAT.format(stream_type, 1, 0, 0, 0, 0, url, name, name, None)
2018-02-12 13:34:00 +03:00
elif profile is Profile.NEUTRINO_MP:
2019-01-27 23:59:06 +03:00
fav_id = NEUTRINO_FAV_ID_FORMAT.format(url, "", 0, None, None, None, None, "", "", 1)
if name and url:
srv = Service(None, None, IPTV_ICON, name, *aggr[0:3], BqServiceType.IPTV.name, *aggr, fav_id, None)
services.append(srv)
2017-12-08 18:32:28 +03:00
2018-11-16 23:08:40 +03:00
return services
2017-12-08 18:32:28 +03:00
2019-04-18 21:43:35 +03:00
def export_to_m3u(path, bouquet):
pattern = re.compile(".*:(http.*):.*")
lines = ["#EXTM3U\n"]
for s in bouquet.services:
bq_type = s.type
if bq_type is BqServiceType.IPTV:
res = re.match(pattern, s.data)
if not res:
continue
data = res.group(1)
lines.append("#EXTINF:-1,{}\n{}\n".format(s.name, urllib.request.unquote(data.strip())))
elif bq_type is BqServiceType.MARKER:
pass
with open(path + "{}.m3u".format(bouquet.name), "w", encoding="utf-8") as file:
file.writelines(lines)
2017-12-08 18:32:28 +03:00
if __name__ == "__main__":
pass