-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundcli.py
130 lines (121 loc) · 4.52 KB
/
soundcli.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
#!/usr/bin/python2
# encoding=utf8
# pylint: disable=print-statement
import soundcloud
import os
import sys
import getpass
import string
reload(sys)
sys.setdefaultencoding('utf8')
if not sys.platform.startswith('linux'):
print("This script can only be run under Linux.")
sys.exit(0)
tracks = None
stream_url = None
thist = None
client = soundcloud.Client(client_id='LBCcHmRB8XSStWL6wKH2HPACspQlXg2P')
def error_noquery():
print("No query sent! Please search for a song.")
def error_nosong():
print("You haven't played any song yet.")
if not os.path.isdir("/home/" + getpass.getuser() + "/Downloads"):
os.system("mkdir ~/Downloads")
def download():
try:
printable = set(string.printable)
artist = filter(lambda x: x in printable, thist.title.split(" - ")[0])
printable = set(string.printable)
title = filter(lambda x: x in printable, thist.title.split(" - ")[1])
if x == "":
os.system("wget \"" + stream_url.location + "\" -O .cache.mp3")
os.system("sacad \"" + artist + "\" \"" + title + "\" 800 .cover.jpg")
os.system("lame -V0 --ti .cover.jpg --ta \"" + artist + "\" --tt \"" + title + "\" .cache.mp3 \"/home/" + getpass.getuser() + "/Downloads/" + filename + "\"")
os.system("rm .cover.jpg")
os.system("rm .cache.mp3")
else:
os.system("wget \"" + stream_url.location + "\" -O .cache.mp3")
os.system("sacad " + artist + " " + title + " 800 .cover.jpg")
os.system("lame -V0 --ti .cover.jpg --ta \"" + artist + "\" --tt \"" + title + "\" .cache.mp3 \"" + filename + "\"")
os.system("rm .cover.jpg")
os.system("rm .cache.mp3")
except:
printable = set(string.printable)
artist = filter(lambda x: x in printable, thist.user['username'])
printable = set(string.printable)
title = filter(lambda x: x in printable, thist.title)
if x == "":
os.system("wget \"" + stream_url.location + "\" -O .cache.mp3")
os.system("sacad \"" + artist + "\" \"" + title + "\" 800 .cover.jpg")
os.system("lame -V0 --ti .cover.jpg --ta \"" + artist + "\" --tt \"" + title + "\" .cache.mp3 \"/home/" + getpass.getuser() + "/Downloads/" + filename + "\"")
os.system("rm .cover.jpg")
os.system("rm .cache.mp3")
else:
os.system("wget \"" + stream_url.location + "\" -O .cache.mp3")
os.system("sacad \"" + artist + "\" \"" + title + "\" 800 .cover.jpg")
os.system("lame -V0 --ti .cover.jpg --ta \"" + artist + "\" --tt \"" + title + "\" .cache.mp3 \"" + filename + "\"")
os.system("rm .cover.jpg")
os.system("rm .cache.mp3")
print("soundcli 0.2")
print("(C) 2018 koyu.space Developers")
print
while True:
sys.stdout.write("soundcli> ")
sys.stdout.flush()
try:
x = raw_input()
except:
sys.exit(0)
counter = 0
if x.startswith("search "):
tracks = client.get('/tracks', q=x.split("search ")[1])
for track in tracks:
counter = counter + 1
print(str(counter) + ": " + track.title)
if x.startswith("play "):
try:
thist = None
for track in tracks:
counter = counter + 1
if str(counter) == x.split("play ")[1]:
thist = track
stream_url = client.get(thist.stream_url, allow_redirects=False)
os.system("mpv \"" + stream_url.location + "\"")
except:
error_noquery()
if x == "list" or x == "ls":
if not tracks == None:
for track in tracks:
counter = counter + 1
print(str(counter) + ": " + track.title)
else:
error_noquery()
if x.startswith("download"):
if x == "download":
if not stream_url == None:
filename = thist.title.replace(" ", "_").replace("!", "_").replace("&", "_").replace("?", "_") + ".mp3"
sys.stdout.write("Download location (Enter for ~/Downloads): ")
sys.stdout.flush()
x = raw_input()
download()
else:
error_nosong()
else:
for track in tracks:
counter = counter + 1
if str(counter) == x.split("download ")[1]:
thist = track
stream_url = client.get(thist.stream_url, allow_redirects=False)
filename = thist.title.replace(" ", "_").replace("!", "_").replace("&", "_").replace("?", "_") + ".mp3"
sys.stdout.write("Download location (Enter for ~/Downloads): ")
sys.stdout.flush()
x = raw_input()
download()
if x == "help":
print
print("soundcli help:")
print("search <query>: searchs a song on soundcloud")
print("ls/list: lists the songs again")
print("play <#song>: plays song from list")
print("download [#song]: Downloads the given song")
print