deemix-py/deemix/utils/__init__.py

76 lines
2.1 KiB
Python
Raw Permalink Normal View History

2020-09-20 08:35:05 +00:00
import string
from deezer import TrackFormats
import os
2020-09-20 08:35:05 +00:00
2021-06-07 18:25:51 +00:00
USER_AGENT_HEADER = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " \
"Chrome/79.0.3945.130 Safari/537.36"
def canWrite(folder):
return os.access(folder, os.W_OK)
2020-09-24 17:20:01 +00:00
def generateReplayGainString(trackGain):
return "{0:.2f} dB".format((float(trackGain) + 18.4) * -1)
2021-03-19 13:31:32 +00:00
def getBitrateNumberFromText(txt):
2020-09-20 08:35:05 +00:00
txt = str(txt).lower()
if txt in ['flac', 'lossless', '9']:
return TrackFormats.FLAC
2021-04-10 09:53:52 +00:00
if txt in ['mp3', '320', '3']:
return TrackFormats.MP3_320
2021-04-10 09:53:52 +00:00
if txt in ['128', '1']:
return TrackFormats.MP3_128
2021-04-10 09:53:52 +00:00
if txt in ['360', '360_hq', '15']:
return TrackFormats.MP4_RA3
2021-04-10 09:53:52 +00:00
if txt in ['360_mq', '14']:
return TrackFormats.MP4_RA2
2021-04-10 09:53:52 +00:00
if txt in ['360_lq', '13']:
return TrackFormats.MP4_RA1
2021-04-10 09:53:52 +00:00
return None
2020-09-20 08:35:05 +00:00
2021-04-10 09:53:52 +00:00
def changeCase(txt, case_type):
if case_type == "lower":
return txt.lower()
if case_type == "upper":
return txt.upper()
if case_type == "start":
return string.capwords(txt)
if case_type == "sentence":
return txt.capitalize()
return str
2020-09-20 08:35:05 +00:00
def removeFeatures(title):
clean = title
if "(feat." in clean.lower():
pos = clean.lower().find("(feat.")
tempTrack = clean[:pos]
if ")" in clean:
tempTrack += clean[clean.find(")", pos + 1) + 1:]
clean = tempTrack.strip()
clean = ' '.join(clean.split())
2020-09-20 08:35:05 +00:00
return clean
def andCommaConcat(lst):
tot = len(lst)
result = ""
for i, art in enumerate(lst):
result += art
if tot != i + 1:
if tot - 1 == i + 1:
result += " & "
else:
result += ", "
return result
def uniqueArray(arr):
for iPrinc, namePrinc in enumerate(arr):
for iRest, nRest in enumerate(arr):
if iPrinc!=iRest and namePrinc.lower() in nRest.lower():
del arr[iRest]
return arr
2021-01-31 16:59:15 +00:00
def removeDuplicateArtists(artist, artists):
artists = uniqueArray(artists)
for role in artist.keys():
artist[role] = uniqueArray(artist[role])
return (artist, artists)