Skip to content
Snippets Groups Projects
Commit 5e4453a4 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#2 - added settings option to specify a path for the login .cache file

parent efbc3171
Branches
Tags
No related merge requests found
...@@ -2,3 +2,6 @@ exports/ ...@@ -2,3 +2,6 @@ exports/
config/settings.json config/settings.json
config/settings-import.json config/settings-import.json
config/settings-creator.json config/settings-creator.json
.cache
.cache_creator
...@@ -3,6 +3,7 @@ from typing import Dict, List ...@@ -3,6 +3,7 @@ from typing import Dict, List
import spotipy import spotipy
from TheCodeLabs_BaseUtils.DefaultLogger import DefaultLogger from TheCodeLabs_BaseUtils.DefaultLogger import DefaultLogger
from spotipy import CacheFileHandler
from spotipy.oauth2 import SpotifyOAuth from spotipy.oauth2 import SpotifyOAuth
LOG_FORMAT = '[%(levelname)-7s] - %(asctime)s - %(message)s' LOG_FORMAT = '[%(levelname)-7s] - %(asctime)s - %(message)s'
...@@ -16,7 +17,8 @@ class SpotifyAutoPlaylistCreator: ...@@ -16,7 +17,8 @@ class SpotifyAutoPlaylistCreator:
destinationPlaylistInfo: Dict[str, str], destinationPlaylistInfo: Dict[str, str],
numberOfTracks: int, numberOfTracks: int,
redirectUrl: str, redirectUrl: str,
openBrowser: bool): openBrowser: bool,
cacheFilePath: str):
self._clientID = clientID self._clientID = clientID
self._clientSecret = clientSecret self._clientSecret = clientSecret
self._playlistInfo = playlistInfo self._playlistInfo = playlistInfo
...@@ -24,6 +26,7 @@ class SpotifyAutoPlaylistCreator: ...@@ -24,6 +26,7 @@ class SpotifyAutoPlaylistCreator:
self._numberOfTracks = numberOfTracks self._numberOfTracks = numberOfTracks
self._redirectUrl = redirectUrl self._redirectUrl = redirectUrl
self._openBrowser = openBrowser self._openBrowser = openBrowser
self._cacheFilePath = cacheFilePath
self._spotify = self.login() self._spotify = self.login()
...@@ -32,7 +35,8 @@ class SpotifyAutoPlaylistCreator: ...@@ -32,7 +35,8 @@ class SpotifyAutoPlaylistCreator:
client_secret=self._clientSecret, client_secret=self._clientSecret,
redirect_uri=self._redirectUrl, redirect_uri=self._redirectUrl,
scope='playlist-modify-private,playlist-modify-public', scope='playlist-modify-private,playlist-modify-public',
open_browser=self._openBrowser) open_browser=self._openBrowser,
cache_handler=CacheFileHandler(cache_path=self._cacheFilePath))
return spotipy.Spotify(client_credentials_manager=client_credentials_manager) return spotipy.Spotify(client_credentials_manager=client_credentials_manager)
def run(self): def run(self):
...@@ -111,7 +115,8 @@ if __name__ == '__main__': ...@@ -111,7 +115,8 @@ if __name__ == '__main__':
SETTINGS['destinationPlaylist'], SETTINGS['destinationPlaylist'],
SETTINGS['numberOfTracks'], SETTINGS['numberOfTracks'],
SETTINGS['redirectUrl'], SETTINGS['redirectUrl'],
SETTINGS['openBrowser']) SETTINGS['openBrowser'],
SETTINGS['cacheFilePath'])
spotifyBackup.run() spotifyBackup.run()
......
...@@ -4,6 +4,7 @@ from typing import Dict, List ...@@ -4,6 +4,7 @@ from typing import Dict, List
import spotipy import spotipy
from TheCodeLabs_BaseUtils.DefaultLogger import DefaultLogger from TheCodeLabs_BaseUtils.DefaultLogger import DefaultLogger
from spotipy import CacheFileHandler
from spotipy.oauth2 import SpotifyClientCredentials from spotipy.oauth2 import SpotifyClientCredentials
from BackupHandler import BackupHandler from BackupHandler import BackupHandler
...@@ -13,14 +14,18 @@ LOGGER = DefaultLogger().create_logger_if_not_exists('SpotifyBackup', logFormat= ...@@ -13,14 +14,18 @@ LOGGER = DefaultLogger().create_logger_if_not_exists('SpotifyBackup', logFormat=
class SpotifyBackup(BackupHandler): class SpotifyBackup(BackupHandler):
def __init__(self, clientID: str, clientSecret: str, exportFolder: str, playlists: List[Dict[str, str]]): def __init__(self, clientID: str, clientSecret: str, exportFolder: str,
playlists: List[Dict[str, str]], cacheFilePath: str):
self._clientSecret = clientSecret self._clientSecret = clientSecret
self._cacheFilePath = cacheFilePath
super().__init__(exportFolder, clientID) super().__init__(exportFolder, clientID)
self._playlists = playlists self._playlists = playlists
def login(self) -> spotipy.Spotify: def login(self) -> spotipy.Spotify:
client_credentials_manager = SpotifyClientCredentials(client_id=self._clientID, client_credentials_manager = SpotifyClientCredentials(client_id=self._clientID,
client_secret=self._clientSecret) client_secret=self._clientSecret,
cache_handler=CacheFileHandler(
cache_path=self._cacheFilePath))
return spotipy.Spotify(client_credentials_manager=client_credentials_manager) return spotipy.Spotify(client_credentials_manager=client_credentials_manager)
def backup(self): def backup(self):
...@@ -82,7 +87,8 @@ if __name__ == '__main__': ...@@ -82,7 +87,8 @@ if __name__ == '__main__':
spotifyBackup = SpotifyBackup(SETTINGS['spotifyAPI']['clientID'], spotifyBackup = SpotifyBackup(SETTINGS['spotifyAPI']['clientID'],
SETTINGS['spotifyAPI']['clientSecret'], SETTINGS['spotifyAPI']['clientSecret'],
SETTINGS['exportFolder'], SETTINGS['exportFolder'],
SETTINGS['playlists']) SETTINGS['playlists'],
SETTINGS['cacheFilePath'])
spotifyBackup.clean_old_exports(SETTINGS['daysToKeep']) spotifyBackup.clean_old_exports(SETTINGS['daysToKeep'])
spotifyBackup.backup() spotifyBackup.backup()
......
...@@ -16,5 +16,6 @@ ...@@ -16,5 +16,6 @@
}, },
"numberOfTracks": 10, "numberOfTracks": 10,
"redirectUrl": "http://localhost:8080", "redirectUrl": "http://localhost:8080",
"openBrowser": false "openBrowser": false,
"cacheFilePath": ".cache"
} }
\ No newline at end of file
...@@ -10,5 +10,6 @@ ...@@ -10,5 +10,6 @@
"user": "", "user": "",
"id": "" "id": ""
} }
] ],
"cacheFilePath": ".cache"
} }
\ No newline at end of file
[tool.poetry] [tool.poetry]
name = "SpotifyBackup" name = "SpotifyBackup"
version = "1.7.0" version = "1.8.1"
description = "Simple script for backing up public playlists from Spotify." description = "Simple script for backing up public playlists from Spotify."
authors = ["deadlocker8"] authors = ["deadlocker8"]
......
{ {
"version": { "version": {
"name": "v1.8.0", "name": "v1.8.1",
"code": 10, "code": 11,
"date": "26.03.22" "date": "14.04.22"
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment