2012-10-13 12:53:09 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
import datetime
|
|
|
|
import eyeD3
|
|
|
|
|
|
|
|
import db
|
|
|
|
|
|
|
|
class Scanner:
|
|
|
|
def __init__(self, session):
|
|
|
|
self.__session = session
|
2012-10-13 22:37:06 +00:00
|
|
|
self.__artists = db.Artist.query.all()
|
2012-10-13 12:53:09 +00:00
|
|
|
self.__added_artists = 0
|
|
|
|
self.__added_albums = 0
|
|
|
|
self.__added_tracks = 0
|
|
|
|
self.__deleted_artists = 0
|
|
|
|
self.__deleted_albums = 0
|
|
|
|
self.__deleted_tracks = 0
|
|
|
|
|
|
|
|
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-10-13 22:37:06 +00:00
|
|
|
for artist in db.Artist.query.all():
|
|
|
|
for album in artist.albums[:]:
|
2012-10-14 11:07:02 +00:00
|
|
|
for track in filter(lambda t: t.folder.id == folder.id, album.tracks):
|
2012-10-13 22:37:06 +00:00
|
|
|
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
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2012-10-14 11:07:02 +00:00
|
|
|
def __scan_file(self, path, folder):
|
2012-10-13 12:53:09 +00:00
|
|
|
tag = eyeD3.Tag()
|
|
|
|
tag.link(path)
|
2012-10-20 16:05:37 +00:00
|
|
|
audio_file = eyeD3.Mp3AudioFile(path)
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
al = self.__find_album(tag.getArtist(), tag.getAlbum())
|
2012-10-13 22:37:06 +00:00
|
|
|
tr = filter(lambda t: t.path == path, al.tracks)
|
|
|
|
if not tr:
|
2012-10-14 11:07:02 +00:00
|
|
|
tr = db.Track(path = path, folder = folder)
|
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-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
tr.disc = (tag.getDiscNum() or (1, 1))[0]
|
|
|
|
tr.number = tag.getTrackNum()[0]
|
|
|
|
tr.title = tag.getTitle()
|
2012-10-20 16:05:37 +00:00
|
|
|
tr.year = tag.getYear()
|
|
|
|
tr.genre = tag.getGenre().name if tag.getGenre() else None
|
|
|
|
tr.duration = audio_file.getPlayTime()
|
2012-10-13 12:53:09 +00:00
|
|
|
tr.album = al
|
2012-10-20 16:05:37 +00:00
|
|
|
tr.bitrate = audio_file.getBitRate()[1]
|
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
|
|
|
|
|
|
|
|
def stats(self):
|
|
|
|
return (self.__added_artists, self.__added_albums, self.__added_tracks), (self.__deleted_artists, self.__deleted_albums, self.__deleted_tracks)
|
|
|
|
|