2012-10-13 12:53:09 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
import os, os.path
|
2012-10-13 12:53:09 +00:00
|
|
|
import datetime
|
2013-06-07 17:18:24 +00:00
|
|
|
import eyed3.id3, eyed3.mp3
|
2012-10-13 12:53:09 +00:00
|
|
|
import db
|
|
|
|
|
|
|
|
class Scanner:
|
|
|
|
def __init__(self, session):
|
|
|
|
self.__session = session
|
2012-12-01 18:52:41 +00:00
|
|
|
self.__tracks = db.Track.query.all()
|
2012-10-13 22:37:06 +00:00
|
|
|
self.__artists = db.Artist.query.all()
|
2012-10-21 14:18:35 +00:00
|
|
|
self.__folders = db.Folder.query.all()
|
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
self.__added_artists = 0
|
2012-12-01 18:52:41 +00:00
|
|
|
self.__added_albums = 0
|
|
|
|
self.__added_tracks = 0
|
2012-10-13 12:53:09 +00:00
|
|
|
self.__deleted_artists = 0
|
2012-12-01 18:52:41 +00:00
|
|
|
self.__deleted_albums = 0
|
|
|
|
self.__deleted_tracks = 0
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
def scan(self, folder):
|
|
|
|
for root, subfolders, files in os.walk(folder.path):
|
|
|
|
for f in files:
|
|
|
|
if f.endswith('.mp3'):
|
2012-10-14 11:07:02 +00:00
|
|
|
self.__scan_file(os.path.join(root, f), folder)
|
|
|
|
folder.last_scan = datetime.datetime.now()
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
def prune(self, folder):
|
2012-12-01 18:52:41 +00:00
|
|
|
for track in [ t for t in self.__tracks if t.root_folder.id == folder.id and not os.path.exists(t.path) ]:
|
|
|
|
track.album.tracks.remove(track)
|
|
|
|
track.folder.tracks.remove(track)
|
|
|
|
self.__session.delete(track)
|
|
|
|
self.__deleted_tracks += 1
|
|
|
|
|
|
|
|
for album in [ album for artist in self.__artists for album in artist.albums if len(album.tracks) == 0 ]:
|
|
|
|
album.artist.albums.remove(album)
|
|
|
|
self.__session.delete(album)
|
|
|
|
self.__deleted_albums += 1
|
|
|
|
|
|
|
|
for artist in [ a for a in self.__artists if len(a.albums) == 0 ]:
|
|
|
|
self.__session.delete(artist)
|
|
|
|
self.__deleted_artists += 1
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
self.__cleanup_folder(folder)
|
|
|
|
|
2012-11-11 20:39:26 +00:00
|
|
|
def check_cover_art(self, folder):
|
|
|
|
folder.has_cover_art = os.path.isfile(os.path.join(folder.path, 'cover.jpg'))
|
|
|
|
for f in folder.children:
|
|
|
|
self.check_cover_art(f)
|
|
|
|
|
2012-10-14 11:07:02 +00:00
|
|
|
def __scan_file(self, path, folder):
|
2012-12-01 18:52:41 +00:00
|
|
|
tr = filter(lambda t: t.path == path, self.__tracks)
|
2012-10-13 22:37:06 +00:00
|
|
|
if not tr:
|
2012-10-21 14:18:35 +00:00
|
|
|
tr = db.Track(path = path, root_folder = folder, folder = self.__find_folder(path, folder))
|
2012-12-01 18:52:41 +00:00
|
|
|
self.__tracks.append(tr)
|
2012-10-13 12:53:09 +00:00
|
|
|
self.__added_tracks += 1
|
2012-10-13 22:37:06 +00:00
|
|
|
else:
|
|
|
|
tr = tr[0]
|
2012-12-01 18:52:41 +00:00
|
|
|
if not os.path.getmtime(path) > tr.last_modification:
|
|
|
|
return
|
|
|
|
|
2013-06-07 17:18:24 +00:00
|
|
|
tag = eyed3.id3.Tag()
|
|
|
|
tag.parse(path)
|
|
|
|
info = eyed3.mp3.Mp3AudioFile(path).info
|
|
|
|
|
|
|
|
tr.disc = tag.disc_num[0] or 1
|
|
|
|
tr.number = tag.track_num[0] or 1
|
|
|
|
tr.title = tag.title
|
2013-06-10 14:51:21 +00:00
|
|
|
tr.year = tag.best_release_date.year if tag.best_release_date else None
|
2013-06-07 17:18:24 +00:00
|
|
|
tr.genre = tag.genre.name if tag.genre else None
|
|
|
|
tr.duration = info.time_secs
|
|
|
|
tr.album = self.__find_album(tag.artist, tag.album)
|
|
|
|
tr.bitrate = info.bit_rate[1]
|
2012-12-01 18:52:41 +00:00
|
|
|
tr.last_modification = os.path.getmtime(path)
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
def __find_album(self, artist, album):
|
|
|
|
ar = self.__find_artist(artist)
|
2012-10-13 22:37:06 +00:00
|
|
|
al = filter(lambda a: a.name == album, ar.albums)
|
|
|
|
if al:
|
|
|
|
return al[0]
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
al = db.Album(name = album, artist = ar)
|
|
|
|
self.__added_albums += 1
|
|
|
|
|
|
|
|
return al
|
|
|
|
|
|
|
|
def __find_artist(self, artist):
|
2012-10-13 22:37:06 +00:00
|
|
|
ar = filter(lambda a: a.name == artist, self.__artists)
|
|
|
|
if ar:
|
|
|
|
return ar[0]
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
ar = db.Artist(name = artist)
|
2012-10-13 22:37:06 +00:00
|
|
|
self.__artists.append(ar)
|
2012-10-13 12:53:09 +00:00
|
|
|
self.__session.add(ar)
|
|
|
|
self.__added_artists += 1
|
|
|
|
|
|
|
|
return ar
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
def __find_folder(self, path, folder):
|
|
|
|
path = os.path.dirname(path)
|
|
|
|
fold = filter(lambda f: f.path == path, self.__folders)
|
|
|
|
if fold:
|
|
|
|
return fold[0]
|
|
|
|
|
|
|
|
full_path = folder.path
|
|
|
|
path = path[len(folder.path) + 1:]
|
|
|
|
|
|
|
|
for name in path.split(os.sep):
|
|
|
|
full_path = os.path.join(full_path, name)
|
|
|
|
fold = filter(lambda f: f.path == full_path, self.__folders)
|
|
|
|
if fold:
|
|
|
|
folder = fold[0]
|
|
|
|
else:
|
|
|
|
folder = db.Folder(root = False, name = name, path = full_path, parent = folder)
|
|
|
|
self.__folders.append(folder)
|
|
|
|
|
|
|
|
return folder
|
|
|
|
|
|
|
|
def __cleanup_folder(self, folder):
|
|
|
|
for f in folder.children:
|
|
|
|
self.__cleanup_folder(f)
|
|
|
|
if len(folder.children) == 0 and len(folder.tracks) == 0 and not folder.root:
|
|
|
|
folder.parent = None
|
|
|
|
self.__session.delete(folder)
|
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
def stats(self):
|
|
|
|
return (self.__added_artists, self.__added_albums, self.__added_tracks), (self.__deleted_artists, self.__deleted_albums, self.__deleted_tracks)
|
|
|
|
|