From 3d36e9db8393e08a86949ceaa48be7c981b99fc6 Mon Sep 17 00:00:00 2001 From: huxuan Date: Fri, 3 Jan 2020 14:14:40 +0800 Subject: [PATCH] Squashed commit of the following: commit 6c01bd0891e1f440d4b2d8bd58a3602b646a18bf Author: huxuan Date: Fri Jan 3 14:12:57 2020 +0800 More robust udpxy. commit 438f8499c80880ec1fa4feab069532285be52997 Author: huxuan Date: Fri Jan 3 14:10:21 2020 +0800 Better version. commit 123de083a3a24ead86697f0730b9220c7ace8712 Author: huxuan Date: Fri Jan 3 13:33:58 2020 +0800 Change filter to unify. --- config.json | 4 ++-- iptvtools/__init__.py | 9 +++++++++ iptvtools/constants/helps.py | 2 +- iptvtools/iptv_filter.py | 3 +++ iptvtools/models.py | 2 +- iptvtools/utils.py | 32 +++++++++++++++++++------------- setup.py | 4 +++- 7 files changed, 38 insertions(+), 18 deletions(-) diff --git a/config.json b/config.json index 945941e..5310bd6 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { - "id_filters": { + "id_unifiers": { "-": "", "IPTV": "", "BTV冬奥纪实": "北京纪实", @@ -11,7 +11,7 @@ "CETV4": "中国教育4台", "纪实频道": "上海纪实" }, - "title_filters": { + "title_unifiers": { "高清": "", "HD": "", "+": "+" diff --git a/iptvtools/__init__.py b/iptvtools/__init__.py index e69de29..f595873 100644 --- a/iptvtools/__init__.py +++ b/iptvtools/__init__.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +File: __init__.py +Author: huxuan +Email: i(at)huxuan.org +Description: iptvtools package. +""" +__version__ = '0.1.2' diff --git a/iptvtools/constants/helps.py b/iptvtools/constants/helps.py index a2a275e..db16272 100644 --- a/iptvtools/constants/helps.py +++ b/iptvtools/constants/helps.py @@ -9,7 +9,7 @@ from . import defaults CONFIG = ( - f'Configuration file with title and id filter information, defaults to ' + f'Configuration file to unify title and id information, defaults to ' f'`{defaults.CONFIG}`' ) INPUT = ( diff --git a/iptvtools/iptv_filter.py b/iptvtools/iptv_filter.py index 7fb0aa6..dcf82f1 100644 --- a/iptvtools/iptv_filter.py +++ b/iptvtools/iptv_filter.py @@ -8,6 +8,7 @@ """ import argparse +from . import __version__ from .config import Config from .constants import defaults from .constants import helps @@ -31,6 +32,8 @@ def parse_args(): help=helps.TIMEOUT) parser.add_argument('-u', '--udpxy', default=defaults.UDPXY, help=helps.UDPXY) + parser.add_argument('-v', '--version', action='version', + version=__version__) return parser.parse_args() diff --git a/iptvtools/models.py b/iptvtools/models.py index 75586d9..fe7ce21 100644 --- a/iptvtools/models.py +++ b/iptvtools/models.py @@ -60,7 +60,7 @@ def parse(self, content, udpxy=None, is_template=False): continue if line.startswith(tags.INF): current_item = parsers.parse_tag_inf(line) - current_item = utils.filter_title_and_id(current_item) + current_item = utils.unify_title_and_id(current_item) else: if is_template: self.template[current_item['id']] = current_item diff --git a/iptvtools/utils.py b/iptvtools/utils.py index 61ec2db..307f7f4 100644 --- a/iptvtools/utils.py +++ b/iptvtools/utils.py @@ -19,33 +19,39 @@ '-of json=c=1 -v quiet' ) +ACCEPT_SCHEME = ( + 'udp', + 'rtp' +) + def convert_url_with_udpxy(orig_url, udpxy): """Convert url with udpxy.""" - orig_url = orig_url.replace('///', '//') # Hack to some abnormal urls. + orig_url = orig_url.replace('///', '//') # Hack for some abnormal urls. parsed_url = urlparse(orig_url) - new_url = f'{udpxy}/{parsed_url.scheme}/{parsed_url.netloc}' - return new_url + if parsed_url.scheme in ACCEPT_SCHEME: + return f'{udpxy}/{parsed_url.scheme}/{parsed_url.netloc}' + return orig_url -def filter_title_and_id(item): - """Filter title and id.""" - for title_filter in sorted(Config().title_filters): - if title_filter in item['title']: +def unify_title_and_id(item): + """Unify title and id.""" + for title_unifier in sorted(Config().title_unifiers): + if title_unifier in item['title']: item['title'] = item['title'].replace( - title_filter, - Config().title_filters[title_filter]) + title_unifier, + Config().title_unifiers[title_unifier]) if 'tvg-name' in item.get('params'): item['id'] = item['params']['tvg-name'] else: item['id'] = item['title'] - for id_filter in sorted(Config().id_filters): - if id_filter in item['id']: + for id_unifier in sorted(Config().id_unifiers): + if id_unifier in item['id']: item['id'] = item['id'].replace( - id_filter, - Config().id_filters[id_filter]) + id_unifier, + Config().id_unifiers[id_unifier]) return item diff --git a/setup.py b/setup.py index f76b80e..df2529f 100644 --- a/setup.py +++ b/setup.py @@ -9,6 +9,8 @@ from setuptools import find_packages from setuptools import setup +import iptvtools + DESCRIPTION = ( 'A set of scripts that help to better IPTV experience.' ) @@ -43,7 +45,7 @@ def readme(): setup(name='iptvtools', - version='0.1.1', + version=iptvtools.__version__, description=DESCRIPTION, long_description=readme(), long_description_content_type='text/markdown',