-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiptv_channels.py
84 lines (72 loc) · 2.52 KB
/
iptv_channels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import re
import sys
import argparse
from io import open
keywords = {}
class M3UFile:
def __init__(self, file):
self.file = file
"""
Extract group-title
"""
def extract_group_from_string(self, string):
#print(string)
return re.search('group-title="(.*)"', string).group(1)
"""
List group titles
"""
def groups(self):
group_set = set()
with open(self.file, "r", encoding="utf8") as f:
for line in f:
if line.startswith("#EXTINF:"):
group_name = self.extract_group_from_string(line)
group_set.add(group_name)
return sorted(group_set)
"""
Restrict list to channels with filter keywords
"""
def filter(self, filters):
results = []
keep_next_row = False
with open(self.file, "r", encoding="utf8") as f:
# file header
results.append(f.readline())
for line in f:
if keep_next_row:
results.append(line)
keep_next_row = False
if any(keyword in line for keyword in filters):
results.append(line)
keep_next_row = True
return results
"""
Load filters file
"""
def init_filters(filter_file):
return [line.rstrip('\n') for line in open(filter_file, encoding="utf8")]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Optional app description')
parser.add_argument('--input', type=str,
help='Original M3U file')
parser.add_argument('--output', type=str,
help='Resulting filtered M3U file')
parser.add_argument('--filters', type=str,
help='Filter (txt) file containaing keywords to filter and keep. One keyword by line.')
parser.add_argument('--list_groups', action="store_true",
help='List groups/categories from the M3U file')
args = parser.parse_args()
if args.input:
m3u = M3UFile(args.input)
if args.list_groups:
for line in m3u.groups():
print(line)
if args.input and args.output and args.filters:
keywords = init_filters(args.filters)
# print(keywords)
with open(args.output, "w", encoding="utf8") as output:
for line in m3u.filter(keywords):
# print(line)
output.write(line)
else:
parser.error('You need to pass at least the input file')