From f060608b6f005faee1d4e826d6e41da59539d8c8 Mon Sep 17 00:00:00 2001 From: John William Ruth Miller Date: Thu, 12 Dec 2024 22:35:17 -0800 Subject: [PATCH] Move Song and Album imports within classes --- lyricsgenius/types/album.py | 155 +++++++++++++++++------------------- lyricsgenius/types/song.py | 118 +++++++++++++-------------- 2 files changed, 134 insertions(+), 139 deletions(-) diff --git a/lyricsgenius/types/album.py b/lyricsgenius/types/album.py index b6e8303..df8e929 100644 --- a/lyricsgenius/types/album.py +++ b/lyricsgenius/types/album.py @@ -1,7 +1,6 @@ from ..utils import convert_to_datetime from .base import BaseEntity from .artist import Artist -from .song import Song class Album(BaseEntity): @@ -9,117 +8,113 @@ class Album(BaseEntity): def __init__(self, client, json_dict, tracks): body = json_dict - super().__init__(body['id']) + super().__init__(body["id"]) self._body = body self._client = client - self.artist = Artist(client, body['artist']) + self.artist = Artist(client, body["artist"]) self.tracks = tracks - self.release_date_components = convert_to_datetime(body.get('release_date_components')) - - self.api_path = body.get('api_path') - self.cover_art_thumbnail_url = body.get('cover_art_thumbnail_url') - self.cover_art_url = body.get('cover_art_url') - self.full_title = body.get('full_title') - self.name = body.get('name') - self.name_with_artist = body.get('name_with_artist') - self.url = body.get('url') + self.release_date_components = convert_to_datetime( + body.get("release_date_components") + ) + + self.api_path = body.get("api_path") + self.cover_art_thumbnail_url = body.get("cover_art_thumbnail_url") + self.cover_art_url = body.get("cover_art_url") + self.full_title = body.get("full_title") + self.name = body.get("name") + self.name_with_artist = body.get("name_with_artist") + self.url = body.get("url") def to_dict(self): body = super().to_dict() - body['tracks'] = [track.to_dict() for track in self.tracks] + body["tracks"] = [track.to_dict() for track in self.tracks] return body - def to_json(self, - filename=None, - sanitize=True, - ensure_ascii=True): + def to_json(self, filename=None, sanitize=True, ensure_ascii=True): data = self.to_dict() - return super().to_json(data=data, - filename=filename, - sanitize=sanitize, - ensure_ascii=ensure_ascii) - - def to_text(self, - filename=None, - sanitize=True): - data = ' '.join(track.song.lyrics for track in self.tracks) - - return super().to_text(data=data, - filename=filename, - sanitize=sanitize) - - def save_lyrics(self, - filename=None, - extension='json', - overwrite=False, - ensure_ascii=True, - sanitize=True, - verbose=True): + return super().to_json( + data=data, filename=filename, sanitize=sanitize, ensure_ascii=ensure_ascii + ) + + def to_text(self, filename=None, sanitize=True): + data = " ".join(track.song.lyrics for track in self.tracks) + + return super().to_text(data=data, filename=filename, sanitize=sanitize) + + def save_lyrics( + self, + filename=None, + extension="json", + overwrite=False, + ensure_ascii=True, + sanitize=True, + verbose=True, + ): if filename is None: - filename = 'Lyrics_' + self.name.replace(' ', '') + filename = "Lyrics_" + self.name.replace(" ", "") - return super().save_lyrics(filename=filename, - extension=extension, - overwrite=overwrite, - ensure_ascii=ensure_ascii, - sanitize=sanitize, - verbose=verbose) + return super().save_lyrics( + filename=filename, + extension=extension, + overwrite=overwrite, + ensure_ascii=ensure_ascii, + sanitize=sanitize, + verbose=verbose, + ) class Track(BaseEntity): """docstring for Track""" def __init__(self, client, json_dict, lyrics): + from .song import Song + body = json_dict - super().__init__(body['song']['id']) + super().__init__(body["song"]["id"]) self._body = body - self.song = Song(client, body['song'], lyrics) + self.song = Song(client, body["song"], lyrics) - self.number = body['number'] + self.number = body["number"] def to_dict(self): body = super().to_dict() - body['song'] = self.song.to_dict() + body["song"] = self.song.to_dict() return body - def to_json(self, - filename=None, - sanitize=True, - ensure_ascii=True): + def to_json(self, filename=None, sanitize=True, ensure_ascii=True): data = self.to_dict() - return super().to_json(data=data, - filename=filename, - sanitize=sanitize, - ensure_ascii=ensure_ascii) + return super().to_json( + data=data, filename=filename, sanitize=sanitize, ensure_ascii=ensure_ascii + ) - def to_text(self, - filename=None, - sanitize=True): + def to_text(self, filename=None, sanitize=True): data = self.song.lyrics - return super().to_text(data=data, - filename=filename, - sanitize=sanitize) - - def save_lyrics(self, - filename=None, - extension='json', - overwrite=False, - ensure_ascii=True, - sanitize=True, - verbose=True): + return super().to_text(data=data, filename=filename, sanitize=sanitize) + + def save_lyrics( + self, + filename=None, + extension="json", + overwrite=False, + ensure_ascii=True, + sanitize=True, + verbose=True, + ): if filename is None: - filename = 'Lyrics_{:02d}_{}'.format(self.number, self.song.title) - filename = filename.replace(' ', '') - - return super().save_lyrics(filename=filename, - extension=extension, - overwrite=overwrite, - ensure_ascii=ensure_ascii, - sanitize=sanitize, - verbose=verbose) + filename = "Lyrics_{:02d}_{}".format(self.number, self.song.title) + filename = filename.replace(" ", "") + + return super().save_lyrics( + filename=filename, + extension=extension, + overwrite=overwrite, + ensure_ascii=ensure_ascii, + sanitize=sanitize, + verbose=verbose, + ) def __repr__(self): name = self.__class__.__name__ diff --git a/lyricsgenius/types/song.py b/lyricsgenius/types/song.py index d7a2e85..9e4becf 100644 --- a/lyricsgenius/types/song.py +++ b/lyricsgenius/types/song.py @@ -4,84 +4,81 @@ from filecmp import cmp -from .base import BaseEntity, Stats -from .album import Album from .artist import Artist +from .base import BaseEntity, Stats class Song(BaseEntity): """A song from the Genius.com database.""" def __init__(self, client, json_dict, lyrics=""): + from .album import Album + body = json_dict - super().__init__(body['id']) + super().__init__(body["id"]) self._body = body self._client = client - self.artist = body['primary_artist']['name'] + self.artist = body["primary_artist"]["name"] self.lyrics = lyrics if lyrics else "" - self.primary_artist = Artist(client, body['primary_artist']) - self.stats = Stats(body['stats']) - self.album = Album(client, body['album'], []) if body.get('album') else None - - self.annotation_count = body['annotation_count'] - self.api_path = body['api_path'] - self.full_title = body['full_title'] - self.header_image_thumbnail_url = body['header_image_thumbnail_url'] - self.header_image_url = body['header_image_url'] - self.lyrics_owner_id = body['lyrics_owner_id'] - self.lyrics_state = body['lyrics_state'] - self.path = body['path'] - self.pyongs_count = body['pyongs_count'] - self.song_art_image_thumbnail_url = body['song_art_image_thumbnail_url'] - self.song_art_image_url = body['song_art_image_url'] - self.title = body['title'] - self.title_with_featured = body['title_with_featured'] - self.url = body['url'] + self.primary_artist = Artist(client, body["primary_artist"]) + self.stats = Stats(body["stats"]) + self.album = Album(client, body["album"], []) if body.get("album") else None + + self.annotation_count = body["annotation_count"] + self.api_path = body["api_path"] + self.full_title = body["full_title"] + self.header_image_thumbnail_url = body["header_image_thumbnail_url"] + self.header_image_url = body["header_image_url"] + self.lyrics_owner_id = body["lyrics_owner_id"] + self.lyrics_state = body["lyrics_state"] + self.path = body["path"] + self.pyongs_count = body["pyongs_count"] + self.song_art_image_thumbnail_url = body["song_art_image_thumbnail_url"] + self.song_art_image_url = body["song_art_image_url"] + self.title = body["title"] + self.title_with_featured = body["title_with_featured"] + self.url = body["url"] def to_dict(self): body = super().to_dict() - body['artist'] = self.artist - body['lyrics'] = self.lyrics + body["artist"] = self.artist + body["lyrics"] = self.lyrics return body - def to_json(self, - filename=None, - sanitize=True, - ensure_ascii=True): + def to_json(self, filename=None, sanitize=True, ensure_ascii=True): data = self.to_dict() - return super().to_json(data=data, - filename=filename, - sanitize=sanitize, - ensure_ascii=ensure_ascii) - - def to_text(self, - filename=None, - sanitize=True): + return super().to_json( + data=data, filename=filename, sanitize=sanitize, ensure_ascii=ensure_ascii + ) + + def to_text(self, filename=None, sanitize=True): data = self.lyrics - return super().to_text(data=data, - filename=filename, - sanitize=sanitize) + return super().to_text(data=data, filename=filename, sanitize=sanitize) - def save_lyrics(self, - filename=None, - extension='json', - overwrite=False, - ensure_ascii=True, - sanitize=True, - verbose=True): + def save_lyrics( + self, + filename=None, + extension="json", + overwrite=False, + ensure_ascii=True, + sanitize=True, + verbose=True, + ): if filename is None: - filename = "Lyrics_{}_{}".format(self.artist.replace(" ", ""), - self.title.replace(" ", "") - ).lower() + filename = "Lyrics_{}_{}".format( + self.artist.replace(" ", ""), self.title.replace(" ", "") + ).lower() - return super().save_lyrics(filename=filename, - extension=extension, - overwrite=overwrite, - ensure_ascii=ensure_ascii, - sanitize=sanitize, - verbose=verbose) + return super().save_lyrics( + filename=filename, + extension=extension, + overwrite=overwrite, + ensure_ascii=ensure_ascii, + sanitize=sanitize, + verbose=verbose, + ) def __str__(self): """Return a string representation of the Song object.""" @@ -90,9 +87,12 @@ def __str__(self): else: lyr = self.lyrics[:100] return '"{title}" by {artist}:\n {lyrics}'.format( - title=self.title, artist=self.artist, lyrics=lyr.replace('\n', '\n ')) + title=self.title, artist=self.artist, lyrics=lyr.replace("\n", "\n ") + ) def __cmp__(self, other): - return (cmp(self.title, other.title) - and cmp(self.artist, other.artist) - and cmp(self.lyrics, other.lyrics)) + return ( + cmp(self.title, other.title) + and cmp(self.artist, other.artist) + and cmp(self.lyrics, other.lyrics) + )