TDTChannels/script/utils.py

121 lines
3.9 KiB
Python
Raw Normal View History

2019-06-13 12:10:42 +02:00
import calendar
2019-06-15 00:12:26 +02:00
import re
2019-06-13 12:10:42 +02:00
import time
2019-01-08 15:47:06 +01:00
from channel import Channel
def get_license_info():
return {
"source": "https://github.com/LaQuay/TDTChannels",
2019-06-13 12:10:42 +02:00
"url": "https://github.com/LaQuay/TDTChannels/blob/master/LICENSE"
2019-01-08 15:47:06 +01:00
}
2019-06-13 12:10:42 +02:00
def get_current_timestamp():
return calendar.timegm(time.gmtime())
2019-01-08 15:47:06 +01:00
def stringbetween(text, start, end):
result = re.search('(?<=' + start + ')(.*)(?=' + end + ')', text, re.DOTALL)
return result.group(1)
def stringbetweenparantheses(text):
return text.split("(")[1].split(")")[0]
2019-03-26 00:38:03 +01:00
def get_tv_channels_from_part(text):
2019-02-06 21:38:51 +01:00
line_where_first_channel_starts = 15
attributes_per_item = 6
2019-02-02 17:42:08 +01:00
list_to_iterate = text.split("|")[line_where_first_channel_starts:-1]
2019-01-08 15:47:06 +01:00
while "\n" in list_to_iterate:
list_to_iterate.remove("\n")
2019-02-02 17:42:08 +01:00
channel_list = []
2019-01-08 15:47:06 +01:00
for i in range(0, len(list_to_iterate), attributes_per_item):
item_name = list_to_iterate[i].strip()
item_options = list_to_iterate[i + 1].strip()
item_web = list_to_iterate[i + 2].strip()
if len(item_web) > 0 and item_web[0] != "-":
item_web = stringbetweenparantheses(item_web)
if len(item_web) == 1:
item_web = None
2019-01-08 15:47:06 +01:00
2019-02-06 21:38:51 +01:00
if "HD" in item_name:
item_resolution = "HD"
else:
item_resolution = "SD"
2019-01-08 15:47:06 +01:00
2019-02-06 21:38:51 +01:00
item_logo = list_to_iterate[i + 3].strip()
2019-01-08 15:47:06 +01:00
if len(item_logo) > 0 and item_logo[0] != "-":
item_logo = stringbetweenparantheses(item_logo)
if len(item_logo) == 1:
item_logo = None
2019-01-08 15:47:06 +01:00
2019-02-06 21:38:51 +01:00
item_epg = list_to_iterate[i + 4].strip()
2019-01-08 15:47:06 +01:00
if len(item_epg) == 1:
item_epg = None
2019-01-08 15:47:06 +01:00
2019-02-06 21:38:51 +01:00
item_extra_info = list_to_iterate[i + 5].strip()
2019-02-02 17:42:08 +01:00
if len(item_extra_info) == 1:
2019-06-13 12:10:42 +02:00
item_extra_info = []
else:
item_extra_info = item_extra_info.split(",")
2019-01-08 15:47:06 +01:00
2019-06-14 06:21:59 +02:00
channel = Channel(item_name, item_web, item_resolution, item_logo, item_epg, item_extra_info)
2019-02-02 17:42:08 +01:00
item_options = item_options.split(" - ")
2019-01-08 15:47:06 +01:00
if len(item_options) > 0 and item_options[0] != "-":
for option in item_options:
format = (option[1:5]).replace("]", "")
url = stringbetweenparantheses(option)
2019-06-13 12:10:42 +02:00
if "# GEO" in option:
more_info = ["GEO"]
else:
2019-06-15 00:12:26 +02:00
more_info = []
2019-06-13 12:10:42 +02:00
channel.add_option(format, url, more_info)
2019-01-08 15:47:06 +01:00
channel_list.append(channel)
return channel_list
2019-03-26 00:38:03 +01:00
def get_radio_channels_from_part(text):
line_where_first_channel_starts = 13
attributes_per_item = 5
list_to_iterate = text.split("|")[line_where_first_channel_starts:-1]
while "\n" in list_to_iterate:
list_to_iterate.remove("\n")
channel_list = []
for i in range(0, len(list_to_iterate), attributes_per_item):
item_name = list_to_iterate[i].strip()
item_options = list_to_iterate[i + 1].strip()
item_web = list_to_iterate[i + 2].strip()
if len(item_web) > 0 and item_web[0] != "-":
item_web = stringbetweenparantheses(item_web)
if len(item_web) == 1:
item_web = None
2019-03-26 00:38:03 +01:00
item_logo = list_to_iterate[i + 3].strip()
if len(item_logo) > 0 and item_logo[0] != "-":
item_logo = stringbetweenparantheses(item_logo)
if len(item_logo) == 1:
item_logo = None
2019-03-26 00:38:03 +01:00
item_extra_info = list_to_iterate[i + 4].strip()
if len(item_extra_info) == 1:
2019-06-13 12:10:42 +02:00
item_extra_info = []
else:
item_extra_info = item_extra_info.split(",")
2019-03-26 00:38:03 +01:00
channel = Channel(item_name, item_web, None, item_logo, None, item_extra_info)
2019-03-26 00:38:03 +01:00
item_options = item_options.split(" - ")
if len(item_options) > 0 and item_options[0] != "-":
for option in item_options:
format = (option[1:5]).replace("]", "")
url = stringbetweenparantheses(option)
2019-06-13 12:10:42 +02:00
channel.add_option(format, url, [])
2019-03-26 00:38:03 +01:00
channel_list.append(channel)
return channel_list