Files
DemonEditor/app/eparser/enigma/bouquets.py

162 lines
6.6 KiB
Python
Raw Normal View History

2020-04-16 11:55:48 +03:00
""" Module for working with Enigma2 bouquets. """
2018-07-13 12:28:13 +03:00
import re
from collections import Counter
2018-07-13 12:28:13 +03:00
from app.commons import log
from app.eparser.ecommons import BqServiceType, BouquetService, Bouquets, Bouquet, BqType
2017-10-10 14:13:35 +03:00
2017-10-26 01:23:05 +03:00
_TV_ROOT_FILE_NAME = "bouquets.tv"
_RADIO_ROOT_FILE_NAME = "bouquets.radio"
2018-11-08 17:48:51 +03:00
_DEFAULT_BOUQUET_NAME = "favourites"
2017-10-10 14:13:35 +03:00
2017-12-19 22:57:04 +03:00
2017-10-10 14:13:35 +03:00
def get_bouquets(path):
return parse_bouquets(path, "bouquets.tv", BqType.TV.value), parse_bouquets(path, "bouquets.radio",
BqType.RADIO.value)
2017-10-10 14:13:35 +03:00
2020-04-16 11:55:48 +03:00
def write_bouquets(path, bouquets, force_bq_names=False):
""" Creating and writing bouquets files.
If "force_bq_names" then naming the files using the name of the bouquet.
Some images may have problems displaying the favorites list!
"""
2019-06-11 21:03:51 +03:00
srv_line = '#SERVICE 1:7:{}:0:0:0:0:0:0:0:FROM BOUQUET "userbouquet.{}.{}" ORDER BY bouquet\n'
2017-10-26 01:23:05 +03:00
line = []
2019-06-11 21:03:51 +03:00
pattern = re.compile("[^\\w_()]+")
2020-05-23 15:16:31 +03:00
m_index = [0]
s_index = [0]
2017-10-26 01:23:05 +03:00
for bqs in bouquets:
line.clear()
line.append("#NAME {}\n".format(bqs.name))
2020-04-16 11:55:48 +03:00
for index, bq in enumerate(bqs.bouquets):
2018-11-08 17:48:51 +03:00
bq_name = bq.name
if bq_name == "Favourites (TV)" or bq_name == "Favourites (Radio)":
bq_name = _DEFAULT_BOUQUET_NAME
else:
2020-04-16 11:55:48 +03:00
bq_name = re.sub(pattern, "_", bq.name) if force_bq_names else "de{0:02d}".format(index)
2019-06-11 21:03:51 +03:00
line.append(srv_line.format(2 if bq.type == BqType.RADIO.value else 1, bq_name, bq.type))
2020-05-23 15:16:31 +03:00
write_bouquet(path + "userbouquet.{}.{}".format(bq_name, bq.type), bq.name, bq.services, m_index, s_index)
2017-10-26 01:23:05 +03:00
2018-02-10 15:49:44 +03:00
with open(path + "bouquets.{}".format(bqs.type), "w", encoding="utf-8") as file:
2017-10-26 01:23:05 +03:00
file.writelines(line)
2020-05-23 15:16:31 +03:00
def write_bouquet(path, name, services, current_marker, current_space):
bouquet = ["#NAME {}\n".format(name)]
2019-09-04 10:39:46 +03:00
marker = "#SERVICE 1:64:{:X}:0:0:0:0:0:0:0::{}\n"
2020-05-23 15:16:31 +03:00
space = "#SERVICE 1:832:D:{}:0:0:0:0:0:0:\n"
2019-09-04 10:39:46 +03:00
for srv in services:
2020-05-23 15:16:31 +03:00
s_type = srv.service_type
if s_type == BqServiceType.IPTV.name:
2019-09-04 10:39:46 +03:00
bouquet.append("#SERVICE {}\n".format(srv.fav_id.strip()))
2020-05-23 15:16:31 +03:00
elif s_type == BqServiceType.MARKER.name:
2019-09-04 10:39:46 +03:00
m_data = srv.fav_id.strip().split(":")
m_data[2] = current_marker[0]
current_marker[0] += 1
bouquet.append(marker.format(m_data[2], m_data[-1]))
2020-05-23 15:16:31 +03:00
elif s_type == BqServiceType.SPACE.name:
bouquet.append(space.format(current_space[0]))
current_space[0] += 1
2017-12-08 23:48:20 +03:00
else:
2019-09-04 10:39:46 +03:00
data = to_bouquet_id(srv)
if srv.service:
bouquet.append("#SERVICE {}:{}\n#DESCRIPTION {}\n".format(data, srv.service, srv.service))
else:
bouquet.append("#SERVICE {}\n".format(data))
2017-10-26 01:23:05 +03:00
2018-07-13 12:28:13 +03:00
with open(path, "w", encoding="utf-8") as file:
file.writelines(bouquet)
2017-10-20 19:03:22 +03:00
2019-09-04 10:39:46 +03:00
def to_bouquet_id(srv):
2020-04-16 11:55:48 +03:00
""" Creates bouquet channel id. """
2019-09-04 10:39:46 +03:00
data_type = srv.data_id
2018-02-05 22:17:06 +03:00
if data_type and len(data_type) > 4:
2019-09-04 10:39:46 +03:00
data_type = int(srv.data_id.split(":")[4])
2018-01-28 23:10:54 +03:00
2019-09-04 10:39:46 +03:00
return "{}:0:{:X}:{}:0:0:0:".format(1, data_type, srv.fav_id)
2017-11-26 20:40:22 +03:00
2020-05-23 14:23:20 +03:00
def get_bouquet(path, bq_name, bq_type):
2020-04-16 11:55:48 +03:00
""" Parsing services ids from bouquet file. """
2020-05-23 14:23:20 +03:00
with open(path + "userbouquet.{}.{}".format(bq_name, bq_type), encoding="utf-8", errors="replace") as file:
2017-10-11 23:22:30 +03:00
chs_list = file.read()
2017-12-19 22:57:04 +03:00
services = []
2018-02-03 16:45:54 +03:00
srvs = list(filter(None, chs_list.split("\n#SERVICE"))) # filtering ['']
2020-05-23 14:23:20 +03:00
# May come across empty[wrong] files!
if not srvs:
log("Bouquet file 'userbouquet.{}.{}' is empty or wrong!".format(bq_name, bq_type))
return "{} [empty]".format(bq_name), services
bq_name = srvs.pop(0)
for srv in srvs:
2020-05-23 15:16:31 +03:00
srv_data = srv.strip().split(":")
if srv_data[1] == "64":
m_data, sep, desc = srv.partition("#DESCRIPTION")
services.append(BouquetService(desc.strip() if desc else "", BqServiceType.MARKER, srv, srv_data[2]))
elif srv_data[1] == "832":
2020-05-23 14:23:20 +03:00
m_data, sep, desc = srv.partition("#DESCRIPTION")
2020-05-23 15:16:31 +03:00
services.append(BouquetService(desc.strip() if desc else "", BqServiceType.SPACE, srv, srv_data[3]))
2020-05-23 14:23:20 +03:00
elif "http" in srv:
stream_data, sep, desc = srv.partition("#DESCRIPTION")
services.append(BouquetService(desc.lstrip(":").strip() if desc else "", BqServiceType.IPTV, srv, 0))
2017-12-08 18:32:28 +03:00
else:
2020-05-23 15:16:31 +03:00
fav_id = "{}:{}:{}:{}".format(srv_data[3], srv_data[4], srv_data[5], srv_data[6])
name = None
2020-05-23 15:16:31 +03:00
if len(srv_data) == 12:
name, sep, desc = str(srv_data[-1]).partition("\n#DESCRIPTION")
2019-01-28 23:10:35 +03:00
services.append(BouquetService(name, BqServiceType.DEFAULT, fav_id.upper(), 0))
2018-02-05 22:17:06 +03:00
2020-05-23 14:23:20 +03:00
return bq_name.lstrip("#NAME").strip(), services
2017-10-10 14:13:35 +03:00
def parse_bouquets(path, bq_name, bq_type):
with open(path + bq_name, encoding="utf-8", errors="replace") as file:
2017-10-10 14:13:35 +03:00
lines = file.readlines()
bouquets = None
nm_sep = "#NAME"
2019-04-13 15:23:24 +03:00
bq_pattern = re.compile(".*userbouquet\\.+(.*)\\.+[tv|radio].*")
b_names = set()
real_b_names = Counter()
2017-10-26 01:23:05 +03:00
2017-10-10 14:13:35 +03:00
for line in lines:
if nm_sep in line:
_, _, name = line.partition(nm_sep)
2017-10-26 01:23:05 +03:00
bouquets = Bouquets(name.strip(), bq_type, [])
if bouquets and "#SERVICE" in line:
name = re.match(bq_pattern, line)
if name:
b_name = name.group(1)
if b_name in b_names:
2020-05-10 18:30:06 +03:00
log("The list of bouquets contains duplicate [{}] names!".format(b_name))
else:
b_names.add(b_name)
rb_name, services = get_bouquet(path, b_name, bq_type)
if rb_name in real_b_names:
log("Bouquet file 'userbouquet.{}.{}' has duplicate name: {}".format(b_name, bq_type, rb_name))
real_b_names[rb_name] += 1
rb_name = "{} {}".format(rb_name, real_b_names[rb_name])
else:
real_b_names[rb_name] = 0
bouquets[2].append(Bouquet(name=rb_name,
type=bq_type,
services=services,
locked=None,
hidden=None))
else:
2019-04-13 15:23:24 +03:00
raise ValueError("No bouquet name found for: {}".format(line))
2018-02-05 22:17:06 +03:00
2017-10-10 14:13:35 +03:00
return bouquets
if __name__ == "__main__":
pass