deemix-py/deemix/types/Album.py

136 lines
5.1 KiB
Python
Raw Normal View History

from deemix.utils import removeDuplicateArtists, removeFeatures
2021-01-31 16:59:15 +00:00
from deemix.types.Artist import Artist
from deemix.types.Date import Date
from deemix.types.Picture import Picture
from deemix.types import VARIOUS_ARTISTS
2021-01-31 16:59:15 +00:00
class Album:
def __init__(self, alb_id="0", title="", pic_md5=""):
self.id = alb_id
2021-01-31 16:59:15 +00:00
self.title = title
self.pic = Picture(pic_md5, "cover")
2021-01-31 16:59:15 +00:00
self.artist = {"Main": []}
self.artists = []
self.mainArtist = None
self.date = Date()
self.dateString = ""
self.trackTotal = "0"
2021-01-31 16:59:15 +00:00
self.discTotal = "0"
self.embeddedCoverPath = ""
self.embeddedCoverURL = ""
2021-01-31 16:59:15 +00:00
self.explicit = False
self.genre = []
self.barcode = "Unknown"
2021-01-31 16:59:15 +00:00
self.label = "Unknown"
self.copyright = ""
2021-01-31 16:59:15 +00:00
self.recordType = "album"
self.bitrate = 0
self.rootArtist = None
2021-01-31 16:59:15 +00:00
self.variousArtists = None
2021-12-23 18:02:33 +00:00
self.playlistID = None
self.owner = None
self.isPlaylist = False
2021-01-31 16:59:15 +00:00
def parseAlbum(self, albumAPI):
self.title = albumAPI['title']
# Getting artist image ID
# ex: https://e-cdns-images.dzcdn.net/images/artist/f2bc007e9133c946ac3c3907ddc5d2ea/56x56-000000-80-0-0.jpg
2021-12-23 18:02:33 +00:00
art_pic = albumAPI['artist'].get('picture_small')
if art_pic: art_pic = art_pic[art_pic.find('artist/') + 7:-24]
else: art_pic = ""
2021-01-31 16:59:15 +00:00
self.mainArtist = Artist(
albumAPI['artist']['id'],
albumAPI['artist']['name'],
"Main",
art_pic
2021-01-31 16:59:15 +00:00
)
if albumAPI.get('root_artist'):
art_pic = albumAPI['root_artist']['picture_small']
art_pic = art_pic[art_pic.find('artist/') + 7:-24]
2021-01-31 16:59:15 +00:00
self.rootArtist = Artist(
albumAPI['root_artist']['id'],
albumAPI['root_artist']['name'],
"Root",
art_pic
2021-01-31 16:59:15 +00:00
)
for artist in albumAPI['contributors']:
isVariousArtists = str(artist['id']) == VARIOUS_ARTISTS
isMainArtist = artist['role'] == "Main"
if isVariousArtists:
self.variousArtists = Artist(
art_id = artist['id'],
2021-01-31 16:59:15 +00:00
name = artist['name'],
role = artist['role']
)
continue
if artist['name'] not in self.artists:
self.artists.append(artist['name'])
if isMainArtist or artist['name'] not in self.artist['Main'] and not isMainArtist:
if not artist['role'] in self.artist:
self.artist[artist['role']] = []
self.artist[artist['role']].append(artist['name'])
self.trackTotal = albumAPI['nb_tracks']
self.recordType = albumAPI['record_type']
self.barcode = albumAPI.get('upc', self.barcode)
self.label = albumAPI.get('label', self.label)
self.explicit = bool(albumAPI.get('explicit_lyrics', False))
2021-12-23 18:02:33 +00:00
release_date = albumAPI.get('release_date')
if 'physical_release_date' in albumAPI:
release_date = albumAPI['physical_release_date']
if release_date:
self.date.day = release_date[8:10]
self.date.month = release_date[5:7]
self.date.year = release_date[0:4]
self.date.fixDayMonth()
2021-01-31 16:59:15 +00:00
2021-12-23 18:25:45 +00:00
self.discTotal = albumAPI.get('nb_disk', "1")
2021-01-31 16:59:15 +00:00
self.copyright = albumAPI.get('copyright')
2021-12-23 18:02:33 +00:00
if self.pic.md5 == "":
if albumAPI.get('md5_image'):
self.pic.md5 = albumAPI['md5_image']
elif albumAPI.get('cover_small'):
# Getting album cover MD5
# ex: https://e-cdns-images.dzcdn.net/images/cover/2e018122cb56986277102d2041a592c8/56x56-000000-80-0-0.jpg
alb_pic = albumAPI['cover_small']
self.pic.md5 = alb_pic[alb_pic.find('cover/') + 6:-24]
2021-01-31 16:59:15 +00:00
if albumAPI.get('genres') and len(albumAPI['genres'].get('data', [])) > 0:
for genre in albumAPI['genres']['data']:
self.genre.append(genre['name'])
def makePlaylistCompilation(self, playlist):
self.variousArtists = playlist.variousArtists
self.mainArtist = playlist.mainArtist
self.title = playlist.title
self.rootArtist = playlist.rootArtist
self.artist = playlist.artist
self.artists = playlist.artists
self.trackTotal = playlist.trackTotal
self.recordType = playlist.recordType
self.barcode = playlist.barcode
self.label = playlist.label
self.explicit = playlist.explicit
self.date = playlist.date
self.discTotal = playlist.discTotal
2021-12-19 17:09:05 +00:00
self.playlistID = playlist.playlistID
2021-01-31 16:59:15 +00:00
self.owner = playlist.owner
self.pic = playlist.pic
self.isPlaylist = True
2021-01-31 16:59:15 +00:00
def removeDuplicateArtists(self):
"""Removes duplicate artists for both artist array and artists dict"""
2021-01-31 16:59:15 +00:00
(self.artist, self.artists) = removeDuplicateArtists(self.artist, self.artists)
def getCleanTitle(self):
"""Removes featuring from the album name"""
2021-01-31 16:59:15 +00:00
return removeFeatures(self.title)