forked from Animenosekai/japanterebi-xmltv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher.py
87 lines (73 loc) · 2.35 KB
/
fetcher.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
85
86
"""Get the fetchers for the given channels."""
import argparse
import json
import pathlib
import typing
from xml.dom.minidom import Element, parse
import tqdm
from model import Channel
def get_nodes(site: pathlib.Path) -> typing.Iterable[Element]:
"""
Get the channels for the given site.
Parameters
----------
site: Path
The path to the site.
Returns
-------
Iterable
typing.Iterable[str]
"""
for element in site.iterdir():
if element.name.endswith(".channels.xml"):
with element.open() as file:
dom = parse(file)
for node in dom.getElementsByTagName("channel"):
yield node
def main(
sites: pathlib.Path, channels: list[Channel], progress: bool = False
) -> typing.Iterable[str]:
"""
Get the fetchers for the given channels.
Parameters
----------
sites: Path
The path to the fetchers.
channels: list
The list of channels.
progress: bool, default = True
Returns
-------
Iterable
typing.Iterable[pathlib.Path]
"""
ids = set((channel.id for channel in channels))
for site in tqdm.tqdm(sites.iterdir(), disable=not progress):
if site.is_dir():
for node in get_nodes(site):
if node.getAttribute("xmltv_id") in ids:
yield node.toxml()
def entry():
"""The main entrypoint for the script."""
parser = argparse.ArgumentParser(prog="fetcher", description="Fetch channels")
parser.add_argument(
"--input", "-i", help="The channels list", type=pathlib.Path, required=True
)
parser.add_argument(
"--sites", "-s", help="The site fetchers", type=pathlib.Path, required=True
)
parser.add_argument("output", default="-", help="The output path", nargs="?")
args = parser.parse_args()
stdout = not (args.output and args.output != "-")
decoded = json.loads(pathlib.Path(args.input).read_text())
channels = [Channel(**channel) for channel in decoded]
sites = main(args.sites, channels, progress=not stdout)
result = '<?xml version="1.0" encoding="UTF-8"?>\n<channels>\n {channel}\n</channels>'.format(
channel="\n ".join(sites)
)
if stdout:
print(result)
else:
pathlib.Path(args.output).write_text(result)
if __name__ == "__main__":
entry()