1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Finally refactored lastfm import

This commit is contained in:
Krateng 2019-12-15 15:27:16 +01:00
parent dbc23ca73c
commit e50cce28fa
4 changed files with 68 additions and 50 deletions

View File

@ -2,7 +2,7 @@ import tarfile
from datetime import datetime from datetime import datetime
import glob import glob
import os import os
from globalconf import datadir from .globalconf import datadir
user_files = { user_files = {

View File

@ -25,8 +25,17 @@ class CleanerAgent:
#self.rules_regextitle = [[b,c] for [a,b,c,d] in raw if a=="regextitle"] #self.rules_regextitle = [[b,c] for [a,b,c,d] in raw if a=="regextitle"]
# TODO # TODO
#self.plugin_artistparsers = []
#self.plugin_titleparsers = []
#if settings.get_settings("USE_PARSE_PLUGINS"):
# for ep in pkg_resources.iter_entry_points(group='maloja.artistparsers'):
# self.plugin_artistparsers.append(ep.load())
# for ep in pkg_resources.iter_entry_points(group='maloja.titleparsers'):
# self.plugin_titleparsers.append(ep.load())
# we always need to be able to tell if our current database is made with the current rules # we always need to be able to tell if our current database is made with the current rules
self.checksums = utilities.checksumTSV("rules") self.checksums = utilities.checksumTSV(datadir("rules"))
@ -125,9 +134,12 @@ class CleanerAgent:
for s in settings.get_settings("REMOVE_FROM_TITLE"): for s in settings.get_settings("REMOVE_FROM_TITLE"):
if s in t: if s in t:
t = t.replace(s,"").strip() t = t.replace(s,"")
return t.strip() t = t.strip()
#for p in self.plugin_titleparsers:
# t = p(t).strip()
return t
def parseTitleForArtists(self,t): def parseTitleForArtists(self,t):
for d in self.delimiters_feat: for d in self.delimiters_feat:

View File

@ -144,10 +144,8 @@ def stop():
def loadlastfm(filename): def loadlastfm(filename):
try: if not os.path.exists(filename):
filename = os.path.join(origpath,filename) print("File could not be found.")
except:
print("Please specify a file!")
return return
if os.path.exists(datadir("scrobbles/lastfmimport.tsv")): if os.path.exists(datadir("scrobbles/lastfmimport.tsv")):
@ -157,7 +155,9 @@ def loadlastfm(filename):
else: else:
return return
print("Please wait...") print("Please wait...")
os.system("python3 -m maloja.lastfmconverter " + filename + " " + datadir("scrobbles/lastfmimport.tsv")) from .lastfmconverter import convert
convert(filename,datadir("scrobbles/lastfmimport.tsv"))
#os.system("python3 -m maloja.lastfmconverter " + filename + " " + datadir("scrobbles/lastfmimport.tsv"))
print("Successfully imported your Last.FM scrobbles!") print("Successfully imported your Last.FM scrobbles!")
def direct(): def direct():

View File

@ -1,63 +1,69 @@
import sys, os, datetime, re, cleanup import os, datetime, re
from .cleanup import * from .cleanup import *
from .utilities import * from .utilities import *
log = open(sys.argv[1])
outputlog = open(sys.argv[2],"w")
checksumfile = open(sys.argv[2] + ".rulestate","w") #this file stores an identifier for all rules that were in place when the corresponding file was created
c = CleanerAgent() c = CleanerAgent()
stamps = [99999999999999]
for l in log:
l = l.replace("\n","")
data = l.split(",")
artist = data[0]
album = data[1]
title = data[2]
time = data[3]
(artists,title) = c.fullclean(artist,title)
artistsstr = "".join(artists) def convert(input,output):
log = open(input,"r")
outputlog = open(output,"w")
checksumfile = open(output + ".rulestate","w") #this file stores an identifier for all rules that were in place when the corresponding file was created
stamps = [99999999999999]
for l in log:
l = l.replace("\n","")
data = l.split(",")
artist = data[0]
album = data[1]
title = data[2]
time = data[3]
timeparts = time.split(" ") (artists,title) = c.fullclean(artist,title)
(h,m) = timeparts[3].split(":")
months = {"Jan":1,"Feb":2,"Mar":3,"Apr":4,"May":5,"Jun":6,"Jul":7,"Aug":8,"Sep":9,"Oct":10,"Nov":11,"Dec":12} artistsstr = "".join(artists)
timestamp = int(datetime.datetime(int(timeparts[2]),months[timeparts[1]],int(timeparts[0]),int(h),int(m)).timestamp())
## We prevent double timestamps in the database creation, so we technically don't need them in the files timeparts = time.split(" ")
## however since the conversion from lastfm to maloja is a one-time thing, we should take any effort to make the file as good as possible (h,m) = timeparts[3].split(":")
if (timestamp < stamps[-1]):
pass months = {"Jan":1,"Feb":2,"Mar":3,"Apr":4,"May":5,"Jun":6,"Jul":7,"Aug":8,"Sep":9,"Oct":10,"Nov":11,"Dec":12}
elif (timestamp == stamps[-1]):
timestamp -= 1 timestamp = int(datetime.datetime(int(timeparts[2]),months[timeparts[1]],int(timeparts[0]),int(h),int(m)).timestamp())
else:
while(timestamp in stamps):
## We prevent double timestamps in the database creation, so we technically don't need them in the files
## however since the conversion from lastfm to maloja is a one-time thing, we should take any effort to make the file as good as possible
if (timestamp < stamps[-1]):
pass
elif (timestamp == stamps[-1]):
timestamp -= 1 timestamp -= 1
else:
while(timestamp in stamps):
timestamp -= 1
if (timestamp < stamps[-1]): if (timestamp < stamps[-1]):
stamps.append(timestamp) stamps.append(timestamp)
else: else:
stamps.insert(0,timestamp) stamps.insert(0,timestamp)
entry = "\t".join([str(timestamp),artistsstr,title,album]) entry = "\t".join([str(timestamp),artistsstr,title,album])
entry = entry.replace("#",r"\num") entry = entry.replace("#",r"\num")
outputlog.write(entry) outputlog.write(entry)
outputlog.write("\n") outputlog.write("\n")
checksumfile.write(c.checksums) checksumfile.write(c.checksums)
log.close() log.close()
outputlog.close() outputlog.close()
checksumfile.close() checksumfile.close()