From 507b6cd5591315b8e94147ef85f946ce52001a76 Mon Sep 17 00:00:00 2001 From: "Xuan (Sean) Hu" Date: Sat, 27 Nov 2021 23:35:18 +0800 Subject: [PATCH] Remove special whitespaces. (#19) --- iptvtools/models.py | 19 +++++++++---------- iptvtools/parsers.py | 30 ++++++++++++------------------ 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/iptvtools/models.py b/iptvtools/models.py index 029cd25..aef212f 100644 --- a/iptvtools/models.py +++ b/iptvtools/models.py @@ -73,21 +73,20 @@ def parse(self): def _parse(self, sources, is_template=False): """Parse playlist sources.""" for source in sources: - lines = parsers.parse_content_to_lines(source) source_name = os.path.splitext(os.path.basename(source))[0] - - if lines[0].startswith(tags.M3U): - res = parsers.parse_tag_m3u(lines[0]) - if res.get('tvg-url'): - self.tvg_url = res.get('tvg-url') - lines = lines[1:] - current_item = {} skip = False - for line in lines: - line = line.strip() + is_first_line = True + for line in parsers.parse_content_to_lines(source): if not line: continue + if is_first_line: + is_first_line = False + if line.startswith(tags.M3U): + res = parsers.parse_tag_m3u(line) + if res.get('tvg-url'): + self.tvg_url = res.get('tvg-url') + continue if skip: skip = False continue diff --git a/iptvtools/parsers.py b/iptvtools/parsers.py index 48bb007..8a032f4 100644 --- a/iptvtools/parsers.py +++ b/iptvtools/parsers.py @@ -7,9 +7,11 @@ Author: huxuan Email: i(at)huxuan.org """ -import logging import os.path -from urllib.request import urlopen +import re +import tempfile + +import requests from iptvtools.constants import patterns @@ -17,8 +19,14 @@ def parse_content_to_lines(content): """Universal interface to split content into lines.""" if os.path.isfile(content): - return _parse_from_file(content) - return _parse_from_url(content) + fp = open(content, encoding='utf-8') + else: + fp = tempfile.TemporaryFile() + fp.write(requests.get(content)) + fp.seek(0) + for line in fp: + yield re.sub(r'[^\S ]+', '', line.strip()) + fp.close() def parse_tag_inf(line): @@ -34,17 +42,3 @@ def parse_tag_m3u(line): """Parse M3U content.""" match = patterns.EXTM3U.fullmatch(line) return match.groupdict() - - -def _parse_from_file(filename): - """Parse content from file.""" - logging.info(f'Retrieving playlists from file: {filename}') - with open(filename, encoding='utf-8') as fin: - return fin.read().splitlines() - - -def _parse_from_url(url): - """Parse content from url.""" - logging.info(f'Retrieving playlists from url: {url}') - with urlopen(url) as response: # noqa: S310 - return response.read().decode('utf-8').splitlines()