spotframework/backup.py

55 lines
1.6 KiB
Python
Raw Normal View History

2019-09-04 17:45:26 +01:00
from spotframework.net.user import NetworkUser
2019-06-11 11:59:24 +01:00
from spotframework.net.network import Network
2019-02-28 07:49:52 +00:00
import spotframework.io.csv as csvwrite
import sys
import datetime
import os
2019-08-07 15:45:25 +01:00
import logging
2019-02-28 07:49:52 +00:00
if __name__ == '__main__':
2019-08-07 15:45:25 +01:00
logger = logging.getLogger('spotframework')
log_format = '%(asctime)s %(levelname)s %(name)s:%(funcName)s - %(message)s'
file_handler = logging.FileHandler(".spot/backup.log")
formatter = logging.Formatter(log_format)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
stream_log_format = '%(levelname)s %(name)s:%(funcName)s - %(message)s'
stream_formatter = logging.Formatter(stream_log_format)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(stream_formatter)
logger.addHandler(stream_handler)
2019-10-10 21:56:50 +01:00
# try:
2019-10-10 21:56:50 +01:00
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
os.environ['SPOTSECRET'],
os.environ['SPOTREFRESH']))
network.user.refresh_access_token()
playlists = network.get_user_playlists()
2019-03-04 22:45:58 +00:00
2019-10-10 21:56:50 +01:00
for playlist in playlists:
playlist.tracks = network.get_playlist_tracks(playlist.uri)
2019-02-28 07:49:52 +00:00
2019-10-10 21:56:50 +01:00
path = sys.argv[1]
2019-10-10 21:56:50 +01:00
datepath = str(datetime.datetime.now()).split(' ')[0].replace('-', '/')
2019-05-14 13:05:25 +01:00
2019-10-10 21:56:50 +01:00
totalpath = os.path.join(path, datepath)
pathdir = os.path.dirname(totalpath)
if not os.path.exists(totalpath):
os.makedirs(totalpath)
2019-05-14 13:05:25 +01:00
2019-10-10 21:56:50 +01:00
for play in playlists:
csvwrite.export_playlist(play, totalpath)
2019-05-14 13:05:25 +01:00
2019-10-10 21:56:50 +01:00
# except Exception as e:
# logger.exception(f'exception occured')