From 2f7dd65d9e84dc559b87179e9312500517f305d2 Mon Sep 17 00:00:00 2001
From: Marc <marcvila10@hotmail.com>
Date: Sat, 22 Sep 2018 22:03:56 +0200
Subject: [PATCH] Update to add EPG info in script

---
 script/ambito.py    |  2 +-
 script/channel.py   | 30 +++++++++++++++++++++++++++---
 script/my_script.py | 29 +++++++++++++++++++----------
 3 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/script/ambito.py b/script/ambito.py
index ccee9a3b..6eb392dd 100644
--- a/script/ambito.py
+++ b/script/ambito.py
@@ -23,7 +23,7 @@ class Ambito:
         for channel in self.channels:
             for option in channel.get_options():
                 if option.is_m3u8_valid():
-                    channels_list += "#EXTINF:-1, " + channel.get_name() + "\n" + option.get_url() + "\n"
+                    channels_list += channel.to_m3u8(option)
         return channels_list
 
     def to_m3u8(self):
diff --git a/script/channel.py b/script/channel.py
index c9c2ff29..f5bab2d3 100644
--- a/script/channel.py
+++ b/script/channel.py
@@ -1,9 +1,17 @@
 class Channel:
     name = None
+    web = None
+    resolution = None
+    logo = None
+    epg_id = None
     options = []
 
-    def __init__(self, name):
+    def __init__(self, name, web, resolution, logo, epg_id):
         self.name = name
+        self.web = web
+        self.resolution = resolution
+        self.logo = logo
+        self.epg_id = epg_id
         self.options = []
 
     def add_option(self, format, url):
@@ -12,6 +20,15 @@
     def get_name(self):
         return self.name
 
+    def get_resolution(self):
+        return self.get_resolution
+
+    def get_logo(self):
+        return self.logo
+
+    def get_epg(self):
+        return self.epg_id
+
     def get_options(self):
         return self.options
 
@@ -19,7 +36,7 @@
         options_string = ""
         for option in self.options:
             options_string += "[Format: " + option.get_format() + ", URL: " + option.get_url() + "]"
-        return (self.name + " " + options_string)
+        return self.name + " " + options_string
 
     def __options_to_json__(self):
         options_list = []
@@ -30,9 +47,16 @@
     def to_json(self):
         return {
             "name": self.name,
+            "resolution": self.resolution,
+            "logo": self.logo,
+            "epg_id": self.epg_id,
             "options": self.__options_to_json__()
         }
 
+    def to_m3u8(self, option):
+        return ('#EXTINF:-1 tvg-id="' + self.epg_id + '" tvg-logo="' + self.logo + '", ' + self.name + "\n" +
+                option.get_url() + "\n")
+
     class Web:
         format = None
         url = None
@@ -51,7 +75,7 @@
             return self.url
 
         def __str__(self):
-            return (self.format + ", " + self.url)
+            return self.format + ", " + self.url
 
         def to_json(self):
             return {
diff --git a/script/my_script.py b/script/my_script.py
index 7768ae3b..bd357298 100644
--- a/script/my_script.py
+++ b/script/my_script.py
@@ -20,17 +20,26 @@ def stringbetween(text, start, end):
 
 
 def get_channels_from_part(text):
+    line_where_first_channel_starts = 15
+    attributes_per_item = 6
     channel_list = []
-    list_to_iterate = text.split("| ")[4:]
-    for i in range(0, len(list_to_iterate), 2):
-        item_name = list_to_iterate[i]
-        item_options = list_to_iterate[i + 1]
+    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()
+        item_options = list_to_iterate[i + 1].strip()
+        item_web = list_to_iterate[i + 2].strip()
+        item_resolution = list_to_iterate[i + 3].strip()
+        item_logo = list_to_iterate[i + 4].strip()
+        item_epg = list_to_iterate[i + 5].strip()
 
-        item_name = item_name.replace("|", "").replace("\n", "").strip()
-        item_options = (item_options.replace("|", "").replace("\n", "")).split(" - ")
+        item_options = item_options.split(" - ")
 
-        channel = Channel(item_name)
-        if len(item_options) > 0 and item_options[0] != "":
+        channel = Channel(item_name, item_web, item_resolution, item_logo, item_epg)
+        if len(item_options) > 0 and item_options[0] != "-":
             for option in item_options:
                 format = (option[1:5]).replace("]", "")
                 url = stringbetween(option, "(", ")")
@@ -118,13 +127,13 @@ canales_autonomicos_valencia = stringbetween(content, "#### Valencia", "## Inter
 spain.add_ambit(Ambito("Valencia", get_channels_from_part(canales_autonomicos_valencia)))
 
 # Save data to JSON file
-json_file = open('code/output/channels.json', "w+")
+json_file = open('./output/channels.json', "w+")
 # TODO Anadir copyright
 json_file.write(json.dumps(spain.to_json()))
 json_file.close()
 
 # Save data to M3U8 file	
-text_file = open('code/output/channels.m3u8', "w+")
+text_file = open('./output/channels.m3u8', "w+")
 text_file.write("#EXTM3U" + "\n")
 text_file.write("# @LaQuay https://github.com/LaQuay/TDTChannels" + "\n")
 text_file.write(spain.to_m3u8())