2020-07-01 10:30:26 +01:00
|
|
|
from spotframework.net.user import NetworkUser
|
|
|
|
from spotframework.net.network import Network, SpotifyNetworkException
|
2019-02-28 07:49:52 +00:00
|
|
|
import spotframework.io.csv as csvwrite
|
|
|
|
|
2019-05-14 12:39:17 +01:00
|
|
|
import sys
|
|
|
|
import datetime
|
|
|
|
import os
|
2019-08-07 15:45:25 +01:00
|
|
|
import logging
|
2019-03-04 21:31:32 +00:00
|
|
|
|
2019-02-28 07:49:52 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2019-08-07 15:45:25 +01:00
|
|
|
logger = logging.getLogger('spotframework')
|
|
|
|
|
|
|
|
file_handler = logging.FileHandler(".spot/backup.log")
|
2020-07-01 10:42:39 +01:00
|
|
|
file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(name)s:%(funcName)s - %(message)s'))
|
2019-08-07 15:45:25 +01:00
|
|
|
logger.addHandler(file_handler)
|
|
|
|
|
2019-09-05 12:20:21 +01:00
|
|
|
stream_handler = logging.StreamHandler()
|
2020-07-01 10:42:39 +01:00
|
|
|
stream_handler.setFormatter(logging.Formatter('%(levelname)s %(name)s:%(funcName)s - %(message)s'))
|
2019-09-05 12:20:21 +01:00
|
|
|
logger.addHandler(stream_handler)
|
|
|
|
|
2020-07-01 10:42:39 +01:00
|
|
|
datepath = str(datetime.datetime.now()).split(' ')[0].replace('-', '/')
|
|
|
|
|
|
|
|
totalpath = os.path.join(sys.argv[1], datepath)
|
|
|
|
if not os.path.exists(totalpath):
|
|
|
|
logger.info(f'creating path {totalpath}')
|
|
|
|
os.makedirs(totalpath)
|
2019-05-08 22:41:47 +01:00
|
|
|
|
2020-06-22 20:15:22 +01:00
|
|
|
network = Network(NetworkUser(client_id=os.environ['SPOT_CLIENT'],
|
|
|
|
client_secret=os.environ['SPOT_SECRET'],
|
|
|
|
refresh_token=os.environ['SPOT_REFRESH'])).refresh_access_token()
|
2019-03-04 22:45:58 +00:00
|
|
|
|
2020-07-01 10:30:26 +01:00
|
|
|
try:
|
2020-08-12 09:28:41 +01:00
|
|
|
playlists = network.user_playlists()
|
2019-02-28 07:49:52 +00:00
|
|
|
|
2020-07-01 10:30:26 +01:00
|
|
|
for playlist in playlists:
|
|
|
|
try:
|
2020-08-12 09:28:41 +01:00
|
|
|
playlist.tracks = network.playlist_tracks(uri=playlist.uri)
|
2020-07-01 10:42:39 +01:00
|
|
|
csvwrite.export_playlist(playlist, totalpath)
|
2020-07-01 10:30:26 +01:00
|
|
|
except SpotifyNetworkException:
|
|
|
|
logger.exception(f'error occured during {playlist.name} track retrieval')
|
2019-05-14 12:39:17 +01:00
|
|
|
|
2020-07-01 10:30:26 +01:00
|
|
|
except SpotifyNetworkException:
|
2020-07-01 10:42:39 +01:00
|
|
|
logger.exception('error occured during user playlists retrieval')
|