Skip to content

Commit

Permalink
WIP: search local database
Browse files Browse the repository at this point in the history
  • Loading branch information
patkub committed Nov 3, 2024
1 parent 092a527 commit 0ddc1e6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/database_songs/MockSongDatabaseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ def get_song_artist(self, name, artist):
"""
return None

def search_song_by_name(self, name, artist):
"""
Search for song by name and artist in database
@param name: song name
@param artist: artist name or None
@return: song, or None if not found
"""
return None

def save_song(self, song_info, lyrics, translation):
"""
Save song in database
Expand Down
26 changes: 26 additions & 0 deletions src/database_songs/SongDatabaseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ def get_song_artist(self, name, artist):
pass
return None

def search_song_by_name(self, name, artist):
"""
Search for song by name and artist in database
@param name: song name
@param artist: artist name or None
@return: song, or None if not found
"""
try:
if self.con:
cur = self.con.cursor()
query_string = (
"SELECT * FROM songs WHERE full_title LIKE '%{full_title}%'".format(
full_title=name
)
)
if artist:
query_string = "SELECT * FROM songs WHERE full_title LIKE '%{full_title}%' AND artist LIKE '%{artist}%'".format(
full_title=name, artist=artist
)
res = cur.execute(query_string)
if res:
return res.fetchone()
except sqlite3.Error:
pass
return None

def save_song(self, song_info, lyrics, translation):
"""
Save song in database
Expand Down
38 changes: 33 additions & 5 deletions src/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,39 @@ def process_song(song, artist, access_keys, refresh, genius_patch):
@param refresh: skip database and refresh song
@param genius_patch: use patched version of Genius api
"""

#
# Fetch song from database
#
song_db_handler = SongDatabaseHandler()
song_db_con = song_db_handler.setup_song_database()

class DBSongInfo:
pass

if not refresh: # pragma: no cover
# search in database
# TODO: only works if it finds an exact match due to accents
# TODO(works): python3 src/songs.py "ucide ea" "Mihail"
# TODO(broken): python3 src/songs.py "ma ucide ea" "Mihail"
search_res = song_db_handler.search_song_by_name(song, artist)
if search_res and len(search_res) >= 5:
# already have this song saved in database
song_info = DBSongInfo()
song_info.full_title = search_res[0]
song_info.url = search_res[2]

song_lyrics = search_res[3]
english_translation = search_res[4]
# display original and English translated lyrics side-by-side
ConsoleDisplayLyrics.display_lyrics(
song_info, song_lyrics, english_translation
)
# song fetched from database, end
# TODO: remove print
print("DONE, used only DB")
return

#
# Fetch song lyrics from Genius
#
Expand All @@ -48,11 +81,6 @@ def process_song(song, artist, access_keys, refresh, genius_patch):
# get the song lyrics
song_lyrics = song_info.lyrics

#
# Fetch song from database
#
song_db_handler = SongDatabaseHandler()
song_db_con = song_db_handler.setup_song_database()
if not refresh and song_db_con: # pragma: no cover
# fetch song from database
song_res = song_db_handler.get_song_artist(
Expand Down

0 comments on commit 0ddc1e6

Please sign in to comment.