Files
DemonEditor/app/eparser/bouquets.py

78 lines
2.4 KiB
Python
Raw Normal View History

2017-10-10 14:13:35 +03:00
""" Module for parsing bouquets """
from collections import namedtuple
2017-10-26 01:23:05 +03:00
_BOUQUETS_PATH = "../data/"
_TV_ROOT_FILE_NAME = "bouquets.tv"
_RADIO_ROOT_FILE_NAME = "bouquets.radio"
2017-10-10 14:13:35 +03:00
Bouquet = namedtuple("Bouquet", ["name", "type", "services"])
2017-10-26 01:23:05 +03:00
Bouquets = namedtuple("Bouquets", ["name", "type", "bouquets"])
2017-10-10 14:13:35 +03:00
def get_bouquets(path):
return parse_bouquets(path, "bouquets.tv", "tv"), parse_bouquets(path, "bouquets.radio", "radio")
2017-10-10 14:13:35 +03:00
2017-10-26 01:23:05 +03:00
def write_bouquets(path, bouquets, bouquets_services):
srv_line = '#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "userbouquet.{}.{}" ORDER BY bouquet\n'
line = []
for bqs in bouquets:
line.clear()
line.append("#NAME {}\n".format(bqs.name))
for bq in bqs.bouquets:
line.append(srv_line.format(bq.name, bq.type))
2017-10-28 12:28:17 +03:00
write_bouquet(path, bq.name, bq.type, bq.services)
2017-10-26 01:23:05 +03:00
with open(path + "bouquets.{}".format(bqs.type), "w") as file:
file.writelines(line)
2017-10-28 12:28:17 +03:00
def write_bouquet(path, name, bq_type, channels):
bouquet = ["#NAME {}\n".format(name)]
2017-10-26 01:23:05 +03:00
for ch in channels:
data_type = int(ch.data_id.split(":")[-2])
if data_type == 22:
data_type = 16
elif data_type == 25:
data_type = 19
bouquet.append("#SERVICE {}:0:{}:{}:0:0:0:\n".format(1, data_type, ch.fav_id))
2017-10-26 01:23:05 +03:00
2017-11-10 15:37:02 +03:00
with open(path + "userbouquet.{}.{}".format(name, bq_type), "w") as file:
file.writelines(bouquet)
2017-10-20 19:03:22 +03:00
def get_bouquet(path, name, bq_type):
""" Parsing services ids from bouquet file """
with open(path + "userbouquet.{}.{}".format(name, bq_type)) as file:
2017-10-11 23:22:30 +03:00
chs_list = file.read()
ids = []
for ch in chs_list.split("#SERVICE")[1:]:
ch_data = ch.strip().split(":")
ids.append("{}:{}:{}:{}".format(ch_data[3], ch_data[4], ch_data[5], ch_data[6]))
2017-10-26 01:23:05 +03:00
2017-10-11 23:22:30 +03:00
return ids
2017-10-10 14:13:35 +03:00
def parse_bouquets(path, bq_name, bq_type):
with open(path + bq_name) as file:
2017-10-10 14:13:35 +03:00
lines = file.readlines()
bouquets = None
nm_sep = "#NAME"
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 = line.split(".")[1]
2017-10-26 01:23:05 +03:00
bouquets[2].append(Bouquet(name=name, type=bq_type, services=get_bouquet(path, name, bq_type)))
2017-10-10 14:13:35 +03:00
return bouquets
if __name__ == "__main__":
pass