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-11-26 07:59:08 +00:00
|
|
|
import time
|
2014-02-04 08:36:59 +00:00
|
|
|
import datetime
|
|
|
|
from mediafile import MediaFile
|
2013-12-29 19:30:12 +00:00
|
|
|
import config
|
2013-11-04 01:43:16 +00:00
|
|
|
import math
|
2013-11-26 07:59:08 +00:00
|
|
|
import sys, traceback
|
2013-11-04 01:43:16 +00:00
|
|
|
from web import app
|
2013-12-29 19:30:12 +00:00
|
|
|
import db
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
class Scanner:
|
|
|
|
def __init__(self, session):
|
|
|
|
self.__session = session
|
2013-11-26 07:59:08 +00:00
|
|
|
|
2012-12-01 18:52:41 +00:00
|
|
|
self.__tracks = db.Track.query.all()
|
2013-11-04 01:43:16 +00:00
|
|
|
self.__tracks = {x.path: x for x in self.__tracks}
|
2013-12-29 19:30:12 +00:00
|
|
|
self.__tracktimes = {x.path: x.last_modification for x in self.__tracks.values()}
|
2013-11-03 20:46:19 +00:00
|
|
|
|
2012-10-13 22:37:06 +00:00
|
|
|
self.__artists = db.Artist.query.all()
|
2013-11-26 07:59:08 +00:00
|
|
|
self.__artists = {x.name.lower(): x for x in self.__artists}
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
self.__folders = db.Folder.query.all()
|
2013-11-26 07:59:08 +00:00
|
|
|
self.__folders = {x.path: x for x in self.__folders}
|
|
|
|
|
|
|
|
self.__playlists = db.Playlist.query.all()
|
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
|
|
|
|
|
2013-12-29 19:30:12 +00:00
|
|
|
def scan(self, root_folder):
|
|
|
|
print "scanning", root_folder.path
|
2013-11-03 20:46:19 +00:00
|
|
|
valid = [x.lower() for x in config.get('base','filetypes').split(',')]
|
2013-11-26 07:59:08 +00:00
|
|
|
valid = tuple(valid)
|
2013-11-03 20:46:19 +00:00
|
|
|
print "valid filetypes: ",valid
|
|
|
|
|
2013-12-29 19:30:12 +00:00
|
|
|
for root, subfolders, files in os.walk(root_folder.path, topdown=False):
|
|
|
|
if(root not in self.__folders):
|
2014-02-04 08:36:59 +00:00
|
|
|
app.logger.debug('Adding folder: ' + root)
|
2013-12-29 19:30:12 +00:00
|
|
|
self.__folders[root] = db.Folder(path = root)
|
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
for f in files:
|
2013-11-26 07:59:08 +00:00
|
|
|
if f.lower().endswith(valid):
|
2013-11-04 01:43:16 +00:00
|
|
|
try:
|
2013-12-29 19:30:12 +00:00
|
|
|
path = os.path.join(root, f)
|
|
|
|
self.__scan_file(path, root)
|
2013-11-04 01:43:16 +00:00
|
|
|
except:
|
|
|
|
app.logger.error('Problem adding file: ' + os.path.join(root,f))
|
2013-11-26 07:59:08 +00:00
|
|
|
app.logger.error(traceback.print_exc())
|
2014-02-04 08:36:59 +00:00
|
|
|
pass
|
2013-11-03 20:46:19 +00:00
|
|
|
|
2013-11-26 07:59:08 +00:00
|
|
|
self.__session.add_all(self.__tracks.values())
|
2013-12-29 19:30:12 +00:00
|
|
|
root_folder.last_scan = int(time.time())
|
2013-11-04 19:00:15 +00:00
|
|
|
self.__session.commit()
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
def prune(self, folder):
|
2013-12-29 19:30:12 +00:00
|
|
|
# check for invalid paths still in database
|
|
|
|
#app.logger.debug('Checking for invalid paths...')
|
|
|
|
#for path in self.__tracks.keys():
|
|
|
|
#if not os.path.exists(path.encode('utf-8')):
|
|
|
|
#app.logger.debug('Removed invalid path: ' + path)
|
|
|
|
#self.__remove_track(self.__tracks[path])
|
|
|
|
|
|
|
|
app.logger.debug('Checking for empty albums...')
|
|
|
|
for album in db.Album.query.filter(~db.Album.id.in_(self.__session.query(db.Track.album_id).distinct())):
|
|
|
|
app.logger.debug(album.name + ' Removed')
|
2012-12-01 18:52:41 +00:00
|
|
|
album.artist.albums.remove(album)
|
|
|
|
self.__session.delete(album)
|
|
|
|
self.__deleted_albums += 1
|
|
|
|
|
2013-12-29 19:30:12 +00:00
|
|
|
app.logger.debug('Checking for artists with no albums...')
|
2013-11-26 07:59:08 +00:00
|
|
|
for artist in [ a for a in self.__artists.values() if len(a.albums) == 0 ]:
|
2012-12-01 18:52:41 +00:00
|
|
|
self.__session.delete(artist)
|
|
|
|
self.__deleted_artists += 1
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2013-11-26 07:59:08 +00:00
|
|
|
self.__session.commit()
|
2012-11-11 20:39:26 +00:00
|
|
|
|
2013-12-29 19:30:12 +00:00
|
|
|
app.logger.debug('Cleaning up folder...')
|
2013-11-26 07:59:08 +00:00
|
|
|
self.__cleanup_folder(folder)
|
2013-11-02 18:52:02 +00:00
|
|
|
|
2013-12-29 19:30:12 +00:00
|
|
|
def __scan_file(self, path, root):
|
2013-11-04 19:00:15 +00:00
|
|
|
curmtime = int(math.floor(os.path.getmtime(path)))
|
|
|
|
|
2013-11-03 20:46:19 +00:00
|
|
|
if path in self.__tracks:
|
|
|
|
tr = self.__tracks[path]
|
2013-11-04 19:00:15 +00:00
|
|
|
|
2013-11-26 07:59:08 +00:00
|
|
|
app.logger.debug('Existing File: ' + path)
|
2014-02-04 08:36:59 +00:00
|
|
|
|
2014-03-12 16:55:27 +00:00
|
|
|
if curmtime <= self.__tracktimes[path]:
|
|
|
|
app.logger.debug('\tFile not modified')
|
|
|
|
return False
|
2012-12-01 18:52:41 +00:00
|
|
|
|
2013-11-04 01:43:16 +00:00
|
|
|
app.logger.debug('\tFile modified, updating tag')
|
|
|
|
app.logger.debug('\tcurmtime %s / last_mod %s', curmtime, tr.last_modification)
|
|
|
|
app.logger.debug('\t\t%s Seconds Newer\n\t\t', str(curmtime - tr.last_modification))
|
2014-03-12 16:55:27 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
mf = MediaFile(path)
|
|
|
|
except:
|
|
|
|
app.logger.error('Problem reading file: ' + path)
|
|
|
|
app.logger.error(traceback.print_exc())
|
|
|
|
return False
|
|
|
|
|
2013-10-14 16:36:45 +00:00
|
|
|
else:
|
2013-11-26 07:59:08 +00:00
|
|
|
app.logger.debug('Scanning File: ' + path + '\n\tReading tag')
|
2014-02-04 08:36:59 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
mf = MediaFile(path)
|
|
|
|
except:
|
|
|
|
app.logger.error('Problem reading file: ' + path)
|
|
|
|
app.logger.error(traceback.print_exc())
|
2013-11-03 20:46:19 +00:00
|
|
|
return False
|
2013-10-14 16:36:45 +00:00
|
|
|
|
2013-12-29 19:30:12 +00:00
|
|
|
tr = db.Track(path = path, folder = self.__find_folder(root))
|
2013-11-26 07:59:08 +00:00
|
|
|
|
2013-11-03 20:46:19 +00:00
|
|
|
self.__tracks[path] = tr
|
2013-10-14 16:36:45 +00:00
|
|
|
self.__added_tracks += 1
|
|
|
|
|
2013-11-04 19:00:15 +00:00
|
|
|
tr.last_modification = curmtime
|
2013-11-26 07:59:08 +00:00
|
|
|
|
2014-02-04 08:36:59 +00:00
|
|
|
# read in file tags
|
|
|
|
tr.disc = getattr(mf, 'disc')
|
|
|
|
tr.number = getattr(mf, 'track')
|
|
|
|
tr.title = getattr(mf, 'title')
|
|
|
|
tr.year = getattr(mf, 'year')
|
|
|
|
tr.genre = getattr(mf, 'genre')
|
|
|
|
tr.artist = getattr(mf, 'artist')
|
|
|
|
tr.bitrate = getattr(mf, 'bitrate')/1000
|
|
|
|
tr.duration = getattr(mf, 'length')
|
2013-11-26 07:59:08 +00:00
|
|
|
|
2014-02-04 08:36:59 +00:00
|
|
|
albumartist = getattr(mf, 'albumartist')
|
|
|
|
if (albumartist == u''):
|
|
|
|
# Use folder name two levels up if no albumartist tag found
|
|
|
|
# Assumes structure main -> artist -> album -> song.file
|
|
|
|
# That way the songs in compilations will show up in the same album
|
|
|
|
albumartist = os.path.basename(os.path.dirname(tr.folder.path))
|
|
|
|
|
|
|
|
tr.created = datetime.datetime.fromtimestamp(curmtime)
|
|
|
|
|
|
|
|
# album year is the same as year of first track found from album, might be inaccurate
|
|
|
|
tr.album = self.__find_album(albumartist, getattr(mf, 'album'), tr.year)
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2013-11-03 20:46:19 +00:00
|
|
|
return True
|
|
|
|
|
2014-02-04 08:36:59 +00:00
|
|
|
def __find_album(self, artist, album, yr):
|
2013-11-26 07:59:08 +00:00
|
|
|
# TODO : DB specific issues with single column name primary key
|
|
|
|
# for instance, case sensitivity and trailing spaces
|
|
|
|
artist = artist.rstrip()
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2013-11-26 07:59:08 +00:00
|
|
|
if artist in self.__artists:
|
|
|
|
ar = self.__artists[artist]
|
|
|
|
else:
|
|
|
|
#Flair!
|
|
|
|
sys.stdout.write('\033[K')
|
2013-12-29 19:30:12 +00:00
|
|
|
sys.stdout.write('%s\r' % artist.encode('utf-8'))
|
2013-11-26 07:59:08 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
ar = db.Artist(name = artist)
|
|
|
|
self.__artists[artist] = ar
|
|
|
|
self.__added_artists += 1
|
|
|
|
|
|
|
|
al = {a.name: a for a in ar.albums}
|
|
|
|
if album in al:
|
|
|
|
return al[album]
|
|
|
|
else:
|
|
|
|
self.__added_albums += 1
|
2014-02-04 08:36:59 +00:00
|
|
|
return db.Album(name = album, artist = ar, year = yr)
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2013-12-29 19:30:12 +00:00
|
|
|
def __find_folder(self, path):
|
2013-11-26 07:59:08 +00:00
|
|
|
|
|
|
|
if path in self.__folders:
|
|
|
|
return self.__folders[path]
|
2012-10-21 14:18:35 +00:00
|
|
|
|
2013-12-29 19:30:12 +00:00
|
|
|
app.logger.debug('Adding folder: ' + path)
|
|
|
|
self.__folders[path] = db.Folder(path = path)
|
|
|
|
return self.__folders[path]
|
2012-10-21 14:18:35 +00:00
|
|
|
|
2013-10-14 16:36:45 +00:00
|
|
|
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
|
2013-11-26 07:59:08 +00:00
|
|
|
for playlist in self.__playlists:
|
|
|
|
if track in playlist.tracks:
|
|
|
|
playlist.tracks.remove(track)
|
|
|
|
|
2013-10-14 16:36:45 +00:00
|
|
|
self.__session.delete(track)
|
|
|
|
self.__deleted_tracks += 1
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
def __cleanup_folder(self, folder):
|
2013-12-29 19:30:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Get all subfolders of folder
|
|
|
|
all_descendants = self.__session.query(db.Folder).filter(db.Folder.path.like(folder.path + os.sep + '%'))
|
|
|
|
|
|
|
|
app.logger.debug('Checking for empty paths')
|
|
|
|
|
|
|
|
# Delete folder if there is no track in a subfolder
|
|
|
|
for d in all_descendants:
|
|
|
|
if any(d.path in k for k in self.__tracks.keys()):
|
|
|
|
continue;
|
|
|
|
else:
|
|
|
|
app.logger.debug('Deleting path with no tracks: ' + d.path)
|
|
|
|
self.__session.delete(d)
|
|
|
|
|
|
|
|
self.__session.commit()
|
|
|
|
return
|
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)
|
|
|
|
|