-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (97 loc) · 4.42 KB
/
main.py
File metadata and controls
118 lines (97 loc) · 4.42 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
import argparse
import pytmdl.utils as utils
from pytmdl.ytsong import YTSong, SongUnavailable
from pytmdl.ytalbum import YTAlbum, NotAnAlbum
class PYTMDL:
"""
Main class for the CLI program.
This class handles argument parsing, validation, and execution of the program.
"""
def __init__(self):
"""
Initialize the PYTMDL object with default values.
Sets up the version, language, output directory, and skip metadata flag.
Loads the language dictionary based on the system's locale or a default language
if the system's locale is not supported. Default "EN" (English).
Initializes the argument parser with the application description.
"""
self.version = 0.1
self.default_language = "EN"
self.output_dir = "~"
self.skip_metadata = False
try:
self.lang_dict = utils.load_language(utils.get_language_from_locale())
except utils.LanguageNotFound:
self.lang_dict = utils.load_language(self.default_language)
self.parser = argparse.ArgumentParser(
add_help=False,
description=self.lang_dict["app_description"]
)
def parse_arguments(self):
"""
Parse the command-line arguments.
Adds several argument options to the parser:
- url: positional argument for one or more URLs
- help: flag to display help message
- version: flag to display version information
- skip-metadata: flag to skip downloading metadata
- output: optional argument for specifying the output directory
- language: optional argument for specifying the language
Returns:
parsed arguments object
"""
self.parser.add_argument("url", nargs="*")
self.parser.add_argument("-h", "--help", action="store_true")
self.parser.add_argument("-v", "--version", action="store_true")
self.parser.add_argument("-s", "--skip-metadata", action="store_true")
self.parser.add_argument("-o", "--output", nargs=1)
self.parser.add_argument("-l", "--language")
return self.parser.parse_args()
def validate_arguments(self, arguments):
"""
Validate and process the parsed arguments.
- If a language is provided, it loads the corresponding language dictionary.
- If an output directory is provided, it updates the output directory for the program.
- Sets the skip metadata flag based on user input.
- Handles help and version flags by printing respective messages and exiting.
- Iterates through each URL and attempts to download a song or album using YTSong or YTAlbum.
Args:
arguments: parsed arguments object
"""
if arguments.language:
try:
# This will load the chosen language. If the language argument is provided,
# the language will be loaded twice: once when the program begins execution
# and the second time if the user is choosing a language
self.lang_dict = utils.load_language(arguments.language)
# If the provided language does not have a translation, do nothing
except FileNotFoundError:
pass
if arguments.output:
self.output_dir = arguments.output[0]
if arguments.skip_metadata:
self.skip_metadata = True
if arguments.help:
print(self.lang_dict["help_message"] % self.lang_dict["app_description"])
elif arguments.version:
print(self.lang_dict["version_text"] % str(self.version))
for url in arguments.url:
try:
YTSong(url,
output_dir=self.output_dir,
skip_metadata=self.skip_metadata,
lang_dict=self.lang_dict
).download()
except SongUnavailable:
try:
YTAlbum(url,
output_dir=self.output_dir,
skip_metadata=self.skip_metadata,
lang_dict=self.lang_dict
).download()
except NotAnAlbum:
print(self.lang_dict["wrong_url"])
if __name__ == "__main__":
pytmdl = PYTMDL()
arguments = pytmdl.parse_arguments()
pytmdl.validate_arguments(arguments)