From a3cf34ba2a9223163a10bd732cf66bb6f9ed1e86 Mon Sep 17 00:00:00 2001 From: Dmitriy Yefremov Date: Sun, 7 Jan 2018 16:33:18 +0300 Subject: [PATCH] telnet for neutrino --- app/eparser/neutrino/bouquets.py | 4 ++++ app/ftp.py | 26 +++++++++++++++++++------- app/ui/download_dialog.py | 11 +++++++---- app/ui/main_app_window.py | 5 ++++- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/eparser/neutrino/bouquets.py b/app/eparser/neutrino/bouquets.py index 1693ad21..0f7897a9 100644 --- a/app/eparser/neutrino/bouquets.py +++ b/app/eparser/neutrino/bouquets.py @@ -90,3 +90,7 @@ def write_bouquet(file, bouquet): bq_elem.appendChild(srv_elem) doc.writexml(open(file, "w"), addindent=" ", newl="\n", encoding="UTF-8") + + +if __name__ == "__main__": + pass diff --git a/app/ftp.py b/app/ftp.py index 9b4411b7..7f99f8ba 100644 --- a/app/ftp.py +++ b/app/ftp.py @@ -5,6 +5,9 @@ from enum import Enum from ftplib import FTP from telnetlib import Telnet +from app.commons import log +from app.properties import Profile + __DATA_FILES_LIST = ("tv", "radio", "lamedb", "blacklist", "whitelist", # enigma 2 "services.xml", "myservices.xml", "bouquets.xml", "ubouquets.xml") # neutrino @@ -46,13 +49,14 @@ def download_data(*, properties, download_type=DownloadDataType.ALL): ftp.retrbinary("RETR " + xml_file, f.write) -def upload_data(*, properties, download_type=DownloadDataType.ALL, remove_unused=False): +def upload_data(*, properties, download_type=DownloadDataType.ALL, remove_unused=False, profile=Profile.ENIGMA_2): data_path = properties["data_dir_path"] host = properties["host"] # telnet - tn = telnet(host=host) + tn = telnet(host=host, user=None if profile is Profile.ENIGMA_2 else "root", password=None, + timeout=5 if profile is Profile.ENIGMA_2 else 1) next(tn) - # terminate enigma + # terminate enigma or enigma tn.send("init 4") with FTP(host=host) as ftp: @@ -80,8 +84,8 @@ def upload_data(*, properties, download_type=DownloadDataType.ALL, remove_unused if file_name == "satellites.xml": continue file_name, send_file(file_name, data_path, ftp) - # resume enigma - tn.send("init 3") + # resume enigma or restart neutrino + tn.send("init 3" if profile is Profile.ENIGMA_2 else "init 6") def send_file(file_name, path, ftp): @@ -90,14 +94,22 @@ def send_file(file_name, path, ftp): return ftp.storbinary("STOR " + file_name, f) -def telnet(host, port=23, user="root", password="root", timeout=5): +def telnet(host, port=23, user=None, password=None, timeout=5): try: tn = Telnet(host=host, port=port, timeout=timeout) except socket.timeout: - print("socket timeout") + log("telnet error: socket timeout") else: time.sleep(1) command = yield + if user is not None: + tn.read_until(b"login: ") + tn.write(user.encode("utf-8") + b"\n") + time.sleep(timeout) + if password is not None: + tn.read_until(b"Password: ") + tn.write(password.encode("utf-8") + b"\n") + time.sleep(timeout) tn.write("{}\r\n".format(command).encode("utf-8")) time.sleep(timeout) command = yield diff --git a/app/ui/download_dialog.py b/app/ui/download_dialog.py index 0a0773c8..3d07e7c3 100644 --- a/app/ui/download_dialog.py +++ b/app/ui/download_dialog.py @@ -1,19 +1,21 @@ from app.commons import run_idle, run_task from app.ftp import download_data, DownloadDataType, upload_data +from app.properties import Profile from . import Gtk, UI_RESOURCES_PATH from .dialogs import show_dialog, DialogType -def show_download_dialog(transient, options, open_data): - dialog = DownloadDialog(transient, options, open_data) +def show_download_dialog(transient, options, open_data, profile=Profile.ENIGMA_2): + dialog = DownloadDialog(transient, options, open_data, profile) dialog.run() dialog.destroy() class DownloadDialog: - def __init__(self, transient, properties, open_data): + def __init__(self, transient, properties, open_data, profile): self._properties = properties self._open_data = open_data + self._profile = profile handlers = {"on_receive": self.on_receive, "on_send": self.on_send, @@ -72,7 +74,8 @@ class DownloadDialog: self.show_info_message("Please, wait...", Gtk.MessageType.INFO) upload_data(properties=self._properties, download_type=d_type, - remove_unused=self._remove_unused_check_button.get_active()) + remove_unused=self._remove_unused_check_button.get_active(), + profile=self._profile) except Exception as e: message = str(getattr(e, "message", str(e))) self.show_info_message(message, Gtk.MessageType.ERROR) diff --git a/app/ui/main_app_window.py b/app/ui/main_app_window.py index 70e20a87..ebbfab47 100644 --- a/app/ui/main_app_window.py +++ b/app/ui/main_app_window.py @@ -655,7 +655,10 @@ class MainAppWindow: pass def on_download(self, item): - show_download_dialog(self.__main_window, self.__options.get(self.__profile), self.open_data) + show_download_dialog(transient=self.__main_window, + options=self.__options.get(self.__profile), + open_data=self.open_data, + profile=Profile(self.__profile)) @run_idle def on_view_focus(self, view, focus_event):