2020-02-20 14:42:12 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import json
|
2020-04-17 10:31:47 +00:00
|
|
|
import os.path as path
|
2020-07-01 14:33:47 +00:00
|
|
|
from os import makedirs, listdir, remove
|
|
|
|
from deemix import __version__ as deemixVersion
|
2020-05-20 14:36:01 +00:00
|
|
|
import random
|
|
|
|
import string
|
2020-05-20 15:02:24 +00:00
|
|
|
import logging
|
2020-07-01 14:33:47 +00:00
|
|
|
import datetime
|
|
|
|
import platform
|
2020-05-20 15:02:24 +00:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger('deemix')
|
2020-02-20 14:42:12 +00:00
|
|
|
|
|
|
|
import deemix.utils.localpaths as localpaths
|
|
|
|
|
|
|
|
settings = {}
|
2020-02-22 11:59:42 +00:00
|
|
|
defaultSettings = {}
|
2020-05-26 10:16:07 +00:00
|
|
|
configDir = ""
|
2020-02-20 14:42:12 +00:00
|
|
|
|
2020-05-26 10:16:07 +00:00
|
|
|
def initSettings(localFolder = False, configFolder = None):
|
2020-04-17 10:31:47 +00:00
|
|
|
global settings
|
|
|
|
global defaultSettings
|
2020-05-26 10:16:07 +00:00
|
|
|
global configDir
|
2020-04-17 10:31:47 +00:00
|
|
|
currentFolder = path.abspath(path.dirname(__file__))
|
2020-05-26 10:16:07 +00:00
|
|
|
if not configFolder:
|
|
|
|
configFolder = localpaths.getConfigFolder()
|
|
|
|
configDir = configFolder
|
2020-05-18 10:31:56 +00:00
|
|
|
makedirs(configFolder, exist_ok=True)
|
2020-04-17 10:31:47 +00:00
|
|
|
with open(path.join(currentFolder, 'default.json'), 'r') as d:
|
|
|
|
defaultSettings = json.load(d)
|
2020-05-20 14:38:11 +00:00
|
|
|
defaultSettings['downloadLocation'] = path.join(localpaths.getHomeFolder(), 'deemix Music')
|
2020-04-17 10:31:47 +00:00
|
|
|
if not path.isfile(path.join(configFolder, 'config.json')):
|
|
|
|
with open(path.join(configFolder, 'config.json'), 'w') as f:
|
|
|
|
json.dump(defaultSettings, f, indent=2)
|
|
|
|
with open(path.join(configFolder, 'config.json'), 'r') as configFile:
|
|
|
|
settings = json.load(configFile)
|
|
|
|
settingsCheck()
|
2020-05-20 14:36:01 +00:00
|
|
|
|
2020-05-20 15:02:24 +00:00
|
|
|
if localFolder:
|
2020-05-20 14:36:01 +00:00
|
|
|
settings['downloadLocation'] = randomString(12)
|
2020-05-20 15:02:24 +00:00
|
|
|
logger.info("Using a local download folder: "+settings['downloadLocation'])
|
2020-05-20 14:36:01 +00:00
|
|
|
elif settings['downloadLocation'] == "":
|
2020-05-20 15:02:24 +00:00
|
|
|
settings['downloadLocation'] = path.join(localpaths.getHomeFolder(), 'deemix Music')
|
2020-05-18 10:31:56 +00:00
|
|
|
saveSettings(settings)
|
2020-05-17 01:26:17 +00:00
|
|
|
makedirs(settings['downloadLocation'], exist_ok=True)
|
2020-07-01 14:33:47 +00:00
|
|
|
|
|
|
|
# logfiles
|
|
|
|
# logfile name
|
|
|
|
logspath = path.join(configFolder, 'logs')
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
logfile = now.strftime("%Y-%m-%d_%H%M%S")+".log"
|
|
|
|
makedirs(logspath, exist_ok=True)
|
|
|
|
# add handler for logfile
|
|
|
|
fh = logging.FileHandler(path.join(logspath, logfile))
|
|
|
|
fh.setLevel(logging.DEBUG)
|
|
|
|
fh.setFormatter(logging.Formatter('%(asctime)s - [%(levelname)s] %(message)s'))
|
|
|
|
logger.addHandler(fh)
|
|
|
|
logger.info(f"{platform.platform(True, True)} - Python {platform.python_version()}, deemix {deemixVersion}")
|
|
|
|
#delete old logfiles
|
|
|
|
logslist = listdir(logspath)
|
|
|
|
logslist.sort()
|
|
|
|
if len(logslist)>5:
|
|
|
|
for i in range(len(logslist)-5):
|
|
|
|
remove(path.join(logspath, logslist[i]))
|
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
return settings
|
|
|
|
|
2020-02-20 14:42:12 +00:00
|
|
|
|
|
|
|
def getSettings():
|
2020-04-17 10:31:47 +00:00
|
|
|
global settings
|
|
|
|
return settings
|
|
|
|
|
2020-02-20 14:42:12 +00:00
|
|
|
|
2020-05-03 14:21:37 +00:00
|
|
|
def getDefaultSettings():
|
|
|
|
global defaultSettings
|
|
|
|
return defaultSettings
|
|
|
|
|
|
|
|
|
2020-02-20 14:42:12 +00:00
|
|
|
def saveSettings(newSettings):
|
2020-04-17 10:31:47 +00:00
|
|
|
global settings
|
|
|
|
settings = newSettings
|
2020-05-26 10:16:07 +00:00
|
|
|
with open(path.join(configDir, 'config.json'), 'w') as configFile:
|
2020-04-17 10:31:47 +00:00
|
|
|
json.dump(settings, configFile, indent=2)
|
|
|
|
return True
|
|
|
|
|
2020-02-22 11:59:42 +00:00
|
|
|
|
|
|
|
def settingsCheck():
|
2020-04-17 10:31:47 +00:00
|
|
|
global settings
|
|
|
|
global defaultSettings
|
|
|
|
changes = 0
|
|
|
|
for x in defaultSettings:
|
|
|
|
if not x in settings or type(settings[x]) != type(defaultSettings[x]):
|
|
|
|
settings[x] = defaultSettings[x]
|
|
|
|
changes += 1
|
|
|
|
for x in defaultSettings['tags']:
|
|
|
|
if not x in settings['tags'] or type(settings['tags'][x]) != type(defaultSettings['tags'][x]):
|
|
|
|
settings['tags'][x] = defaultSettings['tags'][x]
|
|
|
|
changes += 1
|
|
|
|
if changes > 0:
|
|
|
|
saveSettings(settings)
|
2020-05-20 14:36:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def randomString(stringLength=8):
|
|
|
|
letters = string.ascii_lowercase
|
|
|
|
return ''.join(random.choice(letters) for i in range(stringLength))
|