2017-11-12 22:23:12 +03:00
|
|
|
import os
|
|
|
|
|
import socket
|
|
|
|
|
import time
|
2017-11-06 22:17:43 +03:00
|
|
|
from enum import Enum
|
2018-01-20 14:04:07 +03:00
|
|
|
from ftplib import FTP, error_perm
|
2017-11-12 22:23:12 +03:00
|
|
|
from telnetlib import Telnet
|
2017-11-06 22:17:43 +03:00
|
|
|
|
2018-01-07 16:33:18 +03:00
|
|
|
from app.commons import log
|
|
|
|
|
from app.properties import Profile
|
|
|
|
|
|
2018-06-05 20:45:47 +03:00
|
|
|
__DATA_FILES_LIST = ("tv", "radio", "lamedb", "lamedb5", "blacklist", "whitelist", # enigma 2
|
2018-01-04 20:58:22 +03:00
|
|
|
"services.xml", "myservices.xml", "bouquets.xml", "ubouquets.xml") # neutrino
|
2017-10-14 12:24:59 +03:00
|
|
|
|
2018-02-12 13:34:00 +03:00
|
|
|
_SATELLITES_XML_FILE = "satellites.xml"
|
|
|
|
|
_WEBTV_XML_FILE = "webtv.xml"
|
|
|
|
|
|
2017-10-14 12:24:59 +03:00
|
|
|
|
2017-11-06 22:17:43 +03:00
|
|
|
class DownloadDataType(Enum):
|
|
|
|
|
ALL = 0
|
|
|
|
|
BOUQUETS = 1
|
|
|
|
|
SATELLITES = 2
|
2018-01-16 18:51:08 +03:00
|
|
|
PICONS = 3
|
2018-02-12 13:34:00 +03:00
|
|
|
WEBTV = 4
|
2017-11-06 22:17:43 +03:00
|
|
|
|
|
|
|
|
|
2018-01-16 18:51:08 +03:00
|
|
|
def download_data(*, properties, download_type=DownloadDataType.ALL, callback=None):
|
2017-11-12 22:23:12 +03:00
|
|
|
with FTP(host=properties["host"]) as ftp:
|
2017-10-14 12:24:59 +03:00
|
|
|
ftp.login(user=properties["user"], passwd=properties["password"])
|
2018-02-05 18:24:49 +03:00
|
|
|
ftp.encoding = "utf-8"
|
2017-10-14 12:24:59 +03:00
|
|
|
save_path = properties["data_dir_path"]
|
2017-12-30 22:58:47 +03:00
|
|
|
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
2017-10-14 12:24:59 +03:00
|
|
|
files = []
|
2017-11-06 22:17:43 +03:00
|
|
|
# bouquets section
|
|
|
|
|
if download_type is DownloadDataType.ALL or download_type is DownloadDataType.BOUQUETS:
|
|
|
|
|
ftp.cwd(properties["services_path"])
|
|
|
|
|
ftp.dir(files.append)
|
|
|
|
|
|
|
|
|
|
for file in files:
|
|
|
|
|
name = str(file).strip()
|
|
|
|
|
if name.endswith(__DATA_FILES_LIST):
|
|
|
|
|
name = name.split()[-1]
|
2018-02-12 13:34:00 +03:00
|
|
|
download_file(ftp, name, save_path)
|
|
|
|
|
# satellites.xml and webtv section
|
|
|
|
|
if download_type in (DownloadDataType.ALL, DownloadDataType.SATELLITES, DownloadDataType.WEBTV):
|
2017-11-06 22:17:43 +03:00
|
|
|
ftp.cwd(properties["satellites_xml_path"])
|
|
|
|
|
files.clear()
|
|
|
|
|
ftp.dir(files.append)
|
|
|
|
|
|
|
|
|
|
for file in files:
|
|
|
|
|
name = str(file).strip()
|
2018-02-12 13:34:00 +03:00
|
|
|
if download_type in (DownloadDataType.ALL, DownloadDataType.SATELLITES):
|
|
|
|
|
if name.endswith(_SATELLITES_XML_FILE):
|
|
|
|
|
download_file(ftp, _SATELLITES_XML_FILE, save_path)
|
|
|
|
|
elif download_type in (DownloadDataType.ALL, DownloadDataType.WEBTV):
|
|
|
|
|
if name.endswith(_WEBTV_XML_FILE):
|
|
|
|
|
download_file(ftp, _WEBTV_XML_FILE, save_path)
|
2017-10-26 01:23:05 +03:00
|
|
|
|
2018-01-16 18:51:08 +03:00
|
|
|
if callback is not None:
|
|
|
|
|
callback()
|
2017-10-14 12:24:59 +03:00
|
|
|
|
2018-01-16 18:51:08 +03:00
|
|
|
|
|
|
|
|
def upload_data(*, properties, download_type=DownloadDataType.ALL, remove_unused=False, profile=Profile.ENIGMA_2,
|
|
|
|
|
callback=None):
|
2017-11-10 13:38:03 +03:00
|
|
|
data_path = properties["data_dir_path"]
|
2017-11-12 22:23:12 +03:00
|
|
|
host = properties["host"]
|
|
|
|
|
# telnet
|
2018-01-18 00:57:58 +03:00
|
|
|
tn = telnet(host=host, user=properties.get("telnet_user", "root"), password=properties.get("telnet_password", ""),
|
|
|
|
|
timeout=properties.get("telnet_timeout", 5))
|
2017-11-12 22:23:12 +03:00
|
|
|
next(tn)
|
2018-05-01 21:05:18 +03:00
|
|
|
# terminate enigma or neutrino
|
2017-11-12 23:39:27 +03:00
|
|
|
tn.send("init 4")
|
2017-11-10 13:38:03 +03:00
|
|
|
|
2017-11-12 22:23:12 +03:00
|
|
|
with FTP(host=host) as ftp:
|
2017-11-10 13:38:03 +03:00
|
|
|
ftp.login(user=properties["user"], passwd=properties["password"])
|
2018-02-05 18:24:49 +03:00
|
|
|
ftp.encoding = "utf-8"
|
2017-11-10 13:38:03 +03:00
|
|
|
|
|
|
|
|
if download_type is DownloadDataType.ALL or download_type is DownloadDataType.SATELLITES:
|
|
|
|
|
ftp.cwd(properties["satellites_xml_path"])
|
2018-02-12 13:34:00 +03:00
|
|
|
send = send_file(_SATELLITES_XML_FILE, data_path, ftp)
|
|
|
|
|
if download_type is DownloadDataType.SATELLITES:
|
|
|
|
|
tn.send("init 3" if profile is Profile.ENIGMA_2 else "init 6")
|
|
|
|
|
if callback is not None:
|
|
|
|
|
callback()
|
|
|
|
|
return send
|
|
|
|
|
|
|
|
|
|
if profile is Profile.NEUTRINO_MP and download_type in (DownloadDataType.ALL, DownloadDataType.WEBTV):
|
|
|
|
|
ftp.cwd(properties["satellites_xml_path"])
|
|
|
|
|
send = send_file(_WEBTV_XML_FILE, data_path, ftp)
|
|
|
|
|
if download_type is DownloadDataType.WEBTV:
|
|
|
|
|
tn.send("init 6")
|
|
|
|
|
if callback is not None:
|
|
|
|
|
callback()
|
2017-11-10 13:38:03 +03:00
|
|
|
return send
|
|
|
|
|
|
|
|
|
|
if download_type is DownloadDataType.ALL or download_type is DownloadDataType.BOUQUETS:
|
|
|
|
|
ftp.cwd(properties["services_path"])
|
|
|
|
|
if remove_unused:
|
|
|
|
|
files = []
|
|
|
|
|
ftp.dir(files.append)
|
|
|
|
|
for file in files:
|
|
|
|
|
name = str(file).strip()
|
|
|
|
|
if name.endswith(__DATA_FILES_LIST):
|
|
|
|
|
name = name.split()[-1]
|
|
|
|
|
ftp.delete(name)
|
|
|
|
|
|
|
|
|
|
for file_name in os.listdir(data_path):
|
2018-02-12 13:34:00 +03:00
|
|
|
if file_name == _SATELLITES_XML_FILE or file_name == _WEBTV_XML_FILE:
|
2017-11-10 13:38:03 +03:00
|
|
|
continue
|
2018-01-20 11:29:34 +03:00
|
|
|
if file_name.endswith(__DATA_FILES_LIST):
|
2018-01-18 12:38:58 +03:00
|
|
|
send_file(file_name, data_path, ftp)
|
2018-01-16 18:51:08 +03:00
|
|
|
|
|
|
|
|
if download_type is DownloadDataType.PICONS:
|
2018-01-20 14:04:07 +03:00
|
|
|
picons_dir_path = properties.get("picons_dir_path")
|
|
|
|
|
picons_path = properties.get("picons_path")
|
|
|
|
|
try:
|
|
|
|
|
ftp.cwd(picons_path)
|
|
|
|
|
except error_perm as e:
|
|
|
|
|
if str(e).startswith("550"):
|
|
|
|
|
ftp.mkd(picons_path) # if not exist
|
|
|
|
|
ftp.cwd(picons_path)
|
|
|
|
|
|
2018-01-16 18:51:08 +03:00
|
|
|
files = []
|
|
|
|
|
ftp.dir(files.append)
|
|
|
|
|
picons_suf = (".jpg", ".png")
|
|
|
|
|
|
|
|
|
|
for file in files:
|
|
|
|
|
name = str(file).strip()
|
|
|
|
|
if name.endswith(picons_suf):
|
|
|
|
|
name = name.split()[-1]
|
|
|
|
|
ftp.delete(name)
|
2018-01-20 14:04:07 +03:00
|
|
|
for file_name in os.listdir(picons_dir_path):
|
2018-01-16 18:51:08 +03:00
|
|
|
if file_name.endswith(picons_suf):
|
2018-01-20 14:04:07 +03:00
|
|
|
send_file(file_name, picons_dir_path, ftp)
|
2018-01-16 18:51:08 +03:00
|
|
|
|
2018-01-07 16:33:18 +03:00
|
|
|
# resume enigma or restart neutrino
|
|
|
|
|
tn.send("init 3" if profile is Profile.ENIGMA_2 else "init 6")
|
2017-11-10 13:38:03 +03:00
|
|
|
|
2018-01-16 18:51:08 +03:00
|
|
|
if callback is not None:
|
|
|
|
|
callback()
|
|
|
|
|
|
2017-11-10 13:38:03 +03:00
|
|
|
|
2018-02-12 13:34:00 +03:00
|
|
|
def download_file(ftp, name, save_path):
|
|
|
|
|
with open(save_path + name, "wb") as f:
|
|
|
|
|
ftp.retrbinary("RETR " + name, f.write)
|
|
|
|
|
|
|
|
|
|
|
2017-11-10 13:38:03 +03:00
|
|
|
def send_file(file_name, path, ftp):
|
|
|
|
|
""" Opens the file in binary mode and transfers into receiver """
|
|
|
|
|
with open(path + file_name, "rb") as f:
|
|
|
|
|
return ftp.storbinary("STOR " + file_name, f)
|
|
|
|
|
|
|
|
|
|
|
2018-01-18 00:57:58 +03:00
|
|
|
def telnet(host, port=23, user="", password="", timeout=5):
|
2017-11-12 22:23:12 +03:00
|
|
|
try:
|
|
|
|
|
tn = Telnet(host=host, port=port, timeout=timeout)
|
|
|
|
|
except socket.timeout:
|
2018-01-07 16:33:18 +03:00
|
|
|
log("telnet error: socket timeout")
|
2017-11-12 22:23:12 +03:00
|
|
|
else:
|
2017-11-14 19:20:16 +03:00
|
|
|
time.sleep(1)
|
2017-11-12 22:23:12 +03:00
|
|
|
command = yield
|
2018-01-18 00:57:58 +03:00
|
|
|
if user != "":
|
2018-01-07 16:33:18 +03:00
|
|
|
tn.read_until(b"login: ")
|
|
|
|
|
tn.write(user.encode("utf-8") + b"\n")
|
|
|
|
|
time.sleep(timeout)
|
2018-01-18 00:57:58 +03:00
|
|
|
if password != "":
|
2018-01-07 16:33:18 +03:00
|
|
|
tn.read_until(b"Password: ")
|
|
|
|
|
tn.write(password.encode("utf-8") + b"\n")
|
|
|
|
|
time.sleep(timeout)
|
2017-11-12 22:23:12 +03:00
|
|
|
tn.write("{}\r\n".format(command).encode("utf-8"))
|
2017-11-14 19:20:16 +03:00
|
|
|
time.sleep(timeout)
|
2017-11-12 22:23:12 +03:00
|
|
|
command = yield
|
|
|
|
|
time.sleep(timeout)
|
|
|
|
|
tn.write("{}\r\n".format(command).encode("utf-8"))
|
|
|
|
|
time.sleep(timeout)
|
|
|
|
|
tn.close()
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
pass
|