2022-04-09 22:02:17 +03:00
import os
2022-03-10 07:31:41 +03:00
from importlib import resources
2020-06-05 14:16:02 +03:00
from distutils import dir_util
2022-04-09 22:02:17 +03:00
2020-06-05 14:16:02 +03:00
from doreah . io import col , ask , prompt
2020-08-17 20:04:51 +03:00
from doreah import auth
2020-06-05 14:16:02 +03:00
2022-04-09 22:39:04 +03:00
from . pkg_global . conf import data_dir , dir_settings , malojaconfig
2022-03-06 03:57:46 +03:00
2020-06-05 14:16:02 +03:00
# EXTERNAL API KEYS
2021-12-25 04:22:57 +03:00
ext_apikeys = [
2021-12-24 11:41:41 +03:00
" LASTFM_API_KEY " ,
" SPOTIFY_API_ID " ,
" SPOTIFY_API_SECRET " ,
" AUDIODB_API_KEY "
]
2020-06-05 14:16:02 +03:00
def copy_initial_local_files ( ) :
2022-03-10 07:31:41 +03:00
with resources . files ( " maloja " ) / ' data_files ' as folder :
for cat in dir_settings :
dir_util . copy_tree ( os . path . join ( folder , cat ) , dir_settings [ cat ] , update = False )
2020-06-05 14:16:02 +03:00
2020-08-18 05:55:36 +03:00
charset = list ( range ( 10 ) ) + list ( " abcdefghijklmnopqrstuvwxyz " ) + list ( " ABCDEFGHIJKLMNOPQRSTUVWXYZ " )
2020-08-17 20:04:51 +03:00
def randomstring ( length = 32 ) :
import random
2021-10-19 15:58:24 +03:00
return " " . join ( str ( random . choice ( charset ) ) for _ in range ( length ) )
2020-06-05 14:16:02 +03:00
def setup ( ) :
copy_initial_local_files ( )
2021-12-19 23:10:55 +03:00
SKIP = malojaconfig [ " SKIP_SETUP " ]
2020-06-05 14:16:02 +03:00
print ( " Various external services can be used to display images. If not enough of them are set up, only local images will be used. " )
2021-12-25 04:22:57 +03:00
for k in ext_apikeys :
2021-12-24 11:41:41 +03:00
keyname = malojaconfig . get_setting_info ( k ) [ ' name ' ]
2021-12-19 23:10:55 +03:00
key = malojaconfig [ k ]
2021-11-27 22:04:13 +03:00
if key is False :
2022-04-09 18:05:54 +03:00
print ( f " \t Currently not using a { col [ ' red ' ] ( keyname ) } for image display. " )
2021-11-27 22:04:13 +03:00
elif key is None or key == " ASK " :
2022-04-09 18:05:54 +03:00
promptmsg = f " \t Please enter your { col [ ' gold ' ] ( keyname ) } . If you do not want to use one at this moment, simply leave this empty and press Enter. "
key = prompt ( promptmsg , types = ( str , ) , default = False , skip = SKIP )
2021-12-19 23:10:55 +03:00
malojaconfig [ k ] = key
2020-06-05 14:16:02 +03:00
else :
2022-04-09 18:05:54 +03:00
print ( f " \t { col [ ' lawngreen ' ] ( keyname ) } found. " )
2020-06-05 14:16:02 +03:00
# OWN API KEY
2022-04-09 22:02:17 +03:00
from . apis import apikeystore
2021-12-25 04:22:57 +03:00
if len ( apikeystore ) == 0 :
2020-06-05 14:16:02 +03:00
answer = ask ( " Do you want to set up a key to enable scrobbling? Your scrobble extension needs that key so that only you can scrobble tracks to your database. " , default = True , skip = SKIP )
if answer :
2021-12-25 04:22:57 +03:00
key = apikeystore . generate_key ( ' default ' )
2020-06-05 14:16:02 +03:00
print ( " Your API Key: " + col [ " yellow " ] ( key ) )
2020-08-17 20:04:51 +03:00
# PASSWORD
2021-12-19 23:10:55 +03:00
forcepassword = malojaconfig [ " FORCE_PASSWORD " ]
2020-08-17 20:04:51 +03:00
# this is mainly meant for docker, supply password via environment variable
if forcepassword is not None :
# user has specified to force the pw, nothing else matters
auth . defaultuser . setpw ( forcepassword )
print ( " Password has been set. " )
elif auth . defaultuser . checkpw ( " admin " ) :
# if the actual pw is admin, it means we've never set this up properly (eg first start after update)
2022-03-29 05:33:44 +03:00
while True :
2022-02-13 07:56:36 +03:00
newpw = prompt ( " Please set a password for web backend access. Leave this empty to generate a random password. " , skip = SKIP , secret = True )
if newpw is None :
newpw = randomstring ( 32 )
print ( " Generated password: " , col [ " yellow " ] ( newpw ) )
2022-03-29 05:33:44 +03:00
break
2022-02-13 07:56:36 +03:00
else :
newpw_repeat = prompt ( " Please type again to confirm. " , skip = SKIP , secret = True )
if newpw != newpw_repeat : print ( " Passwords do not match! " )
2022-03-29 05:33:44 +03:00
else : break
2022-02-20 07:06:38 +03:00
auth . defaultuser . setpw ( newpw )