-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.py
More file actions
222 lines (178 loc) · 7.02 KB
/
main.py
File metadata and controls
222 lines (178 loc) · 7.02 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import json
import logging
from logging import getLogger
from typing import Optional
import click
from tabulate import tabulate
from os import environ
from .visitor import Visitor
from .utils import range_expand
from .__version__ import __version__
logger = getLogger(__name__)
class Context:
def __init__(self):
self.token: Optional[str] = None
self.visitor: Optional[Visitor] = None
# def _print_table(list):
# table = tabulate()
# click.echo(table)
@click.group()
@click.option("-t", "--token", help="API token.")
@click.option("-v", "--verbosity", default="INFO", help="Logging level.")
@click.version_option(__version__)
@click.pass_context
def main(ctx: click.Context, **argv):
verbosity = argv.pop("verbosity").upper()
logging.basicConfig(format='%(asctime)s %(message)s', level=verbosity)
token = environ.get("VISTOPIA_API_TOKEN", None)
token = argv.get("token", None) or token
logger.debug(f"API token `{token}` received.")
ctx.obj = Context()
ctx.obj.visitor = Visitor(token=token)
@main.command("search", help="搜索节目")
@click.option("--keyword", "-k", type=click.STRING, required=True,
help="Search keyword.")
@click.pass_context
def search(ctx: click.Context, **argv):
visitor: Visitor = ctx.obj.visitor
search_result_list = visitor.search(argv.pop("keyword"))
logger.debug(json.dumps(search_result_list, indent=2, ensure_ascii=False))
table = []
for item in search_result_list:
if item["data_type"] != "content":
continue
author = item["author"]
if item["subtitle"]:
title = "%s: %s" % ([item['title'], item['subtitle']])
else:
title = item['title']
desc = item['share_desc']
content_id = item['id']
table.append((content_id, author, title, desc))
click.echo(tabulate(table))
@main.command("subscriptions", help="列出所有已订阅节目")
@click.pass_context
def subscriptions(ctx: click.Context):
visitor: Visitor = ctx.obj.visitor
logger.debug(visitor.get_user_subscriptions_list())
table = []
for show in visitor.get_user_subscriptions_list():
title = ": ".join([show['title'], show['subtitle']])
content_id = show["content_id"]
table.append((content_id, title))
click.echo(tabulate(table))
@main.command("show-content", help="节目章节信息")
@click.option("--id", type=click.INT, required=True)
@click.pass_context
def show_content(ctx: click.Context, **argv):
visitor: Visitor = ctx.obj.visitor
content_id = argv.pop("id")
logger.debug(visitor.get_content_show(content_id))
logger.debug(json.dumps(
visitor.get_catalog(content_id), indent=2, ensure_ascii=False))
table = []
catalog = visitor.get_catalog(content_id)
for part in catalog["catalog"]:
for article in part["part"]:
table.append((
article["sort_number"],
# article["article_id"],
article["title"],
article["duration_str"],
))
click.echo(f"Title: {catalog['title']}")
click.echo(f"Author: {catalog['author']}")
click.echo(f"Type: {catalog['type']}")
click.echo(tabulate(table))
@main.command("batch-save", help="批量下载专辑节目及文稿")
@click.option("--id", required=True, help="Show ID in the form '1-3,4,8'")
@click.option("--single-file-exec-path", type=click.Path(),
help="Path to the single-file CLI tool")
@click.option("--cookie-file-path", type=click.Path(),
help=(
"Path to the browser cookie file "
"(only needed in single-file mode)"))
@click.pass_context
def batch_save(ctx: click.Context, **argv):
visitor: Visitor = ctx.obj.visitor
album_id = argv.pop("id")
single_file_exec_path = argv.pop("single_file_exec_path")
cookie_file_path = argv.pop("cookie_file_path")
albums = set(range_expand(album_id) if album_id else [])
logger.debug(f"albums: {albums}")
for content_id in albums:
logger.debug(f"album: {content_id}")
logger.debug(visitor.get_content_show(content_id))
logger.debug(json.dumps(
visitor.get_catalog(content_id), indent=2, ensure_ascii=False))
catalog = visitor.get_catalog(content_id)
if catalog is None or catalog["type"] == "free":
continue
print(f"Saving show: [{content_id}]-{catalog['title']}")
ctx.obj.visitor.save_show(
content_id,
no_tag=False,
episodes=None
)
if single_file_exec_path and cookie_file_path:
ctx.obj.visitor.save_transcript_with_single_file(
content_id,
episodes=None,
single_file_exec_path=single_file_exec_path,
cookie_file_path=cookie_file_path
)
else:
ctx.obj.visitor.save_transcript(
content_id,
episodes=None
)
return
@main.command("save-show", help="保存节目至本地,并添加封面和 ID3 信息")
@click.option("--id", type=click.INT, required=True)
@click.option("--no-tag", is_flag=True, default=False,
help="Do not add IDv3 tags.")
@click.option("--episode-id", help="Episode ID in the form '1-3,4,8'")
@click.pass_context
def save_show(ctx: click.Context, **argv):
content_id = argv.pop("id")
episode_id = argv.pop("episode_id", None)
episodes = set(range_expand(episode_id) if episode_id else [])
logger.debug(json.dumps(
ctx.obj.visitor.get_catalog(content_id), indent=2, ensure_ascii=False))
ctx.obj.visitor.save_show(
content_id,
no_tag=argv.pop("no_tag"),
episodes=episodes,
)
@main.command("save-transcript", help="保存节目文稿至本地")
@click.option("--id", type=click.INT, required=True)
@click.option("--episode-id", help="Episode ID in the form '1-3,4,8'")
@click.option("--single-file-exec-path", type=click.Path(),
help="Path to the single-file CLI tool")
@click.option("--cookie-file-path", type=click.Path(),
help=(
"Path to the browser cookie file "
"(only needed in single-file mode)"))
@click.pass_context
def save_transcript(ctx: click.Context, **argv):
content_id = argv.pop("id")
episode_id = argv.pop("episode_id", None)
single_file_exec_path = argv.pop("single_file_exec_path")
cookie_file_path = argv.pop("cookie_file_path")
episodes = set(range_expand(episode_id) if episode_id else [])
logger.debug(json.dumps(
ctx.obj.visitor.get_catalog(content_id), indent=2, ensure_ascii=False))
if single_file_exec_path and cookie_file_path:
ctx.obj.visitor.save_transcript_with_single_file(
content_id,
episodes=episodes,
single_file_exec_path=single_file_exec_path,
cookie_file_path=cookie_file_path
)
else:
ctx.obj.visitor.save_transcript(
content_id,
episodes=episodes
)
if __name__ == "__main__":
main()