2012-10-13 12:53:09 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
2014-03-02 17:31:32 +00:00
|
|
|
# This file is part of Supysonic.
|
|
|
|
#
|
|
|
|
# Supysonic is a Python implementation of the Subsonic server API.
|
|
|
|
# Copyright (C) 2013, 2014 Alban 'spl0k' Féron
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
import os, os.path
|
2013-10-14 16:36:45 +00:00
|
|
|
import time, mimetypes
|
|
|
|
import mutagen
|
2014-03-10 20:52:51 +00:00
|
|
|
import config
|
|
|
|
from db import Folder, Artist, Album, Track
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
def get_mime(ext):
|
2013-10-15 16:16:16 +00:00
|
|
|
return mimetypes.guess_type('dummy.' + ext, False)[0] or config.get('mimetypes', ext) or 'application/octet-stream'
|
2013-10-15 11:14:20 +00:00
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
class Scanner:
|
2014-03-09 18:12:11 +00:00
|
|
|
def __init__(self, store):
|
|
|
|
self.__store = store
|
2012-10-21 14:18:35 +00:00
|
|
|
|
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
|
|
|
|
2013-11-02 18:52:02 +00:00
|
|
|
extensions = config.get('base', 'scanner_extensions')
|
|
|
|
self.__extensions = map(str.lower, extensions.split()) if extensions else None
|
|
|
|
|
2014-01-14 13:39:03 +00:00
|
|
|
def scan(self, folder, progress_callback = None):
|
|
|
|
files = [ os.path.join(root, f) for root, _, fs in os.walk(folder.path) for f in fs if self.__is_valid_path(os.path.join(root, f)) ]
|
|
|
|
total = len(files)
|
|
|
|
current = 0
|
|
|
|
|
|
|
|
for path in files:
|
|
|
|
self.__scan_file(path, folder)
|
|
|
|
current += 1
|
|
|
|
if progress_callback:
|
|
|
|
progress_callback(current, total)
|
|
|
|
|
2013-06-14 16:46:01 +00:00
|
|
|
folder.last_scan = int(time.time())
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2014-03-10 20:52:51 +00:00
|
|
|
self.__store.flush()
|
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
def prune(self, folder):
|
2014-03-10 20:52:51 +00:00
|
|
|
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.__store.remove(track)
|
|
|
|
self.__deleted_tracks += 1
|
2012-12-01 18:52:41 +00:00
|
|
|
|
2014-03-10 20:52:51 +00:00
|
|
|
# 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)
|
2012-12-01 18:52:41 +00:00
|
|
|
self.__deleted_albums += 1
|
|
|
|
|
2014-03-10 20:52:51 +00:00
|
|
|
# TODO execute the conditional part on SQL
|
|
|
|
for artist in [ a for a in self.__store.find(Artist) if a.albums.count() == 0 ]:
|
|
|
|
self.__store.remove(artist)
|
2012-12-01 18:52:41 +00:00
|
|
|
self.__deleted_artists += 1
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
self.__cleanup_folder(folder)
|
2014-03-10 20:52:51 +00:00
|
|
|
self.__store.flush()
|
2012-10-21 14:18:35 +00:00
|
|
|
|
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)
|
|
|
|
|
2013-11-02 18:52:02 +00:00
|
|
|
def __is_valid_path(self, path):
|
|
|
|
if not os.path.exists(path):
|
|
|
|
return False
|
|
|
|
if not self.__extensions:
|
|
|
|
return True
|
|
|
|
return os.path.splitext(path)[1][1:].lower() in self.__extensions
|
|
|
|
|
2012-10-14 11:07:02 +00:00
|
|
|
def __scan_file(self, path, folder):
|
2014-03-10 20:52:51 +00:00
|
|
|
tr = self.__store.find(Track, Track.path == path).one()
|
2013-10-14 16:36:45 +00:00
|
|
|
if tr:
|
2012-12-01 18:52:41 +00:00
|
|
|
if not os.path.getmtime(path) > tr.last_modification:
|
|
|
|
return
|
|
|
|
|
2013-10-14 16:36:45 +00:00
|
|
|
tag = self.__try_load_tag(path)
|
|
|
|
if not tag:
|
2014-03-10 20:52:51 +00:00
|
|
|
self.__store.remove(tr)
|
|
|
|
self.__deleted_tracks += 1
|
2013-10-14 16:36:45 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
tag = self.__try_load_tag(path)
|
|
|
|
if not tag:
|
|
|
|
return
|
|
|
|
|
2014-03-10 20:52:51 +00:00
|
|
|
tr = Track()
|
|
|
|
tr.path = path
|
|
|
|
tr.root_folder = folder
|
|
|
|
tr.folder = self.__find_folder(path, folder)
|
|
|
|
|
|
|
|
self.__store.add(tr)
|
2013-10-14 16:36:45 +00:00
|
|
|
self.__added_tracks += 1
|
|
|
|
|
|
|
|
tr.disc = self.__try_read_tag(tag, 'discnumber', 1, lambda x: int(x[0].split('/')[0]))
|
|
|
|
tr.number = self.__try_read_tag(tag, 'tracknumber', 1, lambda x: int(x[0].split('/')[0]))
|
2013-10-26 11:13:19 +00:00
|
|
|
tr.title = self.__try_read_tag(tag, 'title', '')
|
2013-10-14 16:36:45 +00:00
|
|
|
tr.year = self.__try_read_tag(tag, 'date', None, lambda x: int(x[0].split('-')[0]))
|
|
|
|
tr.genre = self.__try_read_tag(tag, 'genre')
|
|
|
|
tr.duration = int(tag.info.length)
|
2013-10-26 11:13:19 +00:00
|
|
|
tr.album = self.__find_album(self.__try_read_tag(tag, 'artist', ''), self.__try_read_tag(tag, 'album', ''))
|
2013-10-16 07:53:43 +00:00
|
|
|
tr.bitrate = (tag.info.bitrate if hasattr(tag.info, 'bitrate') else int(os.path.getsize(path) * 8 / tag.info.length)) / 1000
|
2013-10-15 16:16:16 +00:00
|
|
|
tr.content_type = get_mime(os.path.splitext(path)[1][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)
|
2014-03-10 20:52:51 +00:00
|
|
|
al = ar.albums.find(name = album).one()
|
2012-10-13 22:37:06 +00:00
|
|
|
if al:
|
2014-03-10 20:52:51 +00:00
|
|
|
return al
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2014-03-10 20:52:51 +00:00
|
|
|
al = Album()
|
|
|
|
al.name = album
|
|
|
|
al.artist = ar
|
|
|
|
|
|
|
|
self.__store.add(al)
|
2012-10-13 12:53:09 +00:00
|
|
|
self.__added_albums += 1
|
|
|
|
|
|
|
|
return al
|
|
|
|
|
|
|
|
def __find_artist(self, artist):
|
2014-03-10 20:52:51 +00:00
|
|
|
ar = self.__store.find(Artist, Artist.name == artist).one()
|
2012-10-13 22:37:06 +00:00
|
|
|
if ar:
|
2014-03-10 20:52:51 +00:00
|
|
|
return ar
|
|
|
|
|
|
|
|
ar = Artist()
|
|
|
|
ar.name = artist
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2014-03-10 20:52:51 +00:00
|
|
|
self.__store.add(ar)
|
2012-10-13 12:53:09 +00:00
|
|
|
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)
|
2014-03-10 20:52:51 +00:00
|
|
|
fold = self.__store.find(Folder, Folder.path == path).one()
|
2012-10-21 14:18:35 +00:00
|
|
|
if fold:
|
2014-03-10 20:52:51 +00:00
|
|
|
return fold
|
2012-10-21 14:18:35 +00:00
|
|
|
|
|
|
|
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)
|
2014-03-10 20:52:51 +00:00
|
|
|
fold = self.__store.find(Folder, Folder.path == full_path).one()
|
|
|
|
if not fold:
|
|
|
|
fold = Folder()
|
|
|
|
fold.root = False
|
|
|
|
fold.name = name
|
|
|
|
fold.path = full_path
|
|
|
|
fold.parent = folder
|
|
|
|
|
|
|
|
self.__store.add(fold)
|
|
|
|
|
|
|
|
folder = fold
|
2012-10-21 14:18:35 +00:00
|
|
|
|
|
|
|
return folder
|
|
|
|
|
2013-10-14 16:36:45 +00:00
|
|
|
def __try_load_tag(self, path):
|
|
|
|
try:
|
|
|
|
return mutagen.File(path, easy = True)
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __try_read_tag(self, metadata, field, default = None, transform = lambda x: x[0]):
|
|
|
|
try:
|
|
|
|
value = metadata[field]
|
|
|
|
if not value:
|
|
|
|
return default
|
|
|
|
if transform:
|
|
|
|
value = transform(value)
|
|
|
|
return value if value else default
|
|
|
|
except:
|
|
|
|
return default
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
def __cleanup_folder(self, folder):
|
|
|
|
for f in folder.children:
|
|
|
|
self.__cleanup_folder(f)
|
2014-03-10 20:52:51 +00:00
|
|
|
if folder.children.count() == 0 and folder.tracks.count() == 0 and not folder.root:
|
|
|
|
self.__store.remove(folder)
|
2012-10-21 14:18:35 +00:00
|
|
|
|
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)
|
|
|
|
|