-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathsubtitle_downloader.py
executable file
·151 lines (135 loc) · 4.73 KB
/
subtitle_downloader.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
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
#!/usr/bin/python3
# coding: utf-8
"""
This module is to download subtitle from stream services.
"""
from __future__ import annotations
import argparse
import logging
from logging import INFO, DEBUG
from datetime import datetime
import os
import sys
import validators
from configs.config import app_name, __version__, directories, filenames
from constants import Service
from services import service_map
from utils.helper import get_locale
from utils.io import load_toml
def main() -> None:
_ = get_locale('main')
support_services = ', '.join(sorted([v for k, v in Service.__dict__.items()
if not k.startswith("__")], key=str.lower))
parser = argparse.ArgumentParser(
description=_("Support downloading subtitles from multiple streaming services, such as {} ,and etc.").format(
support_services),
add_help=False)
parser.add_argument('url',
help=_("series's/movie's url"))
parser.add_argument('-s',
'--season',
dest='season',
help=_("download season [0-9]"))
parser.add_argument('-e',
'--episode',
dest='episode',
help=_("download episode [0-9]"))
parser.add_argument('-l',
'--last-episode',
dest='last_episode',
action='store_true',
help=_("download the latest episode"))
parser.add_argument('-o',
'--output',
dest='output',
help=_("output directory"))
parser.add_argument('-slang',
'--subtitle-language',
dest='subtitle_language',
help=_("languages of subtitles; use commas to separate multiple languages"))
parser.add_argument('-alang',
'--audio-language',
dest='audio_language',
help=_("languages of audio-tracks; use commas to separate multiple languages"))
parser.add_argument('-sf',
'--subtitle-format',
dest='subtitle_format',
help=_("subtitles format: .srt or .ass"))
parser.add_argument(
'-locale',
'--locale',
dest='locale',
help=_("interface language"),
)
parser.add_argument('-p',
'--proxy',
dest='proxy',
nargs='?',
help=_("proxy"))
parser.add_argument(
'-d',
'--debug',
action='store_true',
help=_("enable debug logging"),
)
parser.add_argument(
'-h',
'--help',
action='help',
default=argparse.SUPPRESS,
help=_("show this help message and exit")
)
parser.add_argument(
'-v',
'--version',
action='version',
version=f'{app_name} {__version__}',
help=_("app's version")
)
args = parser.parse_args()
if args.debug:
os.makedirs(directories.logs, exist_ok=True)
log_time = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
log_file_path = str(filenames.log).format(
app_name=app_name, log_time=log_time)
logging.basicConfig(
format='%(asctime)s - %(name)s - %(lineno)d - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG,
handlers=[
logging.FileHandler(log_file_path, encoding='utf-8'),
logging.StreamHandler()
]
)
else:
logging.basicConfig(
format='%(message)s',
level=logging.INFO,
)
start = datetime.now()
if not validators.url(args.url):
logging.warning(
_("\nPlease input correct url!"))
sys.exit(0)
service = next((service for service in service_map
if service['domain'] in args.url), None)
if service:
log = logging.getLogger(service['class'].__module__)
if args.debug:
log.setLevel(DEBUG)
else:
log.setLevel(INFO)
service_config = load_toml(
str(filenames.config).format(service=service['name']))
args.log = log
args.config = service_config
args.service = service
service['class'](args).main()
else:
logging.warning(
_("\nOnly support downloading subtitles from %s ,and etc."), support_services)
sys.exit(1)
logging.info("\n%s took %s seconds", app_name,
int(float((datetime.now() - start).total_seconds())))
if __name__ == "__main__":
main()