-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyinstaller.py
245 lines (222 loc) · 7.95 KB
/
pyinstaller.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""A version of main.py with current Python syntax just for use with Pyinstaller."""
from __future__ import annotations
import logging
from contextlib import closing
from pathlib import Path
import typer
from cachecontrol import CacheControl
from platformdirs import user_music_path
from typing_extensions import Annotated
from tidal_wave.album import Album
from tidal_wave.artist import Artist
from tidal_wave.login import AudioFormat, LogLevel, login
from tidal_wave.mix import Mix
from tidal_wave.models import (
TidalAlbum,
TidalArtist,
TidalMix,
TidalPlaylist,
TidalTrack,
TidalVideo,
match_tidal_url,
)
from tidal_wave.playlist import Playlist
from tidal_wave.track import Track
from tidal_wave.utils import is_tidal_api_reachable
from tidal_wave.video import Video
__version__ = "2024.11.1"
# https://typer.tiangolo.com/tutorial/options/version/#fix-with-is_eager
def version_callback(value: bool) -> None: # noqa: FBT001
"""Pass this function to typer to specify eager option behavior."""
if value:
print(f"tidal-wave {__version__}") # noqa: T201
raise typer.Exit(code=0)
app = typer.Typer()
_user_music_path: Path = user_music_path()
@app.command()
def main(
tidal_url: Annotated[
str,
typer.Argument(
help="The URL to the TIDAL resource that is desired to retrieve.",
),
],
audio_format: Annotated[
AudioFormat,
typer.Option(case_sensitive=False),
] = AudioFormat.lossless.value,
output_directory: Annotated[
Path,
typer.Argument(
help="The directory under which directory(ies) of files will be written",
),
] = _user_music_path,
loglevel: Annotated[
LogLevel,
typer.Option(case_sensitive=False),
] = LogLevel.info.value,
include_eps_singles: Annotated[ # noqa: FBT002
bool,
typer.Option(
"--include-eps-singles",
help=(
"No-op unless passing TIDAL artist. Whether to include artist's EPs and"
" singles with albums"
),
),
] = False,
no_extra_files: Annotated[ # noqa: FBT002
bool,
typer.Option(
"--no-extra-files",
help=(
"Whether to not even attempt to retrieve artist bio, artist image, "
"album credits, album review, or playlist m3u8"
),
),
] = False,
no_flatten: Annotated[ # noqa: FBT002
bool,
typer.Option(
"--no-flatten",
help=(
"Whether to treat playlists or mixes as a list of tracks/videos and, "
"as such, retrieve them independently"
),
),
] = False,
transparent: Annotated[ # noqa: FBT002
bool,
typer.Option(
"--transparent",
help="Whether to dump JSON responses from TIDAL API; maximum verbosity",
),
] = False,
version: Annotated[ # noqa: ARG001
bool | None,
typer.Option("--version", callback=version_callback, is_eager=True),
] = None,
):
"""Parse command line arguments and retrieve data from TIDAL."""
logging.basicConfig(
format="%(asctime)s,%(msecs)03d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s",
datefmt="%Y-%m-%d:%H:%M:%S",
level=logging.getLevelName(loglevel.value),
)
logger = logging.getLogger(__name__)
tidal_resource: (
TidalAlbum | TidalMix | TidalPlaylist | TidalTrack | TidalVideo | None
) = match_tidal_url(tidal_url)
if tidal_resource is None:
_msg: str = (
f"Cannot parse '{tidal_url}' as a TIDAL album, artist, mix, playlist, "
"track, or video URL"
)
logger.critical(_msg)
raise typer.Exit(code=1)
# Check Internet connectivity, and whether api.tidal.com is up
if not is_tidal_api_reachable():
user_wishes_to_continue: bool = typer.confirm(
"\nEven though tidal-wave cannot seem to connect to the Internet, "
"would you like program execution to continue?",
)
if not user_wishes_to_continue:
raise typer.Exit(code=1)
s, audio_format = login(audio_format=audio_format)
if s is None:
raise typer.Exit(code=1)
with closing(CacheControl(s)) as session:
match tidal_resource:
case TidalTrack():
track: Track = Track(
track_id=tidal_resource.tidal_id,
transparent=transparent,
)
track.get(
session=session,
audio_format=audio_format,
out_dir=output_directory,
no_extra_files=no_extra_files,
)
if loglevel == LogLevel.debug:
track.dump()
raise typer.Exit(code=0)
case TidalAlbum():
album: Album = Album(
album_id=tidal_resource.tidal_id,
transparent=transparent,
)
album.get(
session=session,
audio_format=audio_format,
out_dir=output_directory,
no_extra_files=no_extra_files,
)
if loglevel == LogLevel.debug:
album.dump()
raise typer.Exit(code=0)
case TidalArtist():
artist: Artist = Artist(
artist_id=tidal_resource.tidal_id,
transparent=transparent,
)
artist.get(
session=session,
audio_format=audio_format,
out_dir=output_directory,
include_eps_singles=include_eps_singles,
no_extra_files=no_extra_files,
)
raise typer.Exit(code=0)
case TidalVideo():
video: Video = Video(
video_id=tidal_resource.tidal_id, transparent=transparent
)
video.get(session=session, out_dir=output_directory)
if loglevel == LogLevel.debug:
video.dump()
raise typer.Exit(code=0)
case TidalPlaylist():
playlist: Playlist = Playlist(
playlist_id=tidal_resource.tidal_id,
transparent=transparent,
)
if no_flatten:
playlist.get_elements(
session=session,
audio_format=audio_format,
out_dir=output_directory,
no_extra_files=no_extra_files,
)
else:
playlist.get(
session=session,
audio_format=audio_format,
out_dir=output_directory,
no_extra_files=no_extra_files,
)
if loglevel == LogLevel.debug:
playlist.dump()
raise typer.Exit(code=0)
case TidalMix():
mix: Mix = Mix(mix_id=tidal_resource.tidal_id, transparent=transparent)
if no_flatten:
mix.get_elements(
session=session,
audio_format=audio_format,
out_dir=output_directory,
no_extra_files=no_extra_files,
)
else:
mix.get(
session=session,
audio_format=audio_format,
out_dir=output_directory,
no_extra_files=no_extra_files,
)
if loglevel == LogLevel.debug:
mix.dump()
raise typer.Exit(code=0)
case _:
raise NotImplementedError
app()