-
Notifications
You must be signed in to change notification settings - Fork 43
/
soundcloud-dl.py
142 lines (97 loc) · 3.07 KB
/
soundcloud-dl.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
#!/usr/bin/env python3
import re
import requests
import urllib.request
import json
import codecs
import os
import sys
import argparse
import eyed3
import taglib
# btw, this is not my id
CLIENTID="Oa1hmXnTqqE7F2PKUpRdMZqWoguyDLV0"
API = "https://api.soundcloud.com/i1/tracks/{0}/streams?client_id={1}"
headers = {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) '
'Gecko/20100101 Firefox/55.0'
}
def get_id(html):
"""
Getting the ID of the song
"""
try:
song_id = re.findall('soundcloud://sounds:(.*?)"', html)[0]
return song_id
except IndexError:
print("\033[91m✘ Could not find song ID\033[0m")
sys.exit()
def get_tags(html):
"""
Getting the tags so that we can put it into the
music file
"""
title = re.findall('"title":"(.*?)",', html)[0]
title = codecs.getdecoder("unicode_escape")(title)[0]
artist = re.findall('"username":"(.*?)",', html)[0]
artist = codecs.getdecoder("unicode_escape")(artist)[0]
genre = re.findall('"genre":"(.*?)",', html)[0]
genre = codecs.getdecoder("unicode_escape")(genre)[0]
return title, artist, genre
def get_album_art_url(html):
"""
Getting the album art url so that we can download it
and add it to the music file later
"""
return re.findall('img src="(.*?)" width="500"', html)[0]
def tag(fname, title, artist, genre, arturl):
"""
Yes, I know I can do the tagging in a better way.
But I dont know how to. So I would not mind some
help :)
"""
song = taglib.File(fname)
song.tags["ARTIST"] = [artist]
song.tags["TITLE"] = [title]
# Giving the album the same name as
# the title beacause
# I cant find the album name
song.tags["ALBUM"] = [title]
song.tags["GENRE"] = [genre]
song.save()
song = eyed3.load(fname)
imagename = str(title+"500x500.jpg")
image = urllib.request.urlretrieve(arturl, imagename)
print("\033[92m✔ Album art downloaded\033[0m")
imagedata = open(imagename, "rb").read()
song.tag.images.set(3, imagedata, "image/jpeg")
song.tag.save()
# Always leave the place better than you found it ;)
os.remove(imagename)
def main():
parser = argparse.ArgumentParser(description = "Download SoundCloud music at 128kbps with album art and tags")
parser.add_argument('url', action="store", help="URL to the song")
args = parser.parse_args()
r = requests.get(args.url, headers=headers)
print("\033[92m✔ Fetched needed data\033[0m")
html = r.text
song_id = get_id(html)
title = get_tags(html)[0]
artist = get_tags(html)[1]
genre = get_tags(html)[2]
arturl = get_album_art_url(html)
json_url = API.format(song_id, CLIENTID)
data = requests.get(json_url, headers=headers)
data = json.loads(data.text)
# Getting the file url with the best quality
file_url = data["http_mp3_128_url"]
# Example file name --> Adele - Hello.mp3
fname = str(artist+" - "+title+".mp3")
urllib.request.urlretrieve(file_url, fname)
print("\033[92m✔ Downloaded:\033[0m {0} by {1}".format(title, artist))
# Making the file beautiful
tag(fname, title, artist, genre, arturl)
print("\033[92m✔ Saved:\033[0m {}".format(fname))
if __name__=="__main__":
main()