X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sima%2Flib%2Fsimadb.py;h=d08a6cca5edb46fac24fe60568989350d4f21b2b;hb=d81c669fc639184a61c7f9b514d179addbbddf17;hp=e52386cb6fe7554814f0323a70b6836fb8f95982;hpb=6e55148a76140b0e24435a4bcf2478174e2d90c8;p=mpd-sima.git diff --git a/sima/lib/simadb.py b/sima/lib/simadb.py index e52386c..d08a6cc 100644 --- a/sima/lib/simadb.py +++ b/sima/lib/simadb.py @@ -26,6 +26,8 @@ import sqlite3 from collections import deque from datetime import (datetime, timedelta) +from datetime import timezone + from sima.lib.meta import Artist, Album from sima.lib.track import Track @@ -435,10 +437,11 @@ class SimaDB: VALUES (?, ?)""", (trk_id, gen_id)) def add_history(self, track, date=None): - """Record last play date of track (ie. not a real exhautive play history). - :param track sima.lib.track.Track: track to add to history""" + """Record last play date of track (ie. not a real play history). + :param track sima.lib.track.Track: track to add to history + :param date datetime.datetime: UTC datetime object (use "datetime.now(timezone.utc)" is not set)""" if not date: - date = datetime.now() + date = datetime.now(timezone.utc) connection = self.get_database_connection() track_id = self.get_track(track, with_connection=connection) rows = connection.execute("SELECT * FROM history WHERE track = ? ", @@ -466,29 +469,38 @@ class SimaDB: """ :param sima.lib.meta.Artist needle: When specified, returns albums history for this artist. """ - date = datetime.utcnow() - timedelta(hours=duration) + date = datetime.now(timezone.utc) - timedelta(hours=duration) connection = self.get_database_connection() connection.row_factory = sqlite3.Row rows = connection.execute(""" SELECT albums.name AS name, albums.mbid as mbid, artists.name as artist, - artists.mbid as artist_mbib + artists.mbid as artist_mbib, + albumartists.name as albumartist, + albumartists.mbid as albumartist_mbib FROM history JOIN tracks ON history.track = tracks.id LEFT OUTER JOIN albums ON tracks.album = albums.id LEFT OUTER JOIN artists ON tracks.artist = artists.id + LEFT OUTER JOIN albumartists ON tracks.albumartist = albumartists.id WHERE history.last_play > ? AND albums.name NOT NULL AND artists.name NOT NULL ORDER BY history.last_play DESC""", (date.isoformat(' '),)) hist = list() for row in rows: vals = dict(row) - artist = Artist(name=vals.pop('artist'), - mbid=vals.pop('artist_mbib')) - if needle: - if needle != artist: + if needle: # Here use artist instead of albumartist + if needle != Artist(name=vals.get('artist'), + mbid=vals.get('artist_mbib')): continue - album = Album(**vals, artist=artist) + # Use albumartist / MBIDs if possible to build album artist + if not vals.get('albumartist'): + vals['albumartist'] = vals.get('artist') + if not vals.get('albumartist_mbib'): + vals['albumartist_mbib'] = vals.get('artist_mbib') + artist = Artist(name=vals.get('albumartist'), + mbid=vals.pop('albumartist_mbib')) + album = Album(**vals, Artist=artist) if hist and hist[-1] == album: # remove consecutive dupes continue @@ -501,7 +513,7 @@ class SimaDB: :param sima.lib.meta.Artist|sima.lib.meta.MetaContainer needle: When specified, returns history for this artist, it's actually testing the artist presence in history. :param sima.lib.meta.MetaContainer needle: When specified, returns history for these artists only """ - date = datetime.utcnow() - timedelta(hours=duration) + date = datetime.now(timezone.utc) - timedelta(hours=duration) connection = self.get_database_connection() connection.row_factory = sqlite3.Row rows = connection.execute(""" @@ -533,7 +545,7 @@ class SimaDB: return hist def fetch_genres_history(self, duration=__HIST_DURATION__, limit=20): - date = datetime.utcnow() - timedelta(hours=duration) + date = datetime.now(timezone.utc) - timedelta(hours=duration) connection = self.get_database_connection() rows = connection.execute(""" SELECT genres.name, artists.name @@ -558,7 +570,7 @@ class SimaDB: :param sima.lib.meta.Artist artist: limit history to this artist :param int duration: How long ago to fetch history from """ - date = datetime.utcnow() - timedelta(hours=duration) + date = datetime.now(timezone.utc) - timedelta(hours=duration) connection = self.get_database_connection() connection.row_factory = sqlite3.Row sql = """ @@ -683,6 +695,7 @@ class SimaDB: albums.mbid AS musicbrainz_album, tracks.title AS title, tracks.mbid AS musicbrainz_title, + tracks.file AS file, blocklist.id FROM blocklist LEFT OUTER JOIN artists ON blocklist.artist = artists.id