Skip to content

Commit

Permalink
Lint. (#10)
Browse files Browse the repository at this point in the history
* Minor changes.

* Add pyproject.toml.

* Lint.
  • Loading branch information
huxuan authored May 19, 2021
1 parent 72a6d13 commit 96d9c9e
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
exclude = docs/conf.py,.eggs
statistics = True
3 changes: 3 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[settings]
force_single_line=True
lines_after_imports=2
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[MESSAGES CONTROL]
disable=logging-fstring-interpolation
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ dev:
pipenv install --skip-lock --dev || true

flake8:
${PIPRUN} flake8 \
--import-order-style google \
--application-import-names ${PKG} \
setup.py tests ${PKG}
${PIPRUN} flake8

pylint:
${PIPRUN} pylint setup.py tests ${PKG}

lint: dev flake8 pylint
lint: flake8 pylint

dist: clean install
${PIPRUN} python setup.py sdist bdist_wheel
Expand Down
15 changes: 12 additions & 3 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
black = "*"
flake8 = "*"
flake8-bandit = "*"
flake8-commas = "*"
flake8-comprehensions = "*"
flake8-docstrings = "*"
flake8-import-order = "*"
flake8-isort = "*"
flake8-print = "*"
flake8-quotes = "*"
flake8-use-fstring = "*"
pep8-naming = "*"
pip-check-reqs = "*"
pre-commit = "*"
pylint = "*"
sphinx = "*"
sphinx-rtd-theme = "*"
sphinxcontrib-programoutput = "*"
Sphinx = "*"
flake8-noqa = "*"

[packages]
iptvtools = {editable = true,path = "."}
iptvtools = {editable = true, path = "."}

[requires]
python_version = "3"
1 change: 1 addition & 0 deletions iptvtools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pkg_resources import DistributionNotFound
from pkg_resources import get_distribution


__version__ = '0.0.0'
try:
__version__ = get_distribution(__name__).version
Expand Down
28 changes: 14 additions & 14 deletions iptvtools/constants/helps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@
Email: i(at)huxuan.org
"""
CONFIG = (
f'Configuration file to unify title and id.'
'Configuration file to unify title and id.'
)
INPUTS = (
f'One or more input m3u playlist files/urls.'
'One or more input m3u playlist files/urls.'
)
INTERVAL = (
f'Interval in seconds between successive fetching requests.'
'Interval in seconds between successive fetching requests.'
)
MIN_HEIGHT = (
f'Minimum height/resolution to accept, 0 means no resolution filtering.'
'Minimum height/resolution to accept, 0 means no resolution filtering.'
)
OUTPUT = f'Output file name.'
OUTPUT = 'Output file name.'
REPLACE_GROUP_BY_SOURCE = (
f'Flag to replace the group title with the source name, where the source '
f'name is the basename of input files/urls without extension.'
'Flag to replace the group title with the source name, where the source '
'name is the basename of input files/urls without extension.'
)
RESOLUTION_ON_TITLE = (
f'Flag to append resolution such as 8K, 4K, 1080p, 720p to the title.'
'Flag to append resolution such as 8K, 4K, 1080p, 720p to the title.'
)
SORT_KEYS = (
f'List of keys to sort the channels. Valid options currently supported '
f'are `tvg-id`, `height` and `title`.'
'List of keys to sort the channels. Valid options currently supported '
'are `tvg-id`, `height` and `title`.'
)
TEMPLATES = (
f'Template m3u files/urls with well-maintained channel information to '
f'replace the matched entries.'
'Template m3u files/urls with well-maintained channel information to '
'replace the matched entries.'
)
TIMEOUT = (
f'Timeout threshold for fetching request.'
'Timeout threshold for fetching request.'
)
UDPXY = (
f'UDP Proxy for certain IPTV channels.'
'UDP Proxy for certain IPTV channels.'
)
1 change: 1 addition & 0 deletions iptvtools/constants/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""
import re


PARAMS = re.compile(r'(\S+)="(.*?)"')
EXTINF = re.compile(
r'^#EXTINF:(?P<duration>-?\d+?) ?(?P<params>.*),(?P<title>.*?)$')
Expand Down
11 changes: 7 additions & 4 deletions iptvtools/iptv_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Email: i(at)huxuan.org
"""
import argparse
import logging
import shutil

from iptvtools import __version__
Expand Down Expand Up @@ -53,6 +54,8 @@ def main():
"""Filter m3u playlists."""
args = parse_args()

logging.basicConfig(level=logging.INFO)

if args.min_height or args.resolution_on_title:
if shutil.which('ffprobe') is None:
raise exceptions.FFmpegNotInstalledError()
Expand All @@ -63,11 +66,11 @@ def main():
playlist.filter()
playlist.export()
if playlist.inaccessible_urls:
print('Inaccessible Urls:')
print('\n'.join(sorted(playlist.inaccessible_urls)))
logging.info('Inaccessible Urls:')
logging.info('\n'.join(sorted(playlist.inaccessible_urls)))
if playlist.poor_urls:
print('Poor resolution Urls:')
print('\n'.join(sorted(playlist.poor_urls)))
logging.info('Poor resolution Urls:')
logging.info('\n'.join(sorted(playlist.poor_urls)))


if __name__ == '__main__':
Expand Down
7 changes: 4 additions & 3 deletions iptvtools/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Author: huxuan
Email: i(at)huxuan.org
"""
import logging
import os.path
from urllib.request import urlopen

Expand Down Expand Up @@ -37,13 +38,13 @@ def parse_tag_m3u(line):

def _parse_from_file(filename):
"""Parse content from file."""
print(f'Retrieving playlists from file: {filename}')
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."""
print(f'Retrieving playlists from url: {url}')
with urlopen(url) as response:
logging.info(f'Retrieving playlists from url: {url}')
with urlopen(url) as response: # noqa: S310
return response.read().decode('utf-8').splitlines()
24 changes: 14 additions & 10 deletions iptvtools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
Email: i(at)huxuan.org
"""
import json
import logging
import socket
import struct
from subprocess import PIPE
from subprocess import Popen
from subprocess import TimeoutExpired
from subprocess import PIPE # noqa: S404
from subprocess import Popen # noqa: S404
from subprocess import TimeoutExpired # noqa: S404
from urllib.parse import urlparse

import requests

from iptvtools.config import Config


PROBE_COMMAND = (
'ffprobe -hide_banner -show_streams -select_streams v '
'-of json=c=1 -v quiet'
Expand Down Expand Up @@ -63,16 +65,18 @@ def unify_title_and_id(item):
def probe(url, timeout=None):
"""Invoke probe to get stream information."""
outs = None
proc = Popen(f'{PROBE_COMMAND} {url}'.split(), stdout=PIPE, stderr=PIPE)
try:
outs, dummy = proc.communicate(timeout=timeout)
except TimeoutExpired:
proc.kill()
with Popen( # noqa: S603
f'{PROBE_COMMAND} {url}'.split(),
stdout=PIPE, stderr=PIPE) as proc:
try:
outs, _ = proc.communicate(timeout=timeout)
except TimeoutExpired:
proc.kill()
if outs:
try:
return json.loads(outs.decode('utf-8'))
except json.JSONDecodeError as exc:
print(exc)
logging.error(exc)
return None


Expand Down Expand Up @@ -103,7 +107,7 @@ def check_udp_connectivity(url, timeout=None):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(('', int(port)))
mreq = struct.pack("4sl", socket.inet_aton(ipaddr), socket.INADDR_ANY)
mreq = struct.pack('4sl', socket.inet_aton(ipaddr), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
try:
if sock.recv(10240):
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta:__legacy__"
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from setuptools import find_packages
from setuptools import setup


NAME = 'iptvtools'
AUTHOR = 'huxuan'
EMAIL = f'i+{NAME}@huxuan.org'
Expand Down

0 comments on commit 96d9c9e

Please sign in to comment.