From 94ab7b77374b1b87580ff40b72cda334141dca55 Mon Sep 17 00:00:00 2001 From: Sowevo Date: Tue, 8 Feb 2022 21:06:43 +0800 Subject: [PATCH] Support sorting by template order. (#21) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 新的排序方式 template-order * 新的排序方式 template-order * Update models.py --- iptvtools/constants/helps.py | 2 +- iptvtools/models.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/iptvtools/constants/helps.py b/iptvtools/constants/helps.py index 17ea900..d0d8590 100644 --- a/iptvtools/constants/helps.py +++ b/iptvtools/constants/helps.py @@ -46,7 +46,7 @@ ) SORT_KEYS = ( 'List of keys to sort the channels. Valid options currently supported ' - 'are `group-title`, `tvg-id`, `height` and `title`.' + 'are `group-title`, `tvg-id`, `template-order`, `height` and `title`.' ) TEMPLATES = ( 'Template m3u files/urls with well-maintained channel information to ' diff --git a/iptvtools/models.py b/iptvtools/models.py index 27cabcb..ebe81b2 100644 --- a/iptvtools/models.py +++ b/iptvtools/models.py @@ -72,6 +72,7 @@ def parse(self): def _parse(self, sources, is_template=False): """Parse playlist sources.""" + template_order = 0 for source in sources: source_name = os.path.splitext(os.path.basename(source))[0] current_item = {} @@ -120,8 +121,10 @@ def _parse(self, sources, is_template=False): else: if is_template: + template_order = template_order + 1 for url in self.id_url.get(current_id, []): current_params = current_item['params'] + current_params['template-order'] = template_order self.data[url]['params'].update(current_params) self.data[url]['title'] = current_item['title'] else: @@ -158,7 +161,7 @@ def filter(self): pbar.write(f'{url}, {status}!') def __custom_sort(self, url): - """Sort by tvg-id, resolution and title.""" + """Sort by tvg-id, resolution, template-order and title.""" res = [] for key in self.args.sort_keys: entry = self.data[url] @@ -168,6 +171,8 @@ def __custom_sort(self, url): res.append(entry.get(key, '')) elif key == 'tvg-id': res.append(int(entry['params'].get(key) or sys.maxsize)) + elif key == 'template-order': + res.append(int(entry['params'].get(key) or sys.maxsize)) elif key == 'group-title': res.append(entry['params'].get(key) or '') return res