X-Git-Url: http://git.kaliko.me/?a=blobdiff_plain;f=sima%2Flib%2Fsimadb.py;h=d060da32a0af4be6631e45c6f1237082867da44e;hb=9a526a5c96d9cb5622d2d0e85eb59a4a22d4d994;hp=ba2809755da33f0d49a7b9240547acb30e4c12bb;hpb=2f094f7b09621b6360c48eca45a27ea49afef130;p=mpd-sima.git diff --git a/sima/lib/simadb.py b/sima/lib/simadb.py index ba28097..d060da3 100644 --- a/sima/lib/simadb.py +++ b/sima/lib/simadb.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (c) 2009-2013, 2019 Jack Kaliko +# Copyright (c) 2009-2013, 2019-2020 kaliko # Copyright (c) 2009, Eric Casteleijn # Copyright (c) 2008 Rick van Hattem # @@ -42,20 +42,17 @@ class SimaDBError(Exception): """ Exceptions. """ - pass class SimaDBAccessError(SimaDBError): """Error on accessing DB file""" - pass class SimaDBNoFile(SimaDBError): """No DB file present""" - pass -class SimaDB(object): +class SimaDB: "SQLite management" def __init__(self, db_path=None): @@ -324,6 +321,8 @@ class SimaDB(object): def get_artists_history(self, artists, duration=__HIST_DURATION__): """ + :param list artists: list of object that can evaluate equality with + artist name, iterable of str or Artist object """ date = datetime.utcnow() - timedelta(hours=duration) connection = self.get_database_connection() @@ -437,14 +436,14 @@ class SimaDB(object): connection = with_connection else: connection = self.get_database_connection() - artists_ids = set([row[0] for row in connection.execute( - "SELECT id FROM artists")]) - artist_2_artist_ids = set([row[0] for row in connection.execute( - "SELECT artist FROM black_list")] + - [row[0] for row in connection.execute( - "SELECT artist FROM albums")] + - [row[0] for row in connection.execute( - "SELECT artist FROM tracks")]) + artists_ids = {row[0] for row in connection.execute( + "SELECT id FROM artists")} + artist_2_artist_ids = {row[0] for row in connection.execute( + "SELECT artist FROM black_list")} | { + row[0] for row in connection.execute( + "SELECT artist FROM albums")} | { + row[0] for row in connection.execute( + "SELECT artist FROM tracks")} orphans = [(orphan,) for orphan in artists_ids - artist_2_artist_ids] connection.executemany('DELETE FROM artists WHERE id = (?);', orphans) if not with_connection: @@ -457,14 +456,14 @@ class SimaDB(object): connection = with_connection else: connection = self.get_database_connection() - orphan_black_ids = set([row[0] for row in connection.execute( + orphan_black_ids = {row[0] for row in connection.execute( """SELECT albums.id FROM albums LEFT JOIN black_list ON albums.id = black_list.album - WHERE ( black_list.album IS NULL )""")]) - orphan_tracks_ids = set([row[0] for row in connection.execute( + WHERE ( black_list.album IS NULL )""")} + orphan_tracks_ids = {row[0] for row in connection.execute( """SELECT albums.id FROM albums LEFT JOIN tracks ON albums.id = tracks.album - WHERE tracks.album IS NULL""")]) + WHERE tracks.album IS NULL""")} orphans = [(orphan,) for orphan in orphan_black_ids & orphan_tracks_ids] connection.executemany('DELETE FROM albums WHERE id = (?);', orphans) if not with_connection: @@ -477,14 +476,14 @@ class SimaDB(object): connection = with_connection else: connection = self.get_database_connection() - hist_orphan_ids = set([row[0] for row in connection.execute( + hist_orphan_ids = {row[0] for row in connection.execute( """SELECT tracks.id FROM tracks LEFT JOIN history ON tracks.id = history.track - WHERE history.track IS NULL""")]) - black_list_orphan_ids = set([row[0] for row in connection.execute( + WHERE history.track IS NULL""")} + black_list_orphan_ids = {row[0] for row in connection.execute( """SELECT tracks.id FROM tracks LEFT JOIN black_list ON tracks.id = black_list.track - WHERE black_list.track IS NULL""")]) + WHERE black_list.track IS NULL""")} orphans = [(orphan,) for orphan in hist_orphan_ids & black_list_orphan_ids] connection.executemany('DELETE FROM tracks WHERE id = (?);', orphans) if not with_connection: