summaryrefslogtreecommitdiff
path: root/models/track.py
diff options
context:
space:
mode:
authorMichaël Ball <michael.ball@gmail.com>2019-06-30 11:12:05 +0100
committerMichaël Ball <michael.ball@gmail.com>2019-06-30 11:12:05 +0100
commita87517a0b385f1a68dd80e7dabf7843955c95ab9 (patch)
tree696f1e2a45ab7c0db57ce562517790cb73247b53 /models/track.py
parent1acbc1e85d6a0af47c236c48b8c5a1b504871d96 (diff)
Diffstat (limited to 'models/track.py')
-rw-r--r--models/track.py337
1 files changed, 196 insertions, 141 deletions
diff --git a/models/track.py b/models/track.py
index 2a92558..81c60fc 100644
--- a/models/track.py
+++ b/models/track.py
@@ -6,12 +6,10 @@ from models.artist import Artist
from models.album import Album
from models.base import BaseModel
-
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)
class Track(BaseModel):
-
def __init__(self, db, id=None, **kwargs):
self._db = db
@@ -19,9 +17,10 @@ class Track(BaseModel):
if id is not None:
for row in self._db.execute("SELECT * FROM track WHERE id = ?",
- (id,)):
- for key in ["id", "tracknumber", "name", "grouping",
- "filename"]:
+ (id, )):
+ for key in [
+ "id", "tracknumber", "name", "grouping", "filename"
+ ]:
setattr(self, key, row[key])
self.__data[key] = row[key]
else:
@@ -33,7 +32,7 @@ class Track(BaseModel):
delete_sql = "DELETE FROM track WHERE id = ?"
with self._db.conn:
- self._db.execute(delete_sql, (self.id,))
+ self._db.execute(delete_sql, (self.id, ))
# If there is an old album, remove it if it no longer has any
# tracks
@@ -46,7 +45,7 @@ class Track(BaseModel):
if old_album:
self._db.execute("DELETE FROM album_track WHERE track_id = ?",
- (self.id,))
+ (self.id, ))
if not old_album.tracks:
old_album.delete()
@@ -60,8 +59,9 @@ class Track(BaseModel):
old_artists = self.artists
for old_artist in old_artists:
- self._db.execute("DELETE FROM artist_track WHERE track_id = "
- "?", (self.id,))
+ self._db.execute(
+ "DELETE FROM artist_track WHERE track_id = "
+ "?", (self.id, ))
if not old_artist.tracks:
old_artist.delete()
@@ -72,7 +72,6 @@ class Track(BaseModel):
def db(self):
return self._db
-
@db.setter
def db(self, db):
self._db = db
@@ -82,13 +81,17 @@ class Track(BaseModel):
if not hasattr(self, "_album"):
setattr(self, "_album", None)
- for row in self._db.execute("SELECT album.* FROM album INNER "
- "JOIN album_track ON album.id = "
- "album_track.album_id WHERE "
- "track_id = ? LIMIT 1", (self.id,)):
- setattr(self, "_album", Album(id=row["id"], db=self._db,
- name=row["name"],
- date=row["date"]))
+ for row in self._db.execute(
+ "SELECT album.* FROM album INNER "
+ "JOIN album_track ON album.id = "
+ "album_track.album_id WHERE "
+ "track_id = ? LIMIT 1", (self.id, )):
+ setattr(
+ self, "_album",
+ Album(id=row["id"],
+ db=self._db,
+ name=row["name"],
+ date=row["date"]))
return self._album
@@ -98,16 +101,17 @@ class Track(BaseModel):
cursor = self._db.cursor()
setattr(self, "_artists", [])
- for row in cursor.execute("SELECT artist.* FROM artist INNER JOIN "
- "artist_track ON artist.id = "
- "artist_track.artist_id WHERE "
- "artist_track.track_id = ?",
- (self.id,)):
- self._artists.append(Artist(id=row["id"], db=self._db,
- name=row["name"],
- sortname=row["sortname"],
- musicbrainz_artistid=row[
- "musicbrainz_artistid"]))
+ for row in cursor.execute(
+ "SELECT artist.* FROM artist INNER JOIN "
+ "artist_track ON artist.id = "
+ "artist_track.artist_id WHERE "
+ "artist_track.track_id = ?", (self.id, )):
+ self._artists.append(
+ Artist(id=row["id"],
+ db=self._db,
+ name=row["name"],
+ sortname=row["sortname"],
+ musicbrainz_artistid=row["musicbrainz_artistid"]))
return self._artists
@@ -149,20 +153,22 @@ class Track(BaseModel):
rows = None
if musicbrainz_artistid:
- rows = c.execute("SELECT * FROM artist WHERE "
- "musicbrainz_artistid = ?",
- (musicbrainz_artistid,))
+ rows = c.execute(
+ "SELECT * FROM artist WHERE "
+ "musicbrainz_artistid = ?", (musicbrainz_artistid, ))
else:
rows = c.execute("SELECT * FROM artist WHERE name = ?",
- (artist_name,))
+ (artist_name, ))
row = rows.fetchone()
if row:
- artist = Artist(id=row["id"], db=self._db, name=row["name"],
- sortname=row["sortname"],
- musicbrainz_artistid=row[
- "musicbrainz_artistid"])
+ artist = Artist(
+ id=row["id"],
+ db=self._db,
+ name=row["name"],
+ sortname=row["sortname"],
+ musicbrainz_artistid=row["musicbrainz_artistid"])
if artist.name != artist_name:
c.execute("UPDATE artist SET name = ? WHERE id = ?",
@@ -175,15 +181,16 @@ class Track(BaseModel):
artist.sortname = artistsort
else:
- c.execute("INSERT INTO artist (name, sortname, "
- "musicbrainz_artistid) VALUES(?, ?, ?)",
- (artist_name, artistsort, musicbrainz_artistid))
+ c.execute(
+ "INSERT INTO artist (name, sortname, "
+ "musicbrainz_artistid) VALUES(?, ?, ?)",
+ (artist_name, artistsort, musicbrainz_artistid))
- artist = Artist(
- id=c.lastrowid, db=self._db, name=artist_name,
- sortname=artistsort,
- musicbrainz_artistid=musicbrainz_artistid
- )
+ artist = Artist(id=c.lastrowid,
+ db=self._db,
+ name=artist_name,
+ sortname=artistsort,
+ musicbrainz_artistid=musicbrainz_artistid)
i += 1
@@ -211,40 +218,48 @@ class Track(BaseModel):
if mb_albumid:
rows = c.execute(
"SELECT * FROM album WHERE musicbrainz_albumid = ?",
- (mb_albumid,)
- )
+ (mb_albumid, ))
row = rows.fetchone()
if row:
- album = Album(id=row["id"], db=self._db, name=row["name"],
+ album = Album(id=row["id"],
+ db=self._db,
+ name=row["name"],
date=row["date"],
musicbrainz_albumid=row["musicbrainz_albumid"])
else:
- c.execute("INSERT INTO album (name, `date`, "
- "musicbrainz_albumid) VALUES (?, ?, ?)",
- (album_name, album_date, mb_albumid))
+ c.execute(
+ "INSERT INTO album (name, `date`, "
+ "musicbrainz_albumid) VALUES (?, ?, ?)",
+ (album_name, album_date, mb_albumid))
- album = Album(id=c.lastrowid, db=self._db, name=album_name,
- date=album_date, musicbrainz_albumid=mb_albumid)
+ album = Album(id=c.lastrowid,
+ db=self._db,
+ name=album_name,
+ date=album_date,
+ musicbrainz_albumid=mb_albumid)
elif album_name:
rows = c.execute(
"SELECT album.* FROM album INNER JOIN album_artist ON "
"album_artist.album_id = album.id WHERE album.name = ? "
- "AND artist_id = ?", (album_name, artist.id)
- )
+ "AND artist_id = ?", (album_name, artist.id))
row = rows.fetchone()
if row:
- album = Album(id=row["id"], db=self._db, name=row["name"],
+ album = Album(id=row["id"],
+ db=self._db,
+ name=row["name"],
date=row["date"],
musicbrainz_albumid=row["musicbrainz_albumid"])
else:
c.execute("INSERT INTO album (name, `date`) VALUES (?, ?)",
(album_name, album_date))
- album = Album(id=c.lastrowid, db=self._db, name=album_name,
+ album = Album(id=c.lastrowid,
+ db=self._db,
+ name=album_name,
date=album_date)
if album:
@@ -277,9 +292,10 @@ class Track(BaseModel):
except KeyError:
pass
- c.execute("UPDATE track SET tracknumber = ?, name = ?, grouping = ? "
- "WHERE id = ?", (track_number, track_name, track_grouping,
- self.id))
+ c.execute(
+ "UPDATE track SET tracknumber = ?, name = ?, grouping = ? "
+ "WHERE id = ?",
+ (track_number, track_name, track_grouping, self.id))
# If there is an old album, remove it if it no longer has any tracks
try:
@@ -290,7 +306,7 @@ class Track(BaseModel):
old_album = self.album
if old_album:
c.execute("DELETE FROM album_track WHERE track_id = ?",
- (self.id,))
+ (self.id, ))
# If there are old artists, remove them if they no longer have
# any tracks
@@ -302,12 +318,13 @@ class Track(BaseModel):
for old_artist in old_artists:
c.execute("DELETE FROM artist_track WHERE track_id = ?",
- (self.id,))
+ (self.id, ))
if album:
try:
- c.execute("INSERT INTO album_track (album_id, track_id) "
- "VALUES(?, ?)", (album.id, self.id))
+ c.execute(
+ "INSERT INTO album_track (album_id, track_id) "
+ "VALUES(?, ?)", (album.id, self.id))
except sqlite3.IntegrityError:
pass
@@ -318,8 +335,9 @@ class Track(BaseModel):
for artist in artists:
try:
- c.execute("INSERT INTO artist_track (artist_id, track_id) "
- "VALUES(?, ?)", (artist.id, self.id))
+ c.execute(
+ "INSERT INTO artist_track (artist_id, track_id) "
+ "VALUES(?, ?)", (artist.id, self.id))
except sqlite3.IntegrityError:
pass
@@ -395,10 +413,12 @@ class Track(BaseModel):
for row in result:
tracks.append(
- Track(id=row["id"], db=database, tracknumber=row["tracknumber"],
- name=row["name"], grouping=row["grouping"],
- filename=row["filename"])
- )
+ Track(id=row["id"],
+ db=database,
+ tracknumber=row["tracknumber"],
+ name=row["name"],
+ grouping=row["grouping"],
+ filename=row["filename"]))
return tracks
@@ -406,11 +426,15 @@ class Track(BaseModel):
def find_by_path(cls, path, database):
track = None
- for row in database.execute("SELECT * FROM track WHERE filename = ? "
- "LIMIT 1", (path,)):
- track = Track(id=row["id"], db=database,
- tracknumber=row["tracknumber"], name=row["name"],
- grouping=row["grouping"], filename=row["filename"])
+ for row in database.execute(
+ "SELECT * FROM track WHERE filename = ? "
+ "LIMIT 1", (path, )):
+ track = Track(id=row["id"],
+ db=database,
+ tracknumber=row["tracknumber"],
+ name=row["name"],
+ grouping=row["grouping"],
+ filename=row["filename"])
return track
@@ -451,50 +475,52 @@ class Track(BaseModel):
rows = None
row = None
if musicbrainz_artistid:
- rows = c.execute("SELECT * FROM artist WHERE "
- "musicbrainz_artistid = ?",
- (musicbrainz_artistid,))
+ rows = c.execute(
+ "SELECT * FROM artist WHERE "
+ "musicbrainz_artistid = ?", (musicbrainz_artistid, ))
row = rows.fetchone()
if not row:
- rows = c.execute("SELECT * FROM artist WHERE name = ? "
- "AND musicbrainz_artistid IS NULL",
- (artist_name,))
+ rows = c.execute(
+ "SELECT * FROM artist WHERE name = ? "
+ "AND musicbrainz_artistid IS NULL", (artist_name, ))
row = rows.fetchone()
if not row:
rows = c.execute("SELECT * FROM artist WHERE name = ?",
- (artist_name,))
+ (artist_name, ))
row = rows.fetchone()
if row:
- artist = Artist(id=row["id"], db=database, name=row["name"],
- sortname=row["sortname"],
- musicbrainz_artistid=row[
- "musicbrainz_artistid"])
-
- if (musicbrainz_artistid and
- (not hasattr(artist, "musicbrainz_artistid") or
- not artist.musicbrainz_artistid)):
- c.execute("UPDATE artist SET musicbrainz_artistid = ? "
- "WHERE id = ?",
- (musicbrainz_artistid, artist.id))
+ artist = Artist(
+ id=row["id"],
+ db=database,
+ name=row["name"],
+ sortname=row["sortname"],
+ musicbrainz_artistid=row["musicbrainz_artistid"])
+
+ if (musicbrainz_artistid
+ and (not hasattr(artist, "musicbrainz_artistid")
+ or not artist.musicbrainz_artistid)):
+ c.execute(
+ "UPDATE artist SET musicbrainz_artistid = ? "
+ "WHERE id = ?", (musicbrainz_artistid, artist.id))
if (artistsort and
- (not hasattr(artist, "sortname") or
- not artist.sortname)):
+ (not hasattr(artist, "sortname") or not artist.sortname)):
c.execute("UPDATE artist SET sortname = ? WHERE id = ?",
(artistsort, artist.id))
else:
- c.execute("INSERT INTO artist (name, sortname, "
- "musicbrainz_artistid) VALUES(?, ?, ?)",
- (artist_name, artistsort, musicbrainz_artistid))
+ c.execute(
+ "INSERT INTO artist (name, sortname, "
+ "musicbrainz_artistid) VALUES(?, ?, ?)",
+ (artist_name, artistsort, musicbrainz_artistid))
- artist = Artist(
- id=c.lastrowid, db=database, name=artist_name,
- sortname=artistsort,
- musicbrainz_artistid=musicbrainz_artistid
- )
+ artist = Artist(id=c.lastrowid,
+ db=database,
+ name=artist_name,
+ sortname=artistsort,
+ musicbrainz_artistid=musicbrainz_artistid)
i += 1
@@ -520,46 +546,59 @@ class Track(BaseModel):
pass
if mb_albumid:
- rows = c.execute("SELECT * FROM album WHERE "
- "musicbrainz_albumid = ?", (mb_albumid,))
+ rows = c.execute(
+ "SELECT * FROM album WHERE "
+ "musicbrainz_albumid = ?", (mb_albumid, ))
row = rows.fetchone()
if row:
- album = Album(id=row["id"], db=database, name=row["name"],
- date=row["date"], musicbrainz_albumid=row[
- "musicbrainz_albumid"])
+ album = Album(id=row["id"],
+ db=database,
+ name=row["name"],
+ date=row["date"],
+ musicbrainz_albumid=row["musicbrainz_albumid"])
else:
- c.execute("INSERT INTO album (name, `date`, "
- "musicbrainz_albumid) VALUES (?, ?, ?)",
- (album_name, album_date, mb_albumid))
+ c.execute(
+ "INSERT INTO album (name, `date`, "
+ "musicbrainz_albumid) VALUES (?, ?, ?)",
+ (album_name, album_date, mb_albumid))
- album = Album(id=c.lastrowid, db=database, name=album_name,
- date=album_date, musicbrainz_albumid=mb_albumid)
+ album = Album(id=c.lastrowid,
+ db=database,
+ name=album_name,
+ date=album_date,
+ musicbrainz_albumid=mb_albumid)
elif album_name:
for artist in artists:
- rows = c.execute("SELECT album.* FROM album INNER JOIN "
- "album_artist ON album_artist.album_id = "
- "album.id WHERE album.name = ? AND "
- "artist_id = ?", (album_name, artist.id))
+ rows = c.execute(
+ "SELECT album.* FROM album INNER JOIN "
+ "album_artist ON album_artist.album_id = "
+ "album.id WHERE album.name = ? AND "
+ "artist_id = ?", (album_name, artist.id))
row = rows.fetchone()
if row:
- album = Album(id=row["id"], db=database, name=row["name"],
+ album = Album(id=row["id"],
+ db=database,
+ name=row["name"],
date=row["date"])
else:
c.execute("INSERT INTO album (name, `date`) VALUES(?, ?)",
(album_name, album_date))
- album = Album(id=c.lastrowid, db=database, name=album_name,
+ album = Album(id=c.lastrowid,
+ db=database,
+ name=album_name,
date=album_date)
for artist in artists:
if album:
try:
- c.execute("INSERT INTO album_artist (artist_id, album_id) "
- "VALUES(?, ?)", (artist.id, album.id))
+ c.execute(
+ "INSERT INTO album_artist (artist_id, album_id) "
+ "VALUES(?, ?)", (artist.id, album.id))
except sqlite3.IntegrityError:
pass
@@ -580,33 +619,42 @@ class Track(BaseModel):
pass
track = None
- rows = c.execute("SELECT * FROM track WHERE filename = ?", (filename,))
+ rows = c.execute("SELECT * FROM track WHERE filename = ?",
+ (filename, ))
row = rows.fetchone()
if row:
- track = Track(id=row["id"], db=database,
- tracknumber=row["tracknumber"], name=row["name"],
- grouping=row["grouping"], filename=row["filename"])
+ track = Track(id=row["id"],
+ db=database,
+ tracknumber=row["tracknumber"],
+ name=row["name"],
+ grouping=row["grouping"],
+ filename=row["filename"])
else:
- c.execute("INSERT INTO track (tracknumber, name, grouping, "
- "filename) VALUES(?, ?, ?, ?)",
- (track_number, track_name, track_grouping,
- filename))
-
- track = Track(id=c.lastrowid, db=database, tracknumber=track_number,
- name=track_name, grouping=track_grouping,
+ c.execute(
+ "INSERT INTO track (tracknumber, name, grouping, "
+ "filename) VALUES(?, ?, ?, ?)",
+ (track_number, track_name, track_grouping, filename))
+
+ track = Track(id=c.lastrowid,
+ db=database,
+ tracknumber=track_number,
+ name=track_name,
+ grouping=track_grouping,
filename=filename)
if album:
try:
- c.execute("INSERT INTO album_track (album_id, track_id) "
- "VALUES(?,?)", (album.id, track.id))
+ c.execute(
+ "INSERT INTO album_track (album_id, track_id) "
+ "VALUES(?,?)", (album.id, track.id))
except sqlite3.IntegrityError:
pass
for artist in artists:
try:
- c.execute("INSERT INTO artist_track (artist_id, track_id) "
- "VALUES(?, ?)", (artist.id, track.id))
+ c.execute(
+ "INSERT INTO artist_track (artist_id, track_id) "
+ "VALUES(?, ?)", (artist.id, track.id))
except sqlite3.IntegrityError:
pass
@@ -616,7 +664,11 @@ class Track(BaseModel):
return track
@classmethod
- def all(cls, database, order="track.id", direction="ASC", limit=None,
+ def all(cls,
+ database,
+ order="track.id",
+ direction="ASC",
+ limit=None,
offset=None):
tracks = []
@@ -628,15 +680,18 @@ class Track(BaseModel):
direction)
if limit and offset:
- select_string = " ".join((select_string,
- "LIMIT %s OFFSET %s" % (limit, offset)))
+ select_string = " ".join(
+ (select_string, "LIMIT %s OFFSET %s" % (limit, offset)))
result = database.execute(select_string)
for row in result:
- tracks.append(Track(id=row["id"], db=database,
- tracknumber=row["tracknumber"],
- name=row["name"], grouping=row["name"],
- filename=row["filename"]))
+ tracks.append(
+ Track(id=row["id"],
+ db=database,
+ tracknumber=row["tracknumber"],
+ name=row["name"],
+ grouping=row["name"],
+ filename=row["filename"]))
return tracks