added encoding detection for *.m3u import

This commit is contained in:
DYefremov
2020-12-07 09:30:03 +03:00
parent caf4925409
commit 59748aa9ba
2 changed files with 25 additions and 6 deletions

View File

@@ -21,15 +21,27 @@ class StreamType(Enum):
E_SERVICE_URI = "8193"
def parse_m3u(path, s_type):
with open(path) as file:
def parse_m3u(path, s_type, detect_encoding=True):
with open(path, "rb") as file:
data = file.read()
encoding = "utf-8"
if detect_encoding:
try:
import chardet
except ModuleNotFoundError:
pass
else:
enc = chardet.detect(data)
encoding = enc.get("encoding", "utf-8")
aggr = [None] * 10
services = []
groups = set()
counter = 0
name = None
for line in file.readlines():
for line in str(data, encoding=encoding, errors="ignore").splitlines():
if line.startswith("#EXTINF"):
name = line[1 + line.index(","):].strip()
elif line.startswith("#EXTGRP") and s_type is SettingsType.ENIGMA_2:

View File

@@ -2140,10 +2140,17 @@ class Application(Gtk.Application):
self.show_error_dialog("No m3u file is selected!")
return
channels = parse_m3u(response, self._s_type)
self._wait_dialog.show()
self.get_m3u(response)
if channels and self._bq_selected:
self.append_imported_services(channels)
@run_task
def get_m3u(self, path):
try:
channels = parse_m3u(path, self._s_type)
if channels and self._bq_selected:
GLib.idle_add(self.append_imported_services, channels)
finally:
GLib.idle_add(self._wait_dialog.hide)
def append_imported_services(self, services):
bq_services = self._bouquets.get(self._bq_selected)