mirror of
https://github.com/spl0k/supysonic.git
synced 2024-11-09 19:52:16 +00:00
Scanner: remove non exsiting entries
SQLAlchemy's sessions and relationships confuse me a bit
This commit is contained in:
parent
2cffe64946
commit
d34d54fd50
8
db.py
8
db.py
@ -66,8 +66,8 @@ class Artist(Base):
|
||||
__tablename__ = 'artist'
|
||||
|
||||
id = UUID.gen_id_column()
|
||||
name = Column(String)
|
||||
albums = relationship('Album', backref = 'artist', lazy = 'dynamic')
|
||||
name = Column(String, unique = True)
|
||||
albums = relationship('Album', backref = 'artist')
|
||||
|
||||
class Album(Base):
|
||||
__tablename__ = 'album'
|
||||
@ -75,7 +75,7 @@ class Album(Base):
|
||||
id = UUID.gen_id_column()
|
||||
name = Column(String)
|
||||
artist_id = Column(UUID, ForeignKey('artist.id'))
|
||||
tracks = relationship('Track', backref = 'album', lazy = 'dynamic')
|
||||
tracks = relationship('Track', backref = 'album')
|
||||
|
||||
class Track(Base):
|
||||
__tablename__ = 'track'
|
||||
@ -86,7 +86,7 @@ class Track(Base):
|
||||
title = Column(String)
|
||||
duration = Column(Time)
|
||||
album_id = Column(UUID, ForeignKey('album.id'))
|
||||
path = Column(String)
|
||||
path = Column(String, unique = True)
|
||||
|
||||
def init_db():
|
||||
Base.metadata.create_all(bind = engine)
|
||||
|
35
scanner.py
35
scanner.py
@ -15,6 +15,7 @@ def seconds_to_time(secs):
|
||||
class Scanner:
|
||||
def __init__(self, session):
|
||||
self.__session = session
|
||||
self.__artists = db.Artist.query.all()
|
||||
self.__added_artists = 0
|
||||
self.__added_albums = 0
|
||||
self.__added_tracks = 0
|
||||
@ -29,17 +30,32 @@ class Scanner:
|
||||
self.__scan_file(os.path.join(root, f))
|
||||
|
||||
def prune(self, folder):
|
||||
pass
|
||||
for artist in db.Artist.query.all():
|
||||
for album in artist.albums[:]:
|
||||
for track in album.tracks[:]:
|
||||
if not os.path.exists(track.path):
|
||||
album.tracks.remove(track)
|
||||
self.__session.delete(track)
|
||||
self.__deleted_tracks += 1
|
||||
if len(album.tracks) == 0:
|
||||
artist.albums.remove(album)
|
||||
self.__session.delete(album)
|
||||
self.__deleted_albums += 1
|
||||
if len(artist.albums) == 0:
|
||||
self.__session.delete(artist)
|
||||
self.__deleted_artists += 1
|
||||
|
||||
def __scan_file(self, path):
|
||||
tag = eyeD3.Tag()
|
||||
tag.link(path)
|
||||
|
||||
al = self.__find_album(tag.getArtist(), tag.getAlbum())
|
||||
tr = al.tracks.filter(db.Track.path == path).first()
|
||||
if tr is None:
|
||||
tr = filter(lambda t: t.path == path, al.tracks)
|
||||
if not tr:
|
||||
tr = db.Track(path = path)
|
||||
self.__added_tracks += 1
|
||||
else:
|
||||
tr = tr[0]
|
||||
|
||||
tr.disc = (tag.getDiscNum() or (1, 1))[0]
|
||||
tr.number = tag.getTrackNum()[0]
|
||||
@ -49,9 +65,9 @@ class Scanner:
|
||||
|
||||
def __find_album(self, artist, album):
|
||||
ar = self.__find_artist(artist)
|
||||
al = ar.albums.filter(db.Album.name == album).first()
|
||||
if not al is None:
|
||||
return al
|
||||
al = filter(lambda a: a.name == album, ar.albums)
|
||||
if al:
|
||||
return al[0]
|
||||
|
||||
al = db.Album(name = album, artist = ar)
|
||||
self.__added_albums += 1
|
||||
@ -59,11 +75,12 @@ class Scanner:
|
||||
return al
|
||||
|
||||
def __find_artist(self, artist):
|
||||
ar = self.__session.query(db.Artist).filter(db.Artist.name == artist).first()
|
||||
if not ar is None:
|
||||
return ar
|
||||
ar = filter(lambda a: a.name == artist, self.__artists)
|
||||
if ar:
|
||||
return ar[0]
|
||||
|
||||
ar = db.Artist(name = artist)
|
||||
self.__artists.append(ar)
|
||||
self.__session.add(ar)
|
||||
self.__added_artists += 1
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user