-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_single.py
194 lines (161 loc) · 6.07 KB
/
save_single.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
"""
save_single.py - youtubei.py, but for single video ids
Not used in playlist downloading due to redundancy and lag w/ multiple requests
Also: an all in one solution to downloading songs if you prefer, shld work alone w/ minimum requirements
Uses /next (wtf)
"""
import requests
import os
import config
import download
import itunes
import tags
configData = config.load_config()
PLACEHOLDER_WHEN_NO_ALBUM = configData["download_options"]["placeholder_when_no_album"]
NO_ALBUM_PLACEHOLDER_TEXT = configData["download_options"]["no_album_placeholder_text"]
CLIENT_VERSION = configData["download_options"]["youtubei_options"]["client_version"]
CLIENT_NAME = configData["download_options"]["youtubei_options"]["client_name"]
def request_next(videoId: str):
r = requests.post(
url="https://music.youtube.com/youtubei/v1/next",
headers={
"accept": "application/json",
},
json={
"context": {
"client": {
"hl": "en",
"gl": "MY",
"visitorData": "CgtqSnJ2akN1WTlDcyixxYm3BjIKCgJNWRIEGgAgPw%3D%3D",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:130.0) Gecko/20100101 Firefox/130.0,gzip(gfe)",
"clientName": CLIENT_NAME,
"clientVersion": CLIENT_VERSION,
"originalUrl": "https://music.youtube.com/",
},
"user": {"lockedSafetyMode": True},
},
"isAudioOnly": True,
"videoId": f"{videoId}",
"index": 1,
"watchEndpointMusicSupportedConfigs": {
"hasPersistentPlaylistPanel": True,
"musicVideoType": "MUSIC_VIDEO_TYPE_ATV",
},
},
)
return r
def thumbnail_treatment(thumbnailLink: str):
"""
thumbnail_treatment: change thumbnail url to upscale to 1024p
"""
if len(thumbnailLink.split("=")) != 2:
return thumbnailLink
else:
oriThumbnail = thumbnailLink.split("=")[0]
newThumb = f"{oriThumbnail}=w1024"
return newThumb
def get_song_info(videoId: str):
nextData = request_next(videoId=videoId).json()
ytmCheck = nextData["playerOverlays"]["playerOverlayRenderer"][
"browserMediaSession"
]["browserMediaSessionRenderer"]
songDetails = nextData["contents"]["singleColumnMusicWatchNextResultsRenderer"][
"tabbedRenderer"
]["watchNextTabbedResultsRenderer"]["tabs"][0]["tabRenderer"]["content"][
"musicQueueRenderer"
][
"content"
][
"playlistPanelRenderer"
][
"contents"
][
0
][
"playlistPanelVideoRenderer"
]
# check if ytm song
if "album" in ytmCheck:
isYtmSong = True
else:
# not song, stop getting album details
isYtmSong = False
if PLACEHOLDER_WHEN_NO_ALBUM:
noAlbumText = NO_ALBUM_PLACEHOLDER_TEXT
else:
noAlbumText = ""
if isYtmSong:
finalSongInfo = {
"id": videoId,
"title": songDetails["title"]["runs"][0]["text"],
"artist": songDetails["longBylineText"]["runs"][0]["text"],
"album": songDetails["longBylineText"]["runs"][2]["text"],
"releaseTime": songDetails["longBylineText"]["runs"][4]["text"],
"thumbnail": thumbnail_treatment(
songDetails["thumbnail"]["thumbnails"][0]["url"]
),
"isYtmSong": isYtmSong,
}
else:
# use placeholders for non-song
finalSongInfo = {
"id": videoId,
"title": songDetails["title"]["runs"][0]["text"],
"artist": songDetails["longBylineText"]["runs"][0]["text"],
"album": noAlbumText,
"releaseTime": "unknown",
"thumbnail": thumbnail_treatment(
songDetails["thumbnail"]["thumbnails"][0]["url"]
),
"isYtmSong": isYtmSong,
}
return finalSongInfo
def save_single_song(videoId: str):
currentSong = get_song_info(videoId=videoId)
songTitle = currentSong["title"]
songArtist = currentSong["artist"]
songAlbum = currentSong["album"]
songThumbnailUrl = currentSong["thumbnail"]
songReleaseTime = currentSong["releaseTime"]
isYtm = currentSong["isYtmSong"]
print(
f"Video ID: {videoId} | Is YouTube Music song: {isYtm} | Title: {songTitle} | Author: {songArtist} | Album: {songAlbum} | Year: {songReleaseTime} | Thumbnail: {songThumbnailUrl}"
)
download.download_song(id=videoId, playlistId="singles")
downloadedFilePath = f"{config.DEFAULT_SAVES_PATH}/singles/{videoId}.mp3"
# add metadata
tags.add_metadata(
filePath=downloadedFilePath,
songTitle=songTitle,
songArtist=songArtist,
songAlbum=songAlbum,
songThumbnailUrl=songThumbnailUrl,
)
# rename song
os.rename(
f"{config.DEFAULT_SAVES_PATH}/singles/{videoId}.mp3",
f"{config.DEFAULT_SAVES_PATH}/singles/{songTitle}.mp3",
)
print(f"Finished downloading song: {songTitle}!")
# add to itunes
osVersion = itunes.check_os_version()
finalSinglePath = f"{config.DEFAULT_SAVES_PATH}/singles/{songTitle}.mp3"
if configData["itunes_options"]["add_to_itunes"]:
if osVersion == "darwin" or osVersion == "win32" or osVersion == "cygwin":
if osVersion == "darwin":
itunes.add_single_itunes(
singlePath=f"{finalSinglePath}",
osVersion="darwin",
)
else:
print(f"Add to iTunes coming soon! Songs saved at {finalSinglePath}")
exit()
else:
print("Device does not support iTunes/Apple Music! Exiting now...")
exit()
if configData["download_options"]["open_in_finder_after_download"]:
download.open_dir(
osVersion=osVersion, savesPath=f"{config.DEFAULT_SAVES_PATH}/singles/"
)
print(f"Download finished! Song can be found in {finalSinglePath}")
# save_single_song(input("D: "))