2017-10-10 14:13:35 +03:00
|
|
|
""" Module for parsing bouquets """
|
|
|
|
|
from collections import namedtuple
|
2017-12-19 22:57:04 +03:00
|
|
|
from enum import Enum
|
2017-10-10 14:13:35 +03:00
|
|
|
|
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
|
|
|
|
2017-12-19 22:57:04 +03:00
|
|
|
|
|
|
|
|
class BqServiceType(Enum):
|
|
|
|
|
DEFAULT = "DEFAULT"
|
|
|
|
|
IPTV = "IPTV"
|
|
|
|
|
MARKER = "MARKER" # 64
|
|
|
|
|
|
|
|
|
|
|
2017-10-24 00:09:11 +03:00
|
|
|
Bouquet = namedtuple("Bouquet", ["name", "type", "services"])
|
2017-10-26 01:23:05 +03:00
|
|
|
Bouquets = namedtuple("Bouquets", ["name", "type", "bouquets"])
|
2017-12-20 10:54:45 +03:00
|
|
|
BouquetService = namedtuple("BouquetService", ["name", "type", "data", "num"])
|
2017-10-10 14:13:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_bouquets(path):
|
2017-10-24 00:09:11 +03:00
|
|
|
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):
|
2017-10-24 00:09:11 +03:00
|
|
|
bouquet = ["#NAME {}\n".format(name)]
|
2017-10-26 01:23:05 +03:00
|
|
|
|
2017-10-24 00:09:11 +03:00
|
|
|
for ch in channels:
|
2017-11-25 15:55:24 +03:00
|
|
|
if not ch: # if was duplicate
|
|
|
|
|
continue
|
2017-12-19 22:57:04 +03:00
|
|
|
|
|
|
|
|
if ch.service_type == BqServiceType.IPTV.name or ch.service_type == BqServiceType.MARKER.name:
|
2017-12-20 10:54:45 +03:00
|
|
|
bouquet.append("#SERVICE {}\n".format(ch.fav_id.strip()))
|
2017-12-08 23:48:20 +03:00
|
|
|
else:
|
|
|
|
|
bouquet.append("#SERVICE {}\n".format(to_bouquet_id(ch)))
|
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:
|
2017-10-24 00:09:11 +03:00
|
|
|
file.writelines(bouquet)
|
2017-10-20 19:03:22 +03:00
|
|
|
|
|
|
|
|
|
2017-11-26 20:40:22 +03:00
|
|
|
def to_bouquet_id(ch):
|
|
|
|
|
""" Creates bouquet channel id """
|
2017-12-16 10:29:51 +03:00
|
|
|
data_type = ch.data_id
|
|
|
|
|
if data_type:
|
|
|
|
|
data_type = int(ch.data_id.split(":")[-2])
|
|
|
|
|
if data_type == 22:
|
|
|
|
|
data_type = 16
|
|
|
|
|
elif data_type == 25:
|
|
|
|
|
data_type = 19
|
2017-11-26 20:40:22 +03:00
|
|
|
service = "{}:0:{}:{}:0:0:0:".format(1, data_type, ch.fav_id)
|
|
|
|
|
|
|
|
|
|
return service
|
|
|
|
|
|
|
|
|
|
|
2017-10-24 00:09:11 +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()
|
2017-12-19 22:57:04 +03:00
|
|
|
services = []
|
2017-12-08 18:32:28 +03:00
|
|
|
for ch in list(filter(lambda x: len(x) > 1, chs_list.split("#SERVICE")[1:])): # filtering ['']
|
2017-12-16 19:28:57 +03:00
|
|
|
ch_data = ch.strip().split(":")
|
|
|
|
|
if ch_data[1] == "64":
|
2017-12-20 10:54:45 +03:00
|
|
|
services.append(BouquetService(ch_data[-1].split("\n")[0], BqServiceType.MARKER, ch, ch_data[2]))
|
2017-12-16 19:28:57 +03:00
|
|
|
elif "http" in ch:
|
2017-12-20 10:54:45 +03:00
|
|
|
services.append(BouquetService(ch_data[-1].split("\n")[0], BqServiceType.IPTV, ch, 0))
|
2017-12-08 18:32:28 +03:00
|
|
|
else:
|
2017-12-19 22:57:04 +03:00
|
|
|
services.append(BouquetService(None, BqServiceType.DEFAULT,
|
2017-12-20 10:54:45 +03:00
|
|
|
"{}:{}:{}:{}".format(ch_data[3], ch_data[4], ch_data[5], ch_data[6]), 0))
|
2017-10-26 01:23:05 +03:00
|
|
|
|
2017-12-19 22:57:04 +03:00
|
|
|
return services
|
2017-10-10 14:13:35 +03:00
|
|
|
|
|
|
|
|
|
2017-10-24 00:09:11 +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, [])
|
2017-10-24 00:09:11 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2017-10-07 17:33:40 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
pass
|