Files
DemonEditor/app/ftp.py

150 lines
5.2 KiB
Python
Raw Normal View History

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
2017-10-14 12:24:59 +03:00
from ftplib import FTP
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-01-04 20:58:22 +03:00
__DATA_FILES_LIST = ("tv", "radio", "lamedb", "blacklist", "whitelist", # enigma 2
"services.xml", "myservices.xml", "bouquets.xml", "ubouquets.xml") # neutrino
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
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"])
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]
2017-11-12 22:23:12 +03:00
with open(save_path + name, "wb") as f:
ftp.retrbinary("RETR " + name, f.write)
2017-10-14 12:24:59 +03:00
# satellites.xml section
2017-11-06 22:17:43 +03:00
if download_type is DownloadDataType.ALL or download_type is DownloadDataType.SATELLITES:
ftp.cwd(properties["satellites_xml_path"])
files.clear()
ftp.dir(files.append)
for file in files:
name = str(file).strip()
xml_file = "satellites.xml"
if name.endswith(xml_file):
with open(save_path + xml_file, 'wb') as f:
2017-11-12 22:23:12 +03:00
ftp.retrbinary("RETR " + xml_file, f.write)
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-07 16:33:18 +03:00
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)
2017-11-12 22:23:12 +03:00
next(tn)
2018-01-07 16:33:18 +03:00
# terminate enigma or enigma
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"])
if download_type is DownloadDataType.ALL or download_type is DownloadDataType.SATELLITES:
ftp.cwd(properties["satellites_xml_path"])
file_name = "satellites.xml"
send = send_file(file_name, data_path, ftp)
if download_type == DownloadDataType.SATELLITES:
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):
if file_name == "satellites.xml":
continue
2017-11-12 22:23:12 +03:00
file_name, send_file(file_name, data_path, ftp)
2018-01-16 18:51:08 +03:00
if download_type is DownloadDataType.PICONS:
picons_path = properties.get("picons_dir_path")
ftp.cwd(properties.get("picons_path"))
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)
for file_name in os.listdir(picons_path):
if file_name.endswith(picons_suf):
send_file(file_name, picons_path, ftp)
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
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-07 16:33:18 +03:00
def telnet(host, port=23, user=None, password=None, 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-07 16:33:18 +03:00
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)
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