forked from nolt/spotifyripper
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspotifyripper.py
executable file
·186 lines (147 loc) · 5.36 KB
/
spotifyripper.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
#! /usr/bin/env python3
# Install the required Python modules
# pip install pulsectl pydub
import dbus
import os
import pprint
import pulsectl
import re
import requests
import subprocess
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
from pydub import AudioSegment
pre_subprocess = None
r = None
pre_album = ""
pre_artist = ""
pre_title = ""
pre_art_url = ""
pre_track_number = ""
pre_file_input = ""
pre_file_cover = ""
file_cover = ""
spotify_sink_index = 0
def get_spotify_sink_index():
with pulsectl.Pulse('spotify') as pulse:
for sink in pulse.sink_input_list():
# print("sink.name:" + sink.name)
# print("sink.corked:" + str(sink.corked))
if (sink.name == "Spotify") and (sink.corked == False):
return sink.index
return 0
def create_directory(path_album):
# print("path_album: " + path_album)
# re.sub("[^a-zA-Z]+", "", path_album)
re.sub("/", "-", path_album)
# print("path_album: " + path_album)
try:
os.makedirs(path_album, exist_ok=True)
except OSError:
print("Creation of the directory %s failed, use /tmp" % path_album)
return "/tmp"
return path_album
def download_cover(art_url, file_cover):
global r
# print("file_cover: " + file_cover)
# print("art_url: " + art_url)
if (art_url != ""):
# if art_url != pre_art_url:
r = requests.get(art_url, allow_redirects=True)
if r != None:
open(file_cover, 'wb').write(r.content)
def spotify_handler(*args):
global pre_album
global pre_artist
global pre_title
global pre_subprocess
global pre_art_url
global pre_track_number
global pre_file_input
global pre_file_cover
global r
global spotify_sink_index
metadata = args[1]["Metadata"]
# debug
# pprint.pprint(metadata)
artist = metadata["xesam:artist"][0]
# print("Artist: " + artist)
#albumArtist = metadata["xesam:albumArtist"][0]
# print("AArtist " + albumArtist)
title = metadata["xesam:title"]
album = metadata["xesam:album"]
track_number = metadata["xesam:trackNumber"]
art_url = metadata["mpris:artUrl"]
disc_number = int(metadata["xesam:discNumber"])
# if albumArtist != "":
# artist = albumArtist
if title != "":
title = title.replace("/", "-")
# workaround for art URL
art_url = art_url.replace("open.spotify.com", "i.scdn.co")
if title != pre_title:
print("Artist: " + artist)
print("Album: " + album)
print("Title: " + str(track_number) + " - " + title)
# print("Cover: " + art_url)
# print("Track: " + str(track_number))
print()
# create dir
path_base = os.path.expanduser("~/Downloads/spotifyripper")
if disc_number > 1:
disc_number_str = str(disc_number) + " "
else:
disc_number_str = ""
path_album = create_directory(path_base + "/" + artist + "/" + disc_number_str + album)
# print("path_album: " + path_album)
# record stream
if pre_subprocess != None:
pre_subprocess.terminate()
file_input = path_album + "/" + str(track_number) + " - " + artist + " - " + title + ".wav"
if spotify_sink_index == 0:
spotify_sink_index = get_spotify_sink_index()
print("spotify_sink_index: " + str(spotify_sink_index))
if (artist != "") or (album != ""):
pre_subprocess = subprocess.Popen(["parec", "--monitor-stream=" + str(spotify_sink_index), "--file-format=wav", file_input])
# convert previous file
if os.path.isfile(pre_file_input):
pre_file_output = pre_file_input.replace(".wav", ".mp3")
sound = AudioSegment.from_wav(pre_file_input)
sound.export(pre_file_output, format="mp3", bitrate="160k", cover=pre_file_cover, tags={
"album": pre_album,
"artist": pre_artist,
"title": pre_title,
"track": int(pre_track_number)
}
)
pre_file_output_size = os.stat(pre_file_output).st_size
if pre_file_output_size < 1048576:
print('\033[33m' + "Warning: small file " + pre_file_output + " \033[0m\n")
if pre_file_output_size > 10485760:
print('\033[33m' + "Warning: large file " + pre_file_output + " \033[0m\n")
# print("DELETE " + pre_file_cover)
os.remove(pre_file_cover)
# print("DELETE " + pre_file_input)
os.remove(pre_file_input)
# download cover
file_cover = file_input.replace(".wav", ".jpg")
download_cover(art_url, file_cover)
if art_url != "":
pre_art_url = art_url
if file_cover != "":
pre_file_cover = file_cover
pre_file_input = file_input
if album != "":
pre_album = album
if artist != "":
pre_artist = artist
if title != "":
pre_title = title
if track_number != "":
pre_track_number = track_number
spotify_sink_index = get_spotify_sink_index()
DBusGMainLoop(set_as_default=True)
session_bus = dbus.SessionBus()
session_bus.add_signal_receiver(spotify_handler, 'PropertiesChanged', None, 'org.mpris.MediaPlayer2.spotify', '/org/mpris/MediaPlayer2')
loop = GLib.MainLoop()
loop.run()