mirror of
https://github.com/spl0k/supysonic.git
synced 2024-11-10 04:02:17 +00:00
Adapting Scanner
Not fully tested
This commit is contained in:
parent
74246a7659
commit
17e580e7b5
95
scanner.py
95
scanner.py
@ -21,7 +21,8 @@
|
|||||||
import os, os.path
|
import os, os.path
|
||||||
import time, mimetypes
|
import time, mimetypes
|
||||||
import mutagen
|
import mutagen
|
||||||
import config, db
|
import config
|
||||||
|
from db import Folder, Artist, Album, Track
|
||||||
|
|
||||||
def get_mime(ext):
|
def get_mime(ext):
|
||||||
return mimetypes.guess_type('dummy.' + ext, False)[0] or config.get('mimetypes', ext) or 'application/octet-stream'
|
return mimetypes.guess_type('dummy.' + ext, False)[0] or config.get('mimetypes', ext) or 'application/octet-stream'
|
||||||
@ -53,20 +54,25 @@ class Scanner:
|
|||||||
|
|
||||||
folder.last_scan = int(time.time())
|
folder.last_scan = int(time.time())
|
||||||
|
|
||||||
def prune(self, folder):
|
self.__store.flush()
|
||||||
for track in [ t for t in self.__tracks if t.root_folder.id == folder.id and not self.__is_valid_path(t.path) ]:
|
|
||||||
self.__remove_track(track)
|
|
||||||
|
|
||||||
for album in [ album for artist in self.__artists for album in artist.albums if len(album.tracks) == 0 ]:
|
def prune(self, folder):
|
||||||
album.artist.albums.remove(album)
|
for track in [ t for t in self.__store.find(Track, Track.root_folder_id == folder.id) if not self.__is_valid_path(t.path) ]:
|
||||||
self.__session.delete(album)
|
self.__store.remove(track)
|
||||||
|
self.__deleted_tracks += 1
|
||||||
|
|
||||||
|
# TODO execute the conditional part on SQL
|
||||||
|
for album in [ a for a in self.__store.find(Album) if a.tracks.count() == 0 ]:
|
||||||
|
self.__store.remove(album)
|
||||||
self.__deleted_albums += 1
|
self.__deleted_albums += 1
|
||||||
|
|
||||||
for artist in [ a for a in self.__artists if len(a.albums) == 0 ]:
|
# TODO execute the conditional part on SQL
|
||||||
self.__session.delete(artist)
|
for artist in [ a for a in self.__store.find(Artist) if a.albums.count() == 0 ]:
|
||||||
|
self.__store.remove(artist)
|
||||||
self.__deleted_artists += 1
|
self.__deleted_artists += 1
|
||||||
|
|
||||||
self.__cleanup_folder(folder)
|
self.__cleanup_folder(folder)
|
||||||
|
self.__store.flush()
|
||||||
|
|
||||||
def check_cover_art(self, folder):
|
def check_cover_art(self, folder):
|
||||||
folder.has_cover_art = os.path.isfile(os.path.join(folder.path, 'cover.jpg'))
|
folder.has_cover_art = os.path.isfile(os.path.join(folder.path, 'cover.jpg'))
|
||||||
@ -81,23 +87,27 @@ class Scanner:
|
|||||||
return os.path.splitext(path)[1][1:].lower() in self.__extensions
|
return os.path.splitext(path)[1][1:].lower() in self.__extensions
|
||||||
|
|
||||||
def __scan_file(self, path, folder):
|
def __scan_file(self, path, folder):
|
||||||
tr = filter(lambda t: t.path == path, self.__tracks)
|
tr = self.__store.find(Track, Track.path == path).one()
|
||||||
if tr:
|
if tr:
|
||||||
tr = tr[0]
|
|
||||||
if not os.path.getmtime(path) > tr.last_modification:
|
if not os.path.getmtime(path) > tr.last_modification:
|
||||||
return
|
return
|
||||||
|
|
||||||
tag = self.__try_load_tag(path)
|
tag = self.__try_load_tag(path)
|
||||||
if not tag:
|
if not tag:
|
||||||
self.__remove_track(tr)
|
self.__store.remove(tr)
|
||||||
|
self.__deleted_tracks += 1
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
tag = self.__try_load_tag(path)
|
tag = self.__try_load_tag(path)
|
||||||
if not tag:
|
if not tag:
|
||||||
return
|
return
|
||||||
|
|
||||||
tr = db.Track(path = path, root_folder = folder, folder = self.__find_folder(path, folder))
|
tr = Track()
|
||||||
self.__tracks.append(tr)
|
tr.path = path
|
||||||
|
tr.root_folder = folder
|
||||||
|
tr.folder = self.__find_folder(path, folder)
|
||||||
|
|
||||||
|
self.__store.add(tr)
|
||||||
self.__added_tracks += 1
|
self.__added_tracks += 1
|
||||||
|
|
||||||
tr.disc = self.__try_read_tag(tag, 'discnumber', 1, lambda x: int(x[0].split('/')[0]))
|
tr.disc = self.__try_read_tag(tag, 'discnumber', 1, lambda x: int(x[0].split('/')[0]))
|
||||||
@ -113,44 +123,54 @@ 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 = filter(lambda a: a.name == album, ar.albums)
|
al = ar.albums.find(name = album).one()
|
||||||
if al:
|
if al:
|
||||||
return al[0]
|
return al
|
||||||
|
|
||||||
al = db.Album(name = album, artist = ar)
|
al = Album()
|
||||||
|
al.name = album
|
||||||
|
al.artist = ar
|
||||||
|
|
||||||
|
self.__store.add(al)
|
||||||
self.__added_albums += 1
|
self.__added_albums += 1
|
||||||
|
|
||||||
return al
|
return al
|
||||||
|
|
||||||
def __find_artist(self, artist):
|
def __find_artist(self, artist):
|
||||||
ar = filter(lambda a: a.name.lower() == artist.lower(), self.__artists)
|
ar = self.__store.find(Artist, Artist.name == artist).one()
|
||||||
if ar:
|
if ar:
|
||||||
return ar[0]
|
return ar
|
||||||
|
|
||||||
ar = db.Artist(name = artist)
|
ar = Artist()
|
||||||
self.__artists.append(ar)
|
ar.name = artist
|
||||||
self.__session.add(ar)
|
|
||||||
|
self.__store.add(ar)
|
||||||
self.__added_artists += 1
|
self.__added_artists += 1
|
||||||
|
|
||||||
return ar
|
return ar
|
||||||
|
|
||||||
def __find_folder(self, path, folder):
|
def __find_folder(self, path, folder):
|
||||||
path = os.path.dirname(path)
|
path = os.path.dirname(path)
|
||||||
fold = filter(lambda f: f.path == path, self.__folders)
|
fold = self.__store.find(Folder, Folder.path == path).one()
|
||||||
if fold:
|
if fold:
|
||||||
return fold[0]
|
return fold
|
||||||
|
|
||||||
full_path = folder.path
|
full_path = folder.path
|
||||||
path = path[len(folder.path) + 1:]
|
path = path[len(folder.path) + 1:]
|
||||||
|
|
||||||
for name in path.split(os.sep):
|
for name in path.split(os.sep):
|
||||||
full_path = os.path.join(full_path, name)
|
full_path = os.path.join(full_path, name)
|
||||||
fold = filter(lambda f: f.path == full_path, self.__folders)
|
fold = self.__store.find(Folder, Folder.path == full_path).one()
|
||||||
if fold:
|
if not fold:
|
||||||
folder = fold[0]
|
fold = Folder()
|
||||||
else:
|
fold.root = False
|
||||||
folder = db.Folder(root = False, name = name, path = full_path, parent = folder)
|
fold.name = name
|
||||||
self.__folders.append(folder)
|
fold.path = full_path
|
||||||
|
fold.parent = folder
|
||||||
|
|
||||||
|
self.__store.add(fold)
|
||||||
|
|
||||||
|
folder = fold
|
||||||
|
|
||||||
return folder
|
return folder
|
||||||
|
|
||||||
@ -171,22 +191,11 @@ class Scanner:
|
|||||||
except:
|
except:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def __remove_track(self, track):
|
|
||||||
track.album.tracks.remove(track)
|
|
||||||
track.folder.tracks.remove(track)
|
|
||||||
# As we don't have a track -> playlists relationship, SQLAlchemy doesn't know it has to remove tracks
|
|
||||||
# from playlists as well, so let's help it
|
|
||||||
for playlist in db.Playlist.query.filter(db.Playlist.tracks.contains(track)):
|
|
||||||
playlist.tracks.remove(track)
|
|
||||||
self.__session.delete(track)
|
|
||||||
self.__deleted_tracks += 1
|
|
||||||
|
|
||||||
def __cleanup_folder(self, folder):
|
def __cleanup_folder(self, folder):
|
||||||
for f in folder.children:
|
for f in folder.children:
|
||||||
self.__cleanup_folder(f)
|
self.__cleanup_folder(f)
|
||||||
if len(folder.children) == 0 and len(folder.tracks) == 0 and not folder.root:
|
if folder.children.count() == 0 and folder.tracks.count() == 0 and not folder.root:
|
||||||
folder.parent = None
|
self.__store.remove(folder)
|
||||||
self.__session.delete(folder)
|
|
||||||
|
|
||||||
def stats(self):
|
def stats(self):
|
||||||
return (self.__added_artists, self.__added_albums, self.__added_tracks), (self.__deleted_artists, self.__deleted_albums, self.__deleted_tracks)
|
return (self.__added_artists, self.__added_albums, self.__added_tracks), (self.__deleted_artists, self.__deleted_albums, self.__deleted_tracks)
|
||||||
|
Loading…
Reference in New Issue
Block a user