Files
KotoPad/gui/modules/pads/pad_name.py
2023-01-17 18:37:45 +02:00

49 lines
1.5 KiB
Python

import validators
import pafy
import sclib
from modules.spotify.spotify_dl import Spotify
from modules.anonfiles.anonfiles import Anonfiles
import urllib.parse
import os.path
import json
def get_button_name(path: str, length: int = 24) -> str:
if not os.path.exists('temp'):
os.mkdir('temp')
if not os.path.isfile('temp/' + '0' * 32):
with open('temp/' + '0' * 32, 'w') as f:
f.write('{}')
try:
with open('temp/' + '0' * 32) as f:
name_cache = json.load(f)
except Exception as e:
print(e)
with open('temp/' + '0' * 32) as f:
f.write('{}')
name_cache = dict()
if name_cache.get(path):
return name_cache.get(path)
if validators.url(path):
if 'spotify' in path.lower():
name = (lambda x: f"{x.artist} - {x.name}")(Spotify().get_song(path))
elif 'youtu' in path.lower():
name = (lambda x: f"{x.title} ({x.author})")(pafy.new(path))
elif 'anonfiles' in path.lower():
name = urllib.parse.quote(Anonfiles.get_direct(path), safe=':/').split('/')[-1]
elif 'soundcloud' in path.lower():
name = (lambda x: f"{x.artist} - {x.title}")(sclib.SoundcloudAPI().resolve(path))
else:
name = path.split('/')[-1]
else:
name = os.path.split(path)[-1]
name_cache[path] = name[:length:]
with open('temp/' + '0' * 32, 'w') as f:
json.dump(name_cache, f)
return name[:length:]