mirror of
https://github.com/krateng/maloja.git
synced 2023-08-10 21:12:55 +03:00
More leniency on image filenames
This commit is contained in:
parent
4d96500644
commit
a7963ee679
@ -3,42 +3,41 @@ don't like the ones provided by Last.FM, you don't want to use Last.FM or it's
|
|||||||
missing some artists / tracks that are important to you.
|
missing some artists / tracks that are important to you.
|
||||||
|
|
||||||
For artists, you can use a .jpg, .jpeg, .png or .gif image that is named like
|
For artists, you can use a .jpg, .jpeg, .png or .gif image that is named like
|
||||||
the artist, but without whitespace and non-latin-alphabet characters (plus
|
the artist, but with all non-alphanumeric characters removed. You may also chose
|
||||||
extension). In case you want to have multiple images of the same format, you may
|
the safe route and omit everything but numbers and latin characters (except the
|
||||||
add any number from 0-9 to the name. If you need more images than that, create a
|
dot separating the name and the extension). You can either capitalize exactly as
|
||||||
folder named like the artist and add any images inside. So, for example, if your
|
the title or use only lower case, but not use arbitrary capizalization. In case
|
||||||
artist is "Dal★Shabet", all these files would be considered:
|
you want to have multiple images of the same format, you may create a folder
|
||||||
|
named like the artist and add any images inside. So, for example, if your artist
|
||||||
|
is "Dal★Shabet", all these files would be considered:
|
||||||
|
|
||||||
DalShabet.jpg
|
DalShabet.jpg
|
||||||
DalShabet4.png
|
dalshabet.png
|
||||||
DalShabet0.gif
|
|
||||||
DalShabet/bestimage.jpeg
|
DalShabet/bestimage.jpeg
|
||||||
DalShabet/serri_only.png
|
dalshabet/serri_only.png
|
||||||
|
|
||||||
The following files would not be considered:
|
The following files would not be considered:
|
||||||
|
|
||||||
Dal Shabet.gif (Whitespace)
|
Dal★Shabet.png (non-alphanumeric character)
|
||||||
Dal★Shabet.png (non-latin character)
|
Dalshabet.gif (wrong capitalization)
|
||||||
DalShabet.webm (wrong extension)
|
DalShabet.webm (wrong extension)
|
||||||
DalShabet23.jpg (to digit number)
|
|
||||||
DalShabet/SomeoneLikeU_MV.mp4 (wrong extension)
|
DalShabet/SomeoneLikeU_MV.mp4 (wrong extension)
|
||||||
DalShabet3/party.png (wrong folder name)
|
Dal★Shabet/party.png (non-alphanumeric character in folder)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
The same concept applies to tracks, however their name consists of all artists in
|
The same concept applies to tracks, however their name consists of all artists
|
||||||
alphabetical order joined by a dash (-), then an underscore (_) and the track
|
joined by a dash (-), then an underscore (_) and the track title. Artists should
|
||||||
title. Rules as above apply, so if your track was named "Epic Crossover 파티 협동"
|
be joined in alphabetical order, but filenames with the wrong order might still
|
||||||
and it was performed by HyunA, Dal★Shabet and Taylor Swift, the following files
|
work for a low artist number. Rules as above apply, so if your track was named
|
||||||
would be considered:
|
"Epic Crossover 파티 협동" and it was performed by HyunA, Dal★Shabet and Taylor
|
||||||
|
Swift, the following files would be considered:
|
||||||
|
|
||||||
DalShabet-HyunA-TaylorSwift_EpicCrossover.jpg
|
DalShabet-HyunA-TaylorSwift_EpicCrossover.jpg
|
||||||
DalShabet-HyunA-TaylorSwift_EpicCrossover3.png
|
DalShabet-TaylorSwift-HyunA_EpicCrossover파티협동.png
|
||||||
DalShabet-HyunA-TaylorSwift_EpicCrossover/albumcover.png
|
dalshabet-hyuna-taylorswift_epiccrossover/albumcover.png
|
||||||
DalShabet-HyunA-TaylorSwift_EpicCrossover/dancing.gif
|
DalShabet-HyunA-TaylorSwift_EpicCrossover파티협동/dancing.gif
|
||||||
|
|
||||||
Not accepted would be:
|
Not accepted would be:
|
||||||
|
|
||||||
DalShabet-HyunA-Taylor Swift_EpicCrossover.jpg (Whitespace)
|
DalShabet-HyunA-Taylor Swift_epiccrossover.jpg (wrong capitalization)
|
||||||
DalShabet-HyunA-TaylorSwift_EpicCrossover파티협동.jpg (non-latin characters)
|
|
||||||
HyunA-DalShabet-TaylorSwift_EpicCrossover.jpg (wrong artist order)
|
|
||||||
|
@ -155,6 +155,8 @@ def static_html(name):
|
|||||||
|
|
||||||
# add headers for server push
|
# add headers for server push
|
||||||
for resource in resources:
|
for resource in resources:
|
||||||
|
if all(ord(c) < 128 for c in resource["file"]):
|
||||||
|
# we can only put ascii stuff in the http header
|
||||||
linkheaders.append("<" + resource["file"] + ">; rel=preload; as=" + resource["type"])
|
linkheaders.append("<" + resource["file"] + ">; rel=preload; as=" + resource["type"])
|
||||||
|
|
||||||
# apply key substitutions
|
# apply key substitutions
|
||||||
|
125
utilities.py
125
utilities.py
@ -6,6 +6,7 @@ import pickle
|
|||||||
import urllib
|
import urllib
|
||||||
import datetime
|
import datetime
|
||||||
import random
|
import random
|
||||||
|
import itertools
|
||||||
from doreah import settings
|
from doreah import settings
|
||||||
from doreah import caching
|
from doreah import caching
|
||||||
from doreah.logging import log
|
from doreah.logging import log
|
||||||
@ -155,6 +156,89 @@ artist_cache = caching.Cache.create(name="artist_cache",maxage=cacheage,maxage_n
|
|||||||
track_cache = caching.Cache.create(name="track_cache",maxage=cacheage,maxage_negative=cacheage_neg)
|
track_cache = caching.Cache.create(name="track_cache",maxage=cacheage,maxage_negative=cacheage_neg)
|
||||||
|
|
||||||
|
|
||||||
|
# removes emojis and weird shit from names
|
||||||
|
def clean(name):
|
||||||
|
return "".join(c for c in name if c.isalnum() or c in []).strip()
|
||||||
|
|
||||||
|
def local_files(artist=None,artists=None,title=None):
|
||||||
|
|
||||||
|
# check if we're dealing with a track or artist, then clean up names
|
||||||
|
# (only remove non-alphanumeric, allow korean and stuff)
|
||||||
|
if title is not None and artists is not None:
|
||||||
|
track = True
|
||||||
|
title, artists = clean(title), [clean(a) for a in artists]
|
||||||
|
elif artist is not None:
|
||||||
|
track = False
|
||||||
|
artist = clean(artist)
|
||||||
|
else: return []
|
||||||
|
|
||||||
|
|
||||||
|
superfolder = "images/tracks/" if track else "images/artists/"
|
||||||
|
|
||||||
|
filenames = []
|
||||||
|
|
||||||
|
if track:
|
||||||
|
#unsafeartists = [artist.translate(None,"-_./\\") for artist in artists]
|
||||||
|
safeartists = [re.sub("[^a-zA-Z0-9]","",artist) for artist in artists]
|
||||||
|
#unsafetitle = title.translate(None,"-_./\\")
|
||||||
|
safetitle = re.sub("[^a-zA-Z0-9]","",title)
|
||||||
|
|
||||||
|
if len(artists) < 4:
|
||||||
|
unsafeperms = itertools.permutations(artists)
|
||||||
|
safeperms = itertools.permutations(safeartists)
|
||||||
|
else:
|
||||||
|
unsafeperms = [sorted(artists)]
|
||||||
|
safeperms = [sorted(safeartists)]
|
||||||
|
|
||||||
|
|
||||||
|
for unsafeartistlist in unsafeperms:
|
||||||
|
filename = "-".join(unsafeartistlist) + "_" + title
|
||||||
|
if filename != "":
|
||||||
|
filenames.append(filename)
|
||||||
|
filenames.append(filename.lower())
|
||||||
|
for safeartistlist in safeperms:
|
||||||
|
filename = "-".join(safeartistlist) + "_" + safetitle
|
||||||
|
if filename != "":
|
||||||
|
filenames.append(filename)
|
||||||
|
filenames.append(filename.lower())
|
||||||
|
filenames = list(set(filenames))
|
||||||
|
if len(filenames) == 0: filenames.append(str(hash((frozenset(artists),title))))
|
||||||
|
else:
|
||||||
|
#unsafeartist = artist.translate(None,"-_./\\")
|
||||||
|
safeartist = re.sub("[^a-zA-Z0-9]","",artist)
|
||||||
|
|
||||||
|
filename = artist
|
||||||
|
if filename != "":
|
||||||
|
filenames.append(filename)
|
||||||
|
filenames.append(filename.lower())
|
||||||
|
filename = safeartist
|
||||||
|
if filename != "":
|
||||||
|
filenames.append(filename)
|
||||||
|
filenames.append(filename.lower())
|
||||||
|
|
||||||
|
filenames = list(set(filenames))
|
||||||
|
if len(filenames) == 0: filenames.append(str(hash(artist)))
|
||||||
|
|
||||||
|
images = []
|
||||||
|
|
||||||
|
for purename in filenames:
|
||||||
|
# direct files
|
||||||
|
for ext in ["png","jpg","jpeg","gif"]:
|
||||||
|
#for num in [""] + [str(n) for n in range(0,10)]:
|
||||||
|
if os.path.exists(superfolder + purename + "." + ext):
|
||||||
|
images.append("/" + superfolder + purename + "." + ext)
|
||||||
|
|
||||||
|
# folder
|
||||||
|
try:
|
||||||
|
for f in os.listdir(superfolder + purename + "/"):
|
||||||
|
if f.split(".")[-1] in ["png","jpg","jpeg","gif"]:
|
||||||
|
images.append("/" + superfolder + purename + "/" + f)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return images
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def getTrackImage(artists,title,fast=False):
|
def getTrackImage(artists,title,fast=False):
|
||||||
@ -164,24 +248,11 @@ def getTrackImage(artists,title,fast=False):
|
|||||||
if filename == "": filename = str(hash(obj))
|
if filename == "": filename = str(hash(obj))
|
||||||
filepath = "images/tracks/" + filename
|
filepath = "images/tracks/" + filename
|
||||||
|
|
||||||
images = []
|
images = local_files(artists=artists,title=title)
|
||||||
|
|
||||||
# add all images named like the tracks
|
|
||||||
for ext in ["png","jpg","jpeg","gif"]:
|
|
||||||
for num in [""] + [str(n) for n in range(0,10)]:
|
|
||||||
if os.path.exists(filepath + num + "." + ext):
|
|
||||||
images.append("/" + filepath + num + "." + ext)
|
|
||||||
|
|
||||||
# add images in a folder for that track
|
|
||||||
try:
|
|
||||||
for f in os.listdir(filepath + "/"):
|
|
||||||
if f.split(".")[-1] in ["png","jpg","jpeg","gif"]:
|
|
||||||
images.append("/" + filepath + "/" + f)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if len(images) != 0:
|
if len(images) != 0:
|
||||||
return random.choice(images)
|
#return random.choice(images)
|
||||||
|
return urllib.parse.quote(random.choice(images))
|
||||||
|
|
||||||
|
|
||||||
# check if custom image exists
|
# check if custom image exists
|
||||||
# if os.path.exists(filepath + ".png"):
|
# if os.path.exists(filepath + ".png"):
|
||||||
@ -246,24 +317,10 @@ def getArtistImage(artist,fast=False):
|
|||||||
filepath = "images/artists/" + filename
|
filepath = "images/artists/" + filename
|
||||||
#filepath_cache = "info/artists_cache/" + filename
|
#filepath_cache = "info/artists_cache/" + filename
|
||||||
|
|
||||||
images = []
|
images = local_files(artist=artist)
|
||||||
|
|
||||||
# add all images named like the artist
|
|
||||||
for ext in ["png","jpg","jpeg","gif"]:
|
|
||||||
for num in [""] + [str(n) for n in range(0,10)]:
|
|
||||||
if os.path.exists(filepath + num + "." + ext):
|
|
||||||
images.append("/" + filepath + num + "." + ext)
|
|
||||||
|
|
||||||
# add images in a folder for that artist
|
|
||||||
try:
|
|
||||||
for f in os.listdir(filepath + "/"):
|
|
||||||
if f.split(".")[-1] in ["png","jpg","jpeg","gif"]:
|
|
||||||
images.append("/" + filepath + "/" + f)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if len(images) != 0:
|
if len(images) != 0:
|
||||||
return random.choice(images)
|
#return random.choice(images)
|
||||||
|
return urllib.parse.quote(random.choice(images))
|
||||||
|
|
||||||
# check if custom image exists
|
# check if custom image exists
|
||||||
# if os.path.exists(filepath + ".png"):
|
# if os.path.exists(filepath + ".png"):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user