2018-01-12 14:32:36 +03:00
|
|
|
import os
|
|
|
|
|
import shutil
|
2018-01-15 14:56:17 +03:00
|
|
|
from collections import namedtuple
|
2018-01-12 14:32:36 +03:00
|
|
|
from html.parser import HTMLParser
|
|
|
|
|
|
2018-01-16 01:16:03 +03:00
|
|
|
from app.commons import log
|
2018-01-12 14:32:36 +03:00
|
|
|
from app.properties import Profile
|
|
|
|
|
|
2018-01-15 14:56:17 +03:00
|
|
|
Provider = namedtuple("Provider", ["logo", "name", "url", "on_id", "selected"])
|
|
|
|
|
Picon = namedtuple("Picon", ["ref", "ssid", "v_pid"])
|
|
|
|
|
|
2018-01-12 14:32:36 +03:00
|
|
|
|
|
|
|
|
class PiconsParser(HTMLParser):
|
2018-01-15 14:56:17 +03:00
|
|
|
""" Parser for package html page. (https://www.lyngsat.com/packages/*provider-name*.html) """
|
2018-01-12 14:32:36 +03:00
|
|
|
|
|
|
|
|
def __init__(self, entities=False, separator=' '):
|
|
|
|
|
|
|
|
|
|
HTMLParser.__init__(self)
|
|
|
|
|
|
|
|
|
|
self._parse_html_entities = entities
|
|
|
|
|
self._separator = separator
|
|
|
|
|
self._is_td = False
|
|
|
|
|
self._is_th = False
|
|
|
|
|
self._current_row = []
|
|
|
|
|
self._current_cell = []
|
2018-01-16 01:16:03 +03:00
|
|
|
self.picons = []
|
2018-01-12 14:32:36 +03:00
|
|
|
|
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
|
|
|
if tag == 'td':
|
|
|
|
|
self._is_td = True
|
|
|
|
|
if tag == 'th':
|
|
|
|
|
self._is_th = True
|
|
|
|
|
if tag == "img":
|
|
|
|
|
self._current_row.append(attrs[0][1])
|
|
|
|
|
|
|
|
|
|
def handle_data(self, data):
|
|
|
|
|
""" Save content to a cell """
|
|
|
|
|
if self._is_td or self._is_th:
|
|
|
|
|
self._current_cell.append(data.strip())
|
|
|
|
|
|
|
|
|
|
def handle_endtag(self, tag):
|
|
|
|
|
if tag == 'td':
|
|
|
|
|
self._is_td = False
|
|
|
|
|
elif tag == 'th':
|
|
|
|
|
self._is_th = False
|
|
|
|
|
|
|
|
|
|
if tag in ('td', 'th'):
|
|
|
|
|
final_cell = self._separator.join(self._current_cell).strip()
|
|
|
|
|
self._current_row.append(final_cell)
|
|
|
|
|
self._current_cell = []
|
|
|
|
|
elif tag == 'tr':
|
|
|
|
|
row = self._current_row
|
|
|
|
|
ln = len(row)
|
2018-01-16 01:16:03 +03:00
|
|
|
if 9 < ln < 13:
|
|
|
|
|
url = None
|
|
|
|
|
if row[0].startswith("../logo/"):
|
|
|
|
|
url = row[0]
|
|
|
|
|
elif row[1].startswith("../logo/"):
|
|
|
|
|
url = row[1]
|
|
|
|
|
|
|
|
|
|
ssid = row[-4]
|
|
|
|
|
if url and len(ssid) > 2:
|
|
|
|
|
self.picons.append(Picon(url, ssid, row[-3]))
|
|
|
|
|
|
2018-01-12 14:32:36 +03:00
|
|
|
self._current_row = []
|
|
|
|
|
|
|
|
|
|
def error(self, message):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2018-01-17 01:18:02 +03:00
|
|
|
def parse(open_path, picons_path, tmp_path, on_id, profile=Profile.ENIGMA_2):
|
2018-01-12 14:32:36 +03:00
|
|
|
with open(open_path, encoding="utf-8", errors="replace") as f:
|
|
|
|
|
parser = PiconsParser()
|
|
|
|
|
parser.reset()
|
|
|
|
|
parser.feed(f.read())
|
2018-01-16 01:16:03 +03:00
|
|
|
picons = parser.picons
|
|
|
|
|
if picons:
|
2018-01-12 14:32:36 +03:00
|
|
|
os.makedirs(picons_path, exist_ok=True)
|
2018-01-16 01:16:03 +03:00
|
|
|
for p in picons:
|
|
|
|
|
try:
|
2018-01-17 01:18:02 +03:00
|
|
|
picon_file_name = picons_path + PiconsParser.format(p.ssid, on_id, p.v_pid, profile)
|
2018-01-16 01:16:03 +03:00
|
|
|
shutil.copyfile(tmp_path + "www.lyngsat.com/" + p.ref.lstrip("."), picon_file_name)
|
|
|
|
|
except (TypeError, ValueError) as e:
|
|
|
|
|
log("Picons format parse error: {} {} {}".format(p.ref, p.ssid, p.v_pid) + "\n" + str(e))
|
|
|
|
|
print(e)
|
2018-01-12 14:32:36 +03:00
|
|
|
|
|
|
|
|
@staticmethod
|
2018-01-16 01:16:03 +03:00
|
|
|
def format(ssid, on_id, v_pid, profile: Profile):
|
2018-01-17 01:18:02 +03:00
|
|
|
tr_id = int(ssid[:-2] if len(ssid) < 4 else ssid[:2])
|
2018-01-12 14:32:36 +03:00
|
|
|
if profile is Profile.ENIGMA_2:
|
2018-01-16 01:16:03 +03:00
|
|
|
return "1_0_{}_{:X}_{:X}_{:X}_1680000_0_0_0.png".format(1 if v_pid else 2, int(ssid), tr_id, int(on_id))
|
2018-01-12 14:32:36 +03:00
|
|
|
elif profile is Profile.NEUTRINO_MP:
|
2018-01-17 01:18:02 +03:00
|
|
|
return "{:x}{:04x}{:04x}.png".format(tr_id, int(on_id), int(ssid))
|
2018-01-12 14:32:36 +03:00
|
|
|
else:
|
|
|
|
|
return "{}.png".format(ssid)
|
|
|
|
|
|
|
|
|
|
|
2018-01-15 14:56:17 +03:00
|
|
|
class ProviderParser(HTMLParser):
|
|
|
|
|
""" Parser for satellite html page. (https://www.lyngsat.com/*sat-name*.html) """
|
|
|
|
|
|
|
|
|
|
def __init__(self, entities=False, separator=' '):
|
|
|
|
|
|
|
|
|
|
HTMLParser.__init__(self)
|
|
|
|
|
|
|
|
|
|
self._ON_ID_BLACK_LIST = ("65535", "?", "0", "1")
|
|
|
|
|
self._parse_html_entities = entities
|
|
|
|
|
self._separator = separator
|
|
|
|
|
self._is_td = False
|
|
|
|
|
self._is_th = False
|
|
|
|
|
self._is_provider = False
|
|
|
|
|
self._current_row = []
|
|
|
|
|
self._current_cell = []
|
|
|
|
|
self.rows = []
|
|
|
|
|
self._ids = set()
|
|
|
|
|
|
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
|
|
|
if tag == 'td':
|
|
|
|
|
self._is_td = True
|
|
|
|
|
if tag == 'tr':
|
|
|
|
|
self._is_th = True
|
|
|
|
|
if tag == "img":
|
|
|
|
|
if attrs[0][1].startswith("logo/"):
|
|
|
|
|
self._current_row.append(attrs[0][1])
|
|
|
|
|
if tag == "a":
|
|
|
|
|
if "https://www.lyngsat.com/packages/" in attrs[0][1]:
|
|
|
|
|
self._current_row.append(attrs[0][1])
|
|
|
|
|
|
|
|
|
|
def handle_data(self, data):
|
|
|
|
|
""" Save content to a cell """
|
|
|
|
|
if self._is_td or self._is_th:
|
|
|
|
|
self._current_cell.append(data.strip())
|
|
|
|
|
|
|
|
|
|
def handle_endtag(self, tag):
|
|
|
|
|
if tag == 'td':
|
|
|
|
|
self._is_td = False
|
|
|
|
|
elif tag == 'tr':
|
|
|
|
|
self._is_th = False
|
|
|
|
|
|
|
|
|
|
if tag in ('td', 'th'):
|
|
|
|
|
final_cell = self._separator.join(self._current_cell).strip()
|
|
|
|
|
self._current_row.append(final_cell)
|
|
|
|
|
self._current_cell = []
|
|
|
|
|
elif tag == 'tr':
|
|
|
|
|
row = self._current_row
|
|
|
|
|
if len(row) == 12:
|
|
|
|
|
on_id, sep, tid = str(row[-2]).partition("-")
|
|
|
|
|
if tid and on_id not in self._ON_ID_BLACK_LIST and on_id not in self._ids:
|
2018-01-16 01:16:03 +03:00
|
|
|
row[-2] = on_id
|
2018-01-15 14:56:17 +03:00
|
|
|
self.rows.append(row)
|
|
|
|
|
self._ids.add(on_id)
|
|
|
|
|
self._current_row = []
|
|
|
|
|
|
|
|
|
|
def error(self, message):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_providers(open_path):
|
|
|
|
|
with open(open_path, encoding="utf-8", errors="replace") as f:
|
|
|
|
|
parser = ProviderParser()
|
|
|
|
|
parser.reset()
|
|
|
|
|
parser.feed(f.read())
|
|
|
|
|
rows = parser.rows
|
|
|
|
|
|
|
|
|
|
if rows:
|
|
|
|
|
return [Provider(logo=r[2], name=r[5], url=r[6], on_id=r[-2], selected=True) for r in rows]
|
|
|
|
|
|
|
|
|
|
|
2018-01-12 14:32:36 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
pass
|