TDTChannels/script/my_script.py

169 lines
7.2 KiB
Python
Raw Normal View History

2018-09-22 19:28:28 +02:00
# coding=utf-8
import json
import requests
2018-09-23 11:52:56 +02:00
from ambit import Ambito
2018-09-22 19:28:28 +02:00
from channel import Channel
from country import Country
def substring(text, match):
return text.split(match)
def stringbetween(text, start, end):
text_from_start_to_all = substring(text, start)[1]
text_from_start_to_end = substring(text_from_start_to_all, end)[0]
return text_from_start_to_end
def get_channels_from_part(text):
2018-09-22 22:03:56 +02:00
line_where_first_channel_starts = 15
attributes_per_item = 6
2018-09-22 19:28:28 +02:00
channel_list = []
2018-09-22 22:03:56 +02:00
list_to_iterate = text.split("|")[line_where_first_channel_starts:]
while "\n" in list_to_iterate:
list_to_iterate.remove("\n")
while "\n\n" in list_to_iterate:
list_to_iterate.remove("\n\n")
for i in range(0, len(list_to_iterate), attributes_per_item):
item_name = list_to_iterate[i].strip()
2018-09-23 11:47:54 +02:00
2018-09-22 22:03:56 +02:00
item_options = list_to_iterate[i + 1].strip()
2018-09-23 11:47:54 +02:00
2018-09-22 22:03:56 +02:00
item_web = list_to_iterate[i + 2].strip()
2018-09-23 11:47:54 +02:00
if len(item_web) > 0 and item_web[0] != "-":
item_web = stringbetween(item_web, "(", ")")
2018-09-23 13:19:52 +02:00
if len(item_web) == 1:
item_web = ""
2018-09-23 11:47:54 +02:00
2018-09-22 22:03:56 +02:00
item_resolution = list_to_iterate[i + 3].strip()
2018-09-23 13:19:52 +02:00
if len(item_resolution) == 1:
item_resolution = ""
2018-09-23 11:47:54 +02:00
2018-09-22 22:03:56 +02:00
item_logo = list_to_iterate[i + 4].strip()
2018-09-23 11:48:47 +02:00
if len(item_logo) > 0 and item_logo[0] != "-":
item_logo = stringbetween(item_logo, "(", ")")
2018-09-23 13:19:52 +02:00
if len(item_logo) == 1:
item_logo = ""
2018-09-23 11:47:54 +02:00
2018-09-22 22:03:56 +02:00
item_epg = list_to_iterate[i + 5].strip()
2018-09-23 13:19:52 +02:00
if len(item_epg) == 1:
item_epg = ""
2018-09-22 22:03:56 +02:00
item_options = item_options.split(" - ")
channel = Channel(item_name, item_web, item_resolution, item_logo, item_epg)
if len(item_options) > 0 and item_options[0] != "-":
2018-09-22 19:28:28 +02:00
for option in item_options:
format = (option[1:5]).replace("]", "")
url = stringbetween(option, "(", ")")
channel.add_option(format, url)
channel_list.append(channel)
return channel_list
page = requests.get('https://raw.githubusercontent.com/LaQuay/TDTChannels/master/README.md',
headers={'Cache-Control': 'no-cache'})
content = str(page.text)
spain = Country("Spain")
2018-11-04 21:59:53 +01:00
international = Country("International")
2018-09-22 19:28:28 +02:00
canales_nacionales = stringbetween(content, "### Nacionales", "### Informativos")
spain.add_ambit(Ambito("Generalistas", get_channels_from_part(canales_nacionales)))
canales_informativos = stringbetween(content, "### Informativos", "### Deportivos")
spain.add_ambit(Ambito("Informativos", get_channels_from_part(canales_informativos)))
canales_deportivos = stringbetween(content, "### Deportivos", "### Infantiles")
spain.add_ambit(Ambito("Deportivos", get_channels_from_part(canales_deportivos)))
canales_infantiles = stringbetween(content, "### Infantiles", "### Musicales")
spain.add_ambit(Ambito("Infantiles", get_channels_from_part(canales_infantiles)))
canales_musicales = stringbetween(content, "### Musicales", "### Autonómicos")
spain.add_ambit(Ambito("Musicales", get_channels_from_part(canales_musicales)))
canales_autonomicos_andalucia = stringbetween(content, "#### Andalucía", "#### Aragón")
spain.add_ambit(Ambito("Andalucía", get_channels_from_part(canales_autonomicos_andalucia)))
canales_autonomicos_aragon = stringbetween(content, "#### Aragón", "#### Asturias")
spain.add_ambit(Ambito("Aragón", get_channels_from_part(canales_autonomicos_aragon)))
canales_autonomicos_asturias = stringbetween(content, "#### Asturias", "#### Canarias")
spain.add_ambit(Ambito("Asturias", get_channels_from_part(canales_autonomicos_asturias)))
canales_autonomicos_canarias = stringbetween(content, "#### Canarias", "#### Cantabria")
spain.add_ambit(Ambito("Canarias", get_channels_from_part(canales_autonomicos_canarias)))
2018-11-24 22:04:22 +01:00
canales_autonomicos_cantabria = stringbetween(content, "#### Cantabria", "#### Castilla La-Mancha")
2018-09-22 19:28:28 +02:00
spain.add_ambit(Ambito("Cantabria", get_channels_from_part(canales_autonomicos_cantabria)))
2018-11-24 22:04:22 +01:00
canales_autonomicos_castilla_mancha = stringbetween(content, "#### Castilla La-Mancha", "#### Castilla y León")
2018-09-22 19:28:28 +02:00
spain.add_ambit(Ambito("Castilla la Mancha", get_channels_from_part(canales_autonomicos_castilla_mancha)))
canales_autonomicos_castilla_leon = stringbetween(content, "#### Castilla y León", "#### Catalunya")
spain.add_ambit(Ambito("Castilla y León", get_channels_from_part(canales_autonomicos_castilla_leon)))
canales_autonomicos_catalunya = stringbetween(content, "#### Catalunya", "#### Ceuta")
spain.add_ambit(Ambito("Catalunya", get_channels_from_part(canales_autonomicos_catalunya)))
canales_autonomicos_ceuta = stringbetween(content, "#### Ceuta", "#### Extremadura")
spain.add_ambit(Ambito("Ceuta", get_channels_from_part(canales_autonomicos_ceuta)))
canales_autonomicos_extremadura = stringbetween(content, "#### Extremadura", "#### Galicia")
spain.add_ambit(Ambito("Extremadura", get_channels_from_part(canales_autonomicos_extremadura)))
canales_autonomicos_galicia = stringbetween(content, "#### Galicia", "#### Islas Baleares")
spain.add_ambit(Ambito("Galicia", get_channels_from_part(canales_autonomicos_galicia)))
canales_autonomicos_islas_baleares = stringbetween(content, "### Islas Baleares", "#### Madrid")
spain.add_ambit(Ambito("Islas Baleares", get_channels_from_part(canales_autonomicos_islas_baleares)))
canales_autonomicos_madrid = stringbetween(content, "#### Madrid", "#### Melilla")
spain.add_ambit(Ambito("Madrid", get_channels_from_part(canales_autonomicos_madrid)))
canales_autonomicos_melilla = stringbetween(content, "#### Melilla", "#### Murcia")
spain.add_ambit(Ambito("Melilla", get_channels_from_part(canales_autonomicos_melilla)))
canales_autonomicos_murcia = stringbetween(content, "#### Murcia", "#### Navarra")
spain.add_ambit(Ambito("Murcia", get_channels_from_part(canales_autonomicos_murcia)))
canales_autonomicos_navarra = stringbetween(content, "#### Navarra", "#### Pais Vasco")
spain.add_ambit(Ambito("Navarra", get_channels_from_part(canales_autonomicos_navarra)))
canales_autonomicos_pais_vasco = stringbetween(content, "#### Pais Vasco", "#### La Rioja")
spain.add_ambit(Ambito("Pais Vasco", get_channels_from_part(canales_autonomicos_pais_vasco)))
canales_autonomicos_la_rioja = stringbetween(content, "#### La Rioja", "#### Valencia")
spain.add_ambit(Ambito("La Rioja", get_channels_from_part(canales_autonomicos_la_rioja)))
canales_autonomicos_valencia = stringbetween(content, "#### Valencia", "## Internacionales")
spain.add_ambit(Ambito("Valencia", get_channels_from_part(canales_autonomicos_valencia)))
2018-11-04 21:59:53 +01:00
canales_internacionales = stringbetween(content, "## Internacionales", "### Andorra")
international.add_ambit(Ambito("Internacional", get_channels_from_part(canales_internacionales)))
2018-09-22 19:28:28 +02:00
# Save data to JSON file
2018-09-23 13:44:01 +02:00
json_file = open('./public/output/channels.json', "w+")
2018-09-22 19:28:28 +02:00
# TODO Anadir copyright
2018-11-04 21:59:53 +01:00
json_file.write("[")
2018-09-22 19:28:28 +02:00
json_file.write(json.dumps(spain.to_json()))
2018-11-04 21:59:53 +01:00
json_file.write(", ")
json_file.write(json.dumps(international.to_json()))
json_file.write("]")
2018-09-22 19:28:28 +02:00
json_file.close()
# Save data to M3U8 file
2018-09-23 13:44:01 +02:00
text_file = open('./public/output/channels.m3u8', "w+")
2018-09-22 19:28:28 +02:00
text_file.write("#EXTM3U" + "\n")
text_file.write("# @LaQuay https://github.com/LaQuay/TDTChannels" + "\n")
text_file.write(spain.to_m3u8())
2018-11-04 21:59:53 +01:00
text_file.write(international.to_m3u8())
2018-09-22 19:28:28 +02:00
text_file.close()
print("JSON + M3U8 Updated")