-
Notifications
You must be signed in to change notification settings - Fork 2
/
tidal.py
183 lines (152 loc) · 7.14 KB
/
tidal.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import shutil
import typing
from io import BytesIO
from pathlib import Path
import requests
from tidal_api import tidalapi
from mutagen import id3
from mutagen.flac import Picture, FLAC, FLACNoHeaderError
def get_track_title(track):
title = track.name.strip() # just in case
# add featuring artists if not already
if not "(feat." in title and len(track.artists) > 1:
title += f' (feat. {" & ".join([x.name for x in track.artists[1:]])})'
# put track version into title
if len(track.artists) > 1:
title += f" [{track.version}]" if track.version else ""
else:
title += f" ({track.version})" if track.version else ""
return title
def download_flac(track: tidalapi.models.Track, file_path, album=None):
if album is None:
album = track.album
url = session.get_media_url(track_id=track.id)
r = requests.get(url, stream=True)
r.raw.decode_content = True
data = BytesIO()
shutil.copyfileobj(r.raw, data)
data.seek(0)
audio = FLAC(data)
# general metatags
audio['artist'] = track.artist.name
audio['title'] = f'{track.name}{f" ({track.version})" if track.version else ""}'
# album related metatags
audio['albumartist'] = album.artist.name
audio['album'] = f'{album.name}{f" ({album.version})" if album.version else ""}'
audio['date'] = str(album.year)
# track/disc position metatags
audio['discnumber'] = str(track.volumeNumber)
audio['disctotal'] = str(album.numberOfVolumes)
audio['tracknumber'] = str(track.trackNumber)
audio['tracktotal'] = str(album.numberOfTracks)
# Tidal sometimes returns null for track copyright
if hasattr(track, 'copyright') and track.copyright:
audio['copyright'] = track.copyright
elif hasattr(album, 'copyright') and album.copyright:
audio['copyright'] = album.copyright
# identifiers for later use in own music libraries
if hasattr(track, 'isrc') and track.isrc:
audio['isrc'] = track.isrc
if hasattr(album, 'upc') and album.upc:
audio['upc'] = album.upc
pic = Picture()
pic.type = id3.PictureType.COVER_FRONT
pic.width = 640
pic.height = 640
pic.mime = 'image/jpeg'
r = requests.get(track.album.image, stream=True)
r.raw.decode_content = True
pic.data = r.raw.read()
audio.add_picture(pic)
data.seek(0)
audio.save(data)
with open(file_path, "wb") as f:
data.seek(0)
shutil.copyfileobj(data, f)
if __name__ == "__main__":
import argparse
p = argparse.ArgumentParser()
p.add_argument('login', help="TIDAL login/email")
p.add_argument('password', help="TIDAL password")
p.add_argument('output_dir', help="output directory (download target)")
p.add_argument('--api_token', help="TIDAL API token", default='kgsOOmYk3zShYrNP')
args = p.parse_args()
config = tidalapi.Config(tidalapi.Quality.lossless)
config.api_token = args.api_token
session = tidalapi.Session(config)
session.login(args.login, args.password)
print("Tidal FLAC ripper")
while True:
folder = Path(args.output_dir)
folder.mkdir(parents=True, exist_ok=True)
print("0) Search for track")
print("1) Download track")
print("2) Download album")
print("3) Download playlist")
mode = input("Select mode: ")
# TODO: download queue
# TODO: search for album
# TODO: search for artist
# TODO: search for playlist
try:
if mode == "0":
search_query = input("Enter search query: ")
search = session.search(field='track', value=search_query)
for track in search.tracks:
# TODO: selector to download
print(f"{track.id}: {track.artist.name} - {track.name}")
elif mode == "1":
track_id = input("Enter track id: ")
track = session.get_track(track_id, withAlbum=True)
track_name = f'{track.name}{f" ({track.version})" if track.version else ""}'
print(f'Downloading track: {track.artist.name} - {track_name}')
download_flac(track, folder / f'{track.artist.name} - {track_name}.flac'.replace("/", "_"))
print("Track downloaded!")
elif mode == "2":
album_id = input("Enter album id: ")
album = session.get_album(album_id=album_id) # type: tidalapi.models.Album
print(f'Downloading album: {album.artist.name} - {album.name}')
tracks = session.get_album_tracks(album_id=album_id) # type: typing.Iterable[tidalapi.models.Track]
num = 0
# TODO: handle multicd albums better (separate dirs and playlists?)
discs = max(map(lambda x: x.disc_num, tracks))
folder = folder / album.artist.name.replace("/", "_") / f'{f"({album.release_date.year}) " if album.release_date is not None else ""}{album.name}'.replace("/", "_")
folder.mkdir(parents=True, exist_ok=True)
with open(folder / f'00. {album.name.replace("/", "_")}.m3u', 'w') as playlist_file:
for track in tracks:
num += 1
track_name = f'{track.name}{f" ({track.version})" if track.version else ""}'
print(f'Downloading ({num}/{album.num_tracks}): {track_name}')
fname = f'{str(track.track_num).zfill(2)}. {track_name.replace("/", "_")}.flac'
download_flac(track, folder / fname, album=album)
playlist_file.write(fname)
playlist_file.write("\n")
print("Album downloaded!")
elif mode == "3":
playlist_id = input("Enter playlist id: ")
playlist = session.get_playlist(playlist_id=playlist_id)
tracks = session.get_playlist_tracks(playlist_id=playlist_id)
print(f'Downloading playlist: {playlist.name}')
num = 0
folder = folder / playlist.name.replace("/", "_")
folder.mkdir(parents=True, exist_ok=True)
with open(folder / f'{playlist.name.replace("/", "_")}.m3u', "w") as playlist_file:
for track in tracks:
num += 1
track_name = f'{track.name}{f" ({track.version})" if track.version else ""}'
print(f'Downloading ({num}/{playlist.num_tracks}): {track_name}')
fname = f'{track.artist.name} - {track_name}.flac'.replace("/", "_")
download_flac(track, folder / fname)
playlist_file.write(fname)
playlist_file.write("\n")
print("Playlist downloaded!")
else:
print("Incorrect mode!")
except FLACNoHeaderError:
print("This track is not available in lossless quality, abandoning")
except Exception as e:
print(f"Error occured: {e}")
if input("Do you want to continue? [y/n] ") != "y":
break