mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-22 17:06:17 +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'
|
__tablename__ = 'artist'
|
||||||
|
|
||||||
id = UUID.gen_id_column()
|
id = UUID.gen_id_column()
|
||||||
name = Column(String)
|
name = Column(String, unique = True)
|
||||||
albums = relationship('Album', backref = 'artist', lazy = 'dynamic')
|
albums = relationship('Album', backref = 'artist')
|
||||||
|
|
||||||
class Album(Base):
|
class Album(Base):
|
||||||
__tablename__ = 'album'
|
__tablename__ = 'album'
|
||||||
@ -75,7 +75,7 @@ class Album(Base):
|
|||||||
id = UUID.gen_id_column()
|
id = UUID.gen_id_column()
|
||||||
name = Column(String)
|
name = Column(String)
|
||||||
artist_id = Column(UUID, ForeignKey('artist.id'))
|
artist_id = Column(UUID, ForeignKey('artist.id'))
|
||||||
tracks = relationship('Track', backref = 'album', lazy = 'dynamic')
|
tracks = relationship('Track', backref = 'album')
|
||||||
|
|
||||||
class Track(Base):
|
class Track(Base):
|
||||||
__tablename__ = 'track'
|
__tablename__ = 'track'
|
||||||
@ -86,7 +86,7 @@ class Track(Base):
|
|||||||
title = Column(String)
|
title = Column(String)
|
||||||
duration = Column(Time)
|
duration = Column(Time)
|
||||||
album_id = Column(UUID, ForeignKey('album.id'))
|
album_id = Column(UUID, ForeignKey('album.id'))
|
||||||
path = Column(String)
|
path = Column(String, unique = True)
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
Base.metadata.create_all(bind = engine)
|
Base.metadata.create_all(bind = engine)
|
||||||
|
35
scanner.py
35
scanner.py
@ -15,6 +15,7 @@ def seconds_to_time(secs):
|
|||||||
class Scanner:
|
class Scanner:
|
||||||
def __init__(self, session):
|
def __init__(self, session):
|
||||||
self.__session = session
|
self.__session = session
|
||||||
|
self.__artists = db.Artist.query.all()
|
||||||
self.__added_artists = 0
|
self.__added_artists = 0
|
||||||
self.__added_albums = 0
|
self.__added_albums = 0
|
||||||
self.__added_tracks = 0
|
self.__added_tracks = 0
|
||||||
@ -29,17 +30,32 @@ class Scanner:
|
|||||||
self.__scan_file(os.path.join(root, f))
|
self.__scan_file(os.path.join(root, f))
|
||||||
|
|
||||||
def prune(self, folder):
|
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):
|
def __scan_file(self, path):
|
||||||
tag = eyeD3.Tag()
|
tag = eyeD3.Tag()
|
||||||
tag.link(path)
|
tag.link(path)
|
||||||
|
|
||||||
al = self.__find_album(tag.getArtist(), tag.getAlbum())
|
al = self.__find_album(tag.getArtist(), tag.getAlbum())
|
||||||
tr = al.tracks.filter(db.Track.path == path).first()
|
tr = filter(lambda t: t.path == path, al.tracks)
|
||||||
if tr is None:
|
if not tr:
|
||||||
tr = db.Track(path = path)
|
tr = db.Track(path = path)
|
||||||
self.__added_tracks += 1
|
self.__added_tracks += 1
|
||||||
|
else:
|
||||||
|
tr = tr[0]
|
||||||
|
|
||||||
tr.disc = (tag.getDiscNum() or (1, 1))[0]
|
tr.disc = (tag.getDiscNum() or (1, 1))[0]
|
||||||
tr.number = tag.getTrackNum()[0]
|
tr.number = tag.getTrackNum()[0]
|
||||||
@ -49,9 +65,9 @@ class Scanner:
|
|||||||
|
|
||||||
def __find_album(self, artist, album):
|
def __find_album(self, artist, album):
|
||||||
ar = self.__find_artist(artist)
|
ar = self.__find_artist(artist)
|
||||||
al = ar.albums.filter(db.Album.name == album).first()
|
al = filter(lambda a: a.name == album, ar.albums)
|
||||||
if not al is None:
|
if al:
|
||||||
return al
|
return al[0]
|
||||||
|
|
||||||
al = db.Album(name = album, artist = ar)
|
al = db.Album(name = album, artist = ar)
|
||||||
self.__added_albums += 1
|
self.__added_albums += 1
|
||||||
@ -59,11 +75,12 @@ class Scanner:
|
|||||||
return al
|
return al
|
||||||
|
|
||||||
def __find_artist(self, artist):
|
def __find_artist(self, artist):
|
||||||
ar = self.__session.query(db.Artist).filter(db.Artist.name == artist).first()
|
ar = filter(lambda a: a.name == artist, self.__artists)
|
||||||
if not ar is None:
|
if ar:
|
||||||
return ar
|
return ar[0]
|
||||||
|
|
||||||
ar = db.Artist(name = artist)
|
ar = db.Artist(name = artist)
|
||||||
|
self.__artists.append(ar)
|
||||||
self.__session.add(ar)
|
self.__session.add(ar)
|
||||||
self.__added_artists += 1
|
self.__added_artists += 1
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user