Future-proofed update script

This commit is contained in:
Krateng 2019-03-24 13:45:59 +01:00
parent 5603ca9eb1
commit 2246efb4b4
2 changed files with 64 additions and 57 deletions

View File

@ -4,23 +4,19 @@ Simple self-hosted music scrobble database to create personal listening statisti
## Never Asked Questions
### Why not Last.fm / Libre.fm?
### Why not Last.fm / Libre.fm / GNU FM?
Maloja is self-hosted. You will always be able to access your data, and not have to trust anyone to provide an API for it. Your library is not synced with any public or official music database, so you can follow your own tagging schema or even group associated artists together in your charts.
Maloja is self-hosted. You will always be able to access your data in an easily-parseable format. Your library is not synced with any public or official music database, so you can follow your own tagging schema or even group associated artists together in your charts.
### Why not GNU FM?
Maloja also gets rid of all the extra stuff: social networking, radios, recommendations, etc. It only keeps track of your listening history and lets you analyze it.
Maloja gets rid of all the extra stuff: social networking, radios, recommendations, etc. It only keeps track of your listening history and lets you analyze it. This focus on its core allows it to potentially implement much better database features. One example: Maloja supports multiple artists per track. This means artists who are often just "featuring" in the track title get a place in your charts, and collaborations between several artists finally get credited to all participants.
### Why Maloja?
I like to name my projects after regions in Grisons, Switzerland. Don't waste your time trying to find a connection, I just picked one at random. Do visit Maloja though. It's a great pass to drive.
Maloja's database has one big advantage: It supports multiple artists per track. This means artists who are often just "featuring" in the track title get a place in your charts, and collaborations between several artists finally get credited to all participants. This allows you to get an actual idea of your artist preferences over time.
## Current status
You can check [my own Maloja page](https://maloja.krateng.ch) to see what it currently looks like.
You can check [my own Maloja page](https://maloja.krateng.ch) to see what it currently looks like.
There is only one scrobbler (a Chrome extension for Plex), but a very simple API to create your own scrobbler.
There are only two scrobblers (YouTube Music and Plex, both for Chromium), but a very simple API to create your own scrobbler.
## Requirements
@ -37,7 +33,7 @@ There is only one scrobbler (a Chrome extension for Plex), but a very simple API
2) Start the server with
maloja start
If you're missing packages, the console output will tell you so. Install them.
2) (Recommended) Put your server behind a reverse proxy for SSL encryption. Configure that proxy to rewrite /db/ requests to the database port. In nginx this would look as follows:
@ -55,19 +51,19 @@ There is only one scrobbler (a Chrome extension for Plex), but a very simple API
If you didn't install Maloja from the package (and therefore don't have it in `/opt/maloja`), every command needs to be executed from the Maloja directory and led with `./`. Otherwise, all commands work in any location and without the prefix.
1) In order to scrobble your music from Plex Web, install the included Chrome extension. Make sure to enter the random key Maloja generates on first startup in the extension.
1) In order to scrobble your music from Plex Web or YouTube Music, install the included Chrome extension. Make sure to enter the random key Maloja generates on first startup in the extension.
2) If you would like to import all your previous last.fm scrobbles, use [benfoxall's website](https://benjaminbenben.com/lastfm-to-csv/) ([GitHub page](https://github.com/benfoxall/lastfm-to-csv)). Use the command
maloja import *filename*
to import the downloaded file into Maloja.
3) You can interact with the server at any time with the commands
maloja stop
maloja restart
maloja start
maloja update
The `update` command will always fetch the latest version, while packages are only offered for release versions.

95
maloja
View File

@ -30,10 +30,10 @@ def gotodir():
elif os.path.exists("/opt/maloja/server.py"):
os.chdir("/opt/maloja/")
return True
print("Maloja installation could not be found.")
return False
def setup():
@ -41,7 +41,7 @@ def setup():
if os.path.exists("./apikey"):
with open("apikey","r") as keyfile:
apikey = keyfile.read().replace("\n","")
if apikey == "NONE": print("Currently not using an API key for image display. Only local images will be used.")
else:
print("Please enter your Last.FM API key. If you do not want to display artist and track images, simply leave this empty and press Enter.")
@ -49,7 +49,7 @@ def setup():
if key == "": key = "NONE"
with open("apikey","w") as keyfile:
keyfile.write(key)
# OWN API KEY
if os.path.exists("./clients/authenticated_machines.tsv"):
pass
@ -76,13 +76,13 @@ def install():
exec("import " + m) #I'm sorry
except:
toinstall.append(m)
for m in recommendedmodules:
try:
exec("import " + m)
except:
toinstallr.append(m)
if toinstall != []:
print("The following python modules need to be installed:")
for m in toinstall:
@ -91,7 +91,7 @@ def install():
print("The following python modules are highly recommended, some features will not work without them:")
for m in toinstallr:
print("\t" + m)
if toinstall != [] or toinstallr != []:
if os.geteuid() != 0:
print("Installing python modules should be fairly straight-forward, but Maloja can try to install \
@ -104,7 +104,7 @@ def install():
if toinstall != []:
print("Attempt to install required modules? [Y/n]")
answer = input()
if answer.lower() in ["y","yes","yea","1","positive","true",""]:
for m in neededmodules:
try:
@ -116,16 +116,16 @@ def install():
except:
print("Failure!")
fail = True
elif answer.lower() in ["n","no","nay","0","negative","false"]:
return False #if you dont want to auto install required, you probably dont want to install recommended
else:
print("What?")
return False
if toinstallr != []:
if toinstallr != []:
print("Attempt to install recommended modules? [Y/n]")
answer = input()
if answer.lower() in ["y","yes","yea","1","positive","true",""]:
for m in recommendedmodules:
try:
@ -137,17 +137,17 @@ def install():
except:
print("Failure!")
fail = True
elif answer.lower() in ["n","no","nay","0","negative","false"]:
return False
else:
print("What?")
return False
if fail: return False
print("All modules successfully installed!")
return True
else:
print("All necessary modules seem to be installed.")
return True
@ -161,18 +161,18 @@ def getInstance():
return pid
except:
return None
def start():
if install():
if gotodir():
setup()
p = subprocess.Popen(["python3","server.py"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
print(green("Maloja started!") + " PID: " + str(p.pid))
print("Visit your server address (Port 42010) to see your web interface. Visit /setup to get started.")
print("If you're installing this on your local machine, these links should get you there:")
print("\t" + blue("http://localhost:42010"))
@ -183,11 +183,11 @@ def start():
# p = subprocess.Popen(["python3","server.py"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
# print("Maloja started! PID: " + str(p.pid))
# return True
print("Error while starting Maloja.")
return False
def restart():
#pid = getInstance()
@ -196,11 +196,11 @@ def restart():
#else:
# stop()
#start()
wasrunning = stop()
start()
return wasrunning
def stop():
pid = getInstance()
if pid == None:
@ -225,33 +225,45 @@ def update():
#with urllib.request.urlopen(SOURCE_URL) as response:
# with tempfile.NamedTemporaryFile(delete=True) as tmpfile:
# shutil.copyfileobj(response,tmpfile)
#
#
# with zipfile.ZipFile(tmpfile.name,"r") as z:
#
#
# for f in z.namelist():
# #print("extracting " + f)
# z.extract(f)
os.system("wget " + SOURCE_URL)
with zipfile.ZipFile("./master.zip","r") as z:
for f in z.namelist():
#print("extracting " + f)
z.extract(f)
# if we ever have a separate directory for the code
# (the calling update script is not the same version as the current
# remote repository, so we better add this check just in case)
if "source/" in z.namelist():
for f in z.namelist():
if f.startswith("source/"):
z.extract(f)
for dir,_,files in os.walk("source"):
for f in files:
origfile = os.path.join(dir,f)
newfile = ps.path.join(dir[7:],f)
os.renames(origfile,newfile) #also prunes empty directory
else:
for f in z.namelist():
z.extract(f)
os.remove("./master.zip")
distutils.dir_util.copy_tree("./maloja-master/","./",verbose=2)
shutil.rmtree("./maloja-master")
print("Done!")
os.chmod("./maloja",os.stat("./maloja").st_mode | stat.S_IXUSR)
if stop(): start() #stop returns whether it was running before, in which case we restart it
def loadlastfm():
try:
@ -260,8 +272,8 @@ def loadlastfm():
except:
print("Please specify a file!")
return
if gotodir():
if gotodir():
if os.path.exists("./scrobbles/lastfmimport.tsv"):
print("Already imported Last.FM data. Overwrite? [y/N]")
if input().lower() in ["y","yes","yea","1","positive","true"]:
@ -271,7 +283,7 @@ def loadlastfm():
print("Please wait...")
os.system("python3 ./lastfmconverter.py " + filename + " ./scrobbles/lastfmimport.tsv")
print("Successfully imported your Last.FM scrobbles!")
if __name__ == "__main__":
@ -281,4 +293,3 @@ if __name__ == "__main__":
elif sys.argv[1] == "update": update()
elif sys.argv[1] == "import": loadlastfm()
else: print("Valid commands: start restart stop update")