From 1a150636d2154f81a6bc776b1217f9b1cfb44e35 Mon Sep 17 00:00:00 2001
From: Marc <marcvila10@hotmail.com>
Date: Sat, 26 Jan 2019 15:40:27 +0100
Subject: [PATCH] support to enigma2

---
 script/ambit.py     | 13 +++++++++++++
 script/channel.py   | 29 ++++++++++++++++++++---------
 script/country.py   |  9 +++++++++
 script/my_script.py | 10 +++++++++-
 4 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/script/ambit.py b/script/ambit.py
index 395e160c..8756d88d 100644
--- a/script/ambit.py
+++ b/script/ambit.py
@@ -30,5 +30,18 @@ class Ambito:
     def to_m3u8(self):
         return self.__channels_to_m3u8__()
 
+    def __channels_to_enigma2__(self):
+        channels_list = ""
+        counter = 3
+        for channel in self.channels:
+            for option in channel.get_options():
+                if option.is_m3u8_valid():
+                    channels_list += channel.to_enigma2(option, counter)
+                    counter += 2
+        return channels_list
+
+    def to_enigma2(self):
+        return self.__channels_to_enigma2__()
+
     def __str__(self):
         return self.name
diff --git a/script/channel.py b/script/channel.py
index 92b90527..e734f692 100644
--- a/script/channel.py
+++ b/script/channel.py
@@ -28,7 +28,7 @@
     def __str__(self):
         options_string = ""
         for option in self.options:
-            options_string += "[Format: " + option.get_format() + ", URL: " + option.get_url() + "]"
+            options_string += f"[Format: {option.get_format()}, URL: {option.get_url()}]"
         return self.name + " " + options_string
 
     def __options_to_json__(self):
@@ -47,20 +47,28 @@
             "options": self.__options_to_json__()
         }
 
-    def to_m3u8(self, ambit, option):
+    def to_m3u8(self, ambit_name, option):
         info = '#EXTINF:-1'
         if self.epg_id != "":
-            info += ' tvg-id="' + self.epg_id + '"'
+            info += f' tvg-id="{self.epg_id}"'
         if self.logo != "":
-            info += ' tvg-logo="' + self.logo + '"'
-        if ambit != "":
-            info += ' group-title="' + ambit + '"'
+            info += f' tvg-logo="{self.logo}"'
+        if ambit_name != "":
+            info += f' group-title="{ambit_name}"'
 
         info += ', ' + self.name
         info += '\n' + option.get_url() + '\n'
 
         return info
 
+    def to_enigma2(self, option, counter):
+        info = f'#SERVICE 4097:0:1:{counter}:0:0:0:0:0:0'
+        info += f':{option.get_url(double_dot=False)}'
+        info += f':{self.name}\n'
+        info += f'#DESCRIPTION {self.name}\n'
+
+        return info
+
     class Web:
         def __init__(self, format, url):
             self.format = format
@@ -72,8 +80,11 @@
         def get_format(self):
             return self.format
 
-        def get_url(self):
-            return self.url
+        def get_url(self, double_dot=True):
+            if double_dot:
+                return self.url
+            else:
+                return self.url.replace(":", "%3a")
 
         def __str__(self):
             return self.format + ", " + self.url
@@ -82,4 +93,4 @@
             return {
                 "format": self.format,
                 "url": self.url
-            }
\ No newline at end of file
+            }
diff --git a/script/country.py b/script/country.py
index fd35617d..4f127927 100644
--- a/script/country.py
+++ b/script/country.py
@@ -32,3 +32,12 @@ class Country:
 
     def to_m3u8(self):
         return self.__ambits_to_m3u8__()
+
+    def __ambits_to_enigma2__(self):
+        ambits_list = ""
+        for ambit in self.ambits:
+            ambits_list += ambit.to_enigma2()
+        return ambits_list
+
+    def to_enigma2(self):
+        return self.__ambits_to_enigma2__()
diff --git a/script/my_script.py b/script/my_script.py
index 85d58513..ff0dbfe2 100644
--- a/script/my_script.py
+++ b/script/my_script.py
@@ -165,6 +165,7 @@ json_file.write(", ")
 json_file.write(json.dumps(andorra.to_json()))
 json_file.write("]")
 json_file.close()
+print("JSON Updated")
 
 # Save data to M3U8 file	
 text_file = open('./public/output/channels.m3u8', "w+")
@@ -173,5 +174,12 @@ text_file.write("# @LaQuay https://github.com/LaQuay/TDTChannels" + "\n")
 text_file.write(spain.to_m3u8())
 text_file.write(international.to_m3u8())
 text_file.close()
+print("M3U8 Updated")
 
-print("JSON + M3U8 Updated")
+# Save data to .tv file (Enigma2)
+text_file = open('./public/output/userbouquet.tdtchannels.tv', "w+")
+text_file.write("#NAME @LaQuay https://github.com/LaQuay/TDTChannels" + "\n")
+text_file.write(spain.to_enigma2())
+text_file.write(international.to_enigma2())
+text_file.close()
+print("ENIGMA2 Updated")