From 4edc352bcce96d320ac595ced1600608902dc0b7 Mon Sep 17 00:00:00 2001 From: Krateng Date: Sat, 2 Feb 2019 16:17:07 +0100 Subject: [PATCH] Asynchronous image calls and expanded functionality of website generation --- .gitignore | 3 +++ server.py | 7 ++++++- utilities.py | 36 +++++++++++++++++++++++------------- 3 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8e4fdc6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +*.sh +apikey diff --git a/server.py b/server.py index 6540f08..3ecc19b 100755 --- a/server.py +++ b/server.py @@ -100,7 +100,12 @@ def static_html(name): with open("website/" + name + ".html") as htmlfile: html = htmlfile.read() for k in txt_keys: - html = html.replace(k,txt_keys[k]) + if isinstance(txt_keys[k],list): + # if list, we replace each occurence with the next item + for element in txt_keys[k]: + html = html.replace(k,element,1) + else: + html = html.replace(k,txt_keys[k]) return html diff --git a/utilities.py b/utilities.py index 60907fa..d17d1cf 100644 --- a/utilities.py +++ b/utilities.py @@ -1,3 +1,9 @@ +import re +import os +import hashlib +from threading import Thread + + ### TSV files def parseTSV(filename,*args): @@ -37,8 +43,6 @@ def parseTSV(filename,*args): return result def checksumTSV(folder): - import hashlib - import os sums = "" @@ -80,7 +84,6 @@ def combineChecksums(filename,checksums): # checks ALL files for their rule state. if they are all the same as the current loaded one, the entire database can be assumed to be consistent with the current ruleset # in any other case, get out def consistentRulestate(folder,checksums): - import os result = [] for scrobblefile in os.listdir(folder + "/"): @@ -101,8 +104,7 @@ def consistentRulestate(folder,checksums): def parseAllTSV(path,*args): - - import os + result = [] for f in os.listdir(path + "/"): @@ -114,8 +116,7 @@ def parseAllTSV(path,*args): return result def createTSV(filename): - import os - + if not os.path.exists(filename): open(filename,"w").close() @@ -211,8 +212,6 @@ cachedTracks = {} cachedArtists = {} def getTrackInfo(artists,title): - import re - import os obj = (frozenset(artists),title) filename = "-".join([re.sub("[^a-zA-Z0-9]","",artist) for artist in artists]) + "_" + re.sub("[^a-zA-Z0-9]","",title) @@ -245,8 +244,6 @@ def getTrackInfo(artists,title): return result def getArtistInfo(artist): - import re - import os obj = artist filename = re.sub("[^a-zA-Z0-9]","",artist) @@ -274,6 +271,19 @@ def getArtistInfo(artist): result = apirequest(artist=artist) cachedArtists[artist] = result["image"] return result - - +def getArtistsInfo(artistlist): + + threads = [] + + for artist in artistlist: + t = Thread(target=getArtistInfo,args=(artist,)) + t.start() + threads.append(t) + + for t in threads: + t.join() + + # async calls only cached results, now we need to get them + return [getArtistInfo(a) for a in artistlist] +