Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SpotifyBackup
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Robert Goldmann
SpotifyBackup
Commits
aeb63f39
Commit
aeb63f39
authored
1 year ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
SpotifyRecorder: added settings to specify start index and limit
parent
9b062917
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
SpotifyRecorder.py
+29
-19
29 additions, 19 deletions
SpotifyRecorder.py
config/settings-recorder-example.json
+2
-0
2 additions, 0 deletions
config/settings-recorder-example.json
with
31 additions
and
19 deletions
SpotifyRecorder.py
+
29
−
19
View file @
aeb63f39
...
...
@@ -26,7 +26,9 @@ class SpotifyRecorder:
playlist
:
Dict
[
str
,
str
],
spotifyDeviceName
:
str
,
audioDeviceName
:
str
,
destinationFolder
:
str
):
destinationFolder
:
str
,
startNumber
:
int
=
0
,
limit
:
int
=
-
1
):
self
.
_clientID
=
clientID
self
.
_clientSecret
=
clientSecret
self
.
_redirectUrl
=
redirectUrl
...
...
@@ -36,11 +38,11 @@ class SpotifyRecorder:
self
.
_spotifyDeviceName
=
spotifyDeviceName
self
.
_audioDeviceName
=
audioDeviceName
self
.
_destinationFolder
=
destinationFolder
self
.
_startNumber
=
startNumber
self
.
_limit
=
limit
os
.
makedirs
(
self
.
_destinationFolder
,
exist_ok
=
True
)
# TODO: options specify range (start / stop track) or only offset index + limit
self
.
_spotify
=
self
.
login
()
def
login
(
self
)
->
spotipy
.
Spotify
:
...
...
@@ -53,16 +55,14 @@ class SpotifyRecorder:
return
spotipy
.
Spotify
(
client_credentials_manager
=
client_credentials_manager
)
def
run
(
self
):
LOGGER
.
info
(
f
'
>>> Fetching all tracks for playlist
{
self
.
_playlist
[
"
name
"
]
}
...
'
)
allTracks
=
[]
LOGGER
.
info
(
f
'
Fetching all tracks for playlist
{
self
.
_playlist
[
"
name
"
]
}
...
'
)
playlist
=
self
.
__get_playlist
(
self
.
_playlist
[
'
user
'
],
self
.
_playlist
[
'
id
'
])
allTracks
.
extend
(
self
.
__get_tracks
(
playlist
))
LOGGER
.
info
(
f
'
>>> Found
{
len
(
allTracks
)
}
tracks
'
)
tracks
=
self
.
__get_tracks
(
playlist
)
self
.
__record_tracks
(
allT
racks
)
self
.
__record_tracks
(
t
racks
)
def
__get_playlist
(
self
,
username
:
str
,
playlistID
:
str
)
->
Dict
:
LOGGER
.
info
(
f
'
>>>
Fetching playlist with ID:
{
playlistID
}
by
{
username
}
...
'
)
LOGGER
.
info
(
f
'
Fetching playlist with ID:
{
playlistID
}
by
{
username
}
...
'
)
identifier
=
f
'
spotify:user:
{
username
}
:playlist:
{
playlistID
}
'
playlist
=
self
.
_spotify
.
playlist
(
identifier
)
LOGGER
.
info
(
f
'
Found playlist
"
{
playlist
[
"
name
"
]
}
"'
)
...
...
@@ -76,7 +76,7 @@ class SpotifyRecorder:
tracks
=
self
.
_spotify
.
next
(
tracks
)
results
.
extend
(
tracks
[
'
items
'
])
LOGGER
.
info
(
f
'
F
etche
d
{
len
(
result
s
)
}
tracks
'
)
LOGGER
.
info
(
f
'
F
oun
d
{
len
(
track
s
)
}
tracks
in playlist
'
)
return
results
def
__extract_track_uris
(
self
,
tracks
:
List
)
->
List
[
str
]:
...
...
@@ -88,18 +88,26 @@ class SpotifyRecorder:
recordedTracks
=
[]
skippedTracks
=
[]
errorTracks
=
[]
for
index
,
track
in
enumerate
(
tracks
[:
2
]):
if
self
.
_limit
==
-
1
:
LOGGER
.
info
(
f
'
Recording track #
{
self
.
_startNumber
}
to all
'
)
tracks
=
tracks
[
self
.
_startNumber
:]
else
:
LOGGER
.
info
(
f
'
Recording track #
{
self
.
_startNumber
}
to (including) #
{
self
.
_startNumber
+
self
.
_limit
-
1
}
'
)
tracks
=
tracks
[
self
.
_startNumber
:
self
.
_startNumber
+
self
.
_limit
]
for
index
,
track
in
enumerate
(
tracks
):
if
track
[
'
is_local
'
]:
# TODO:
# It's not possible to add a local track to a playlist using the web API.
# https://github.com/plamere/spotipy/issues/793#issuecomment-1082421408
LOGGER
.
info
(
f
'
Skipping local track
"
{
track
[
"
track
"
][
"
name
"
]
}
"'
)
skippedTracks
.
append
(
track
[
'
track
'
][
'
name
'
])
continue
LOGGER
.
info
(
f
'
Recording track
{
index
+
1
}
/
{
len
(
tracks
)
}
:
"
{
track
[
"
track
"
][
"
name
"
]
}
"
...
'
)
indexInPlaylist
=
self
.
_startNumber
+
index
LOGGER
.
info
(
f
'
>>> Recording track
{
index
+
1
}
/
{
len
(
tracks
)
}
: #
{
indexInPlaylist
}
"
{
track
[
"
track
"
][
"
name
"
]
}
"
...
'
)
try
:
filePath
=
self
.
__determine_file_path
(
index
+
1
,
track
)
filePath
=
self
.
__determine_file_path
(
index
InPlaylist
+
1
,
track
)
recorder
=
SpotifyAudioRecorder
(
self
.
_audioDeviceName
,
filePath
)
with
recorder
.
record
():
self
.
__play_track
(
deviceId
,
track
[
'
track
'
][
'
uri
'
])
...
...
@@ -123,7 +131,7 @@ class SpotifyRecorder:
def
__wait_for_track_end
(
self
,
track
,
timeWaitedForPlaying
:
float
)
->
None
:
trackDurationInMs
=
track
[
'
track
'
][
'
duration_ms
'
]
trackDurationInSeconds
=
trackDurationInMs
//
1000
LOGGER
.
info
(
f
'
Track duration:
{
self
.
__convert_seconds_to_duration
(
trackDurationInSeconds
)
}
'
)
LOGGER
.
info
(
f
'
\t
Track duration:
{
self
.
__convert_seconds_to_duration
(
trackDurationInSeconds
)
}
'
)
startTime
=
time
.
time
()
...
...
@@ -138,7 +146,7 @@ class SpotifyRecorder:
if
remainingTimeInMs
>
self
.
_THRESHOLD_TRACK_END_IN_MS
:
sleepTime
=
remainingTimeInSeconds
/
2
LOGGER
.
debug
(
f
'
Waiting for track to end (remaining:
'
LOGGER
.
debug
(
f
'
\t\t
Waiting for track to end (remaining:
'
f
'
{
self
.
__convert_seconds_to_duration
(
remainingTimeInSeconds
)
}
,
'
f
'
sleep:
{
self
.
__convert_seconds_to_duration
(
int
(
sleepTime
))
}
s)...
'
)
time
.
sleep
(
sleepTime
)
...
...
@@ -175,13 +183,13 @@ class SpotifyRecorder:
self
.
_spotify
.
start_playback
(
device_id
=
deviceId
,
uris
=
[
trackUri
])
def
__wait_for_track_playing
(
self
)
->
float
:
LOGGER
.
debug
(
f
'
Wait for track to start playing...
'
)
LOGGER
.
debug
(
f
'
\t\t
Wait for track to start playing...
'
)
startTime
=
time
.
time
()
duration
=
0
while
time
.
time
()
-
startTime
<
self
.
_MAX_WAIT_TIME_TRACK_STARTING_IN_S
:
if
self
.
_spotify
.
current_playback
()[
'
is_playing
'
]:
duration
=
time
.
time
()
-
startTime
LOGGER
.
debug
(
f
'
Track started playing after
{
duration
:
.
1
f
}
s
'
)
LOGGER
.
debug
(
f
'
\t\t
Track started playing after
{
duration
:
.
1
f
}
s
'
)
break
time
.
sleep
(
1
)
...
...
@@ -201,7 +209,9 @@ if __name__ == '__main__':
SETTINGS
[
'
playlist
'
],
SETTINGS
[
'
spotifyDeviceName
'
],
SETTINGS
[
'
audioDeviceName
'
],
SETTINGS
[
'
destinationFolder
'
])
SETTINGS
[
'
destinationFolder
'
],
SETTINGS
[
'
startNumber
'
],
SETTINGS
[
'
limit
'
],)
spotifyBackup
.
run
()
...
...
This diff is collapsed.
Click to expand it.
config/settings-recorder-example.json
+
2
−
0
View file @
aeb63f39
...
...
@@ -10,6 +10,8 @@
"spotifyDeviceName"
:
"MYDEVICE"
,
"audioDeviceName"
:
"3/4 - Musik (2- GIGAPort HD Audio driver) [Loopback]"
,
"destinationFolder"
:
""
,
"startNumber"
:
1
,
"limit"
:
-1
,
"redirectUrl"
:
"http://localhost:8080"
,
"openBrowser"
:
false
,
"cacheFilePath"
:
".cache"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment