Skip to content

Commit

Permalink
Move Song and Album imports within classes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwmillr committed Dec 13, 2024
1 parent 67f900b commit f060608
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 139 deletions.
155 changes: 75 additions & 80 deletions lyricsgenius/types/album.py
Original file line number Diff line number Diff line change
@@ -1,125 +1,120 @@
from ..utils import convert_to_datetime
from .base import BaseEntity
from .artist import Artist
from .song import Song


class Album(BaseEntity):
"""An album from the Genius.com database."""

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__
Expand Down
118 changes: 59 additions & 59 deletions lyricsgenius/types/song.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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)
)

0 comments on commit f060608

Please sign in to comment.