Skip to content

Commit

Permalink
Remove special whitespaces. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
huxuan authored Nov 27, 2021
1 parent fa014ee commit 507b6cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
19 changes: 9 additions & 10 deletions iptvtools/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 12 additions & 18 deletions iptvtools/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
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


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):
Expand All @@ -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()

0 comments on commit 507b6cd

Please sign in to comment.