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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Robert Goldmann
SpotifyBackup
Commits
caa80a8a
Commit
caa80a8a
authored
5 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
added option to delete old files after certain days
parent
b0b25203
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
SpotifyBackup.py
+24
-3
24 additions, 3 deletions
SpotifyBackup.py
settings-example.json
+1
-0
1 addition, 0 deletions
settings-example.json
with
25 additions
and
3 deletions
SpotifyBackup.py
+
24
−
3
View file @
caa80a8a
...
@@ -3,7 +3,7 @@ import json
...
@@ -3,7 +3,7 @@ import json
import
logging
import
logging
import
os
import
os
import
sys
import
sys
from
datetime
import
datetime
from
datetime
import
datetime
,
timedelta
import
spotipy
import
spotipy
from
spotipy.oauth2
import
SpotifyClientCredentials
from
spotipy.oauth2
import
SpotifyClientCredentials
...
@@ -36,6 +36,7 @@ LOGGER = prepare_logging()
...
@@ -36,6 +36,7 @@ LOGGER = prepare_logging()
class
SpotifyBackup
:
class
SpotifyBackup
:
DATE_FORMAT
=
'
%Y-%m-%d_%H-%M-%S
'
DATE_FORMAT
=
'
%Y-%m-%d_%H-%M-%S
'
DATE_FORMAT_SHORT
=
'
%Y-%m-%d
'
INVALID_CHARS
=
[
'
'
,
'
\t
'
,
'
\n
'
]
INVALID_CHARS
=
[
'
'
,
'
\t
'
,
'
\n
'
]
REPLACE_CHAR
=
'
_
'
REPLACE_CHAR
=
'
_
'
...
@@ -53,12 +54,12 @@ class SpotifyBackup:
...
@@ -53,12 +54,12 @@ class SpotifyBackup:
exportPath
=
self
.
__determine_export_path
(
playlist
[
'
name
'
])
exportPath
=
self
.
__determine_export_path
(
playlist
[
'
name
'
])
LOGGER
.
info
(
f
'
Exporting tracks to
"
{
exportPath
}
"
...
'
)
LOGGER
.
info
(
f
'
>>>
Exporting tracks to
"
{
exportPath
}
"
...
'
)
self
.
__export_csv
(
exportPath
,
tracks
)
self
.
__export_csv
(
exportPath
,
tracks
)
LOGGER
.
info
(
'
Exporting DONE
'
)
LOGGER
.
info
(
'
Exporting DONE
'
)
def
__get_playlist
(
self
,
username
:
str
,
playlistID
:
str
):
def
__get_playlist
(
self
,
username
:
str
,
playlistID
:
str
):
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
}
'
identifier
=
f
'
spotify:user:
{
username
}
:playlist:
{
playlistID
}
'
playlist
=
self
.
_spotify
.
playlist
(
identifier
)
playlist
=
self
.
_spotify
.
playlist
(
identifier
)
LOGGER
.
info
(
f
'
Found playlist
"
{
playlist
[
"
name
"
]
}
"'
)
LOGGER
.
info
(
f
'
Found playlist
"
{
playlist
[
"
name
"
]
}
"'
)
...
@@ -101,6 +102,24 @@ class SpotifyBackup:
...
@@ -101,6 +102,24 @@ class SpotifyBackup:
except
Exception
:
except
Exception
:
LOGGER
.
exception
(
f
'
Error while exporting track
"
{
track
}
"'
)
LOGGER
.
exception
(
f
'
Error while exporting track
"
{
track
}
"'
)
def
clean_old_exports
(
self
,
daysToKeep
):
if
not
daysToKeep
:
LOGGER
.
info
(
'
Skipping deletion of old files
'
)
return
minimumDate
=
datetime
.
today
()
-
timedelta
(
days
=
int
(
daysToKeep
))
LOGGER
.
info
(
f
'
>>> Deleting files older than
{
minimumDate
}
(
{
daysToKeep
}
days)
'
)
files
=
[
file
for
file
in
sorted
(
os
.
listdir
(
self
.
_exportFolder
))
if
os
.
path
.
isfile
(
os
.
path
.
join
(
self
.
_exportFolder
,
file
))]
for
file
in
files
:
parts
=
file
.
split
(
'
_
'
)
creationDate
=
datetime
.
strptime
(
parts
[
0
],
self
.
DATE_FORMAT_SHORT
)
if
creationDate
<
minimumDate
:
LOGGER
.
info
(
f
'
Removing old file
"
{
file
}
"'
)
os
.
remove
(
os
.
path
.
join
(
self
.
_exportFolder
,
file
))
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
with
open
(
'
settings.json
'
,
'
r
'
,
encoding
=
'
utf-8
'
)
as
f
:
with
open
(
'
settings.json
'
,
'
r
'
,
encoding
=
'
utf-8
'
)
as
f
:
...
@@ -110,6 +129,8 @@ if __name__ == '__main__':
...
@@ -110,6 +129,8 @@ if __name__ == '__main__':
SETTINGS
[
'
spotifyAPI
'
][
'
clientSecret
'
],
SETTINGS
[
'
spotifyAPI
'
][
'
clientSecret
'
],
SETTINGS
[
'
exportFolder
'
])
SETTINGS
[
'
exportFolder
'
])
spotifyBackup
.
clean_old_exports
(
SETTINGS
[
'
daysToKeep
'
])
for
playlist
in
SETTINGS
[
'
playlists
'
]:
for
playlist
in
SETTINGS
[
'
playlists
'
]:
spotifyBackup
.
backup_playlist
(
playlist
[
'
user
'
],
playlist
[
'
id
'
])
spotifyBackup
.
backup_playlist
(
playlist
[
'
user
'
],
playlist
[
'
id
'
])
...
...
This diff is collapsed.
Click to expand it.
settings-example.json
+
1
−
0
View file @
caa80a8a
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
"clientSecret"
:
""
"clientSecret"
:
""
},
},
"exportFolder"
:
""
,
"exportFolder"
:
""
,
"daysToKeep"
:
"30"
,
"playlists"
:
[
"playlists"
:
[
{
{
"user"
:
""
,
"user"
:
""
,
...
...
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