예제 #1
0
파일: ajax.py 프로젝트: Kirembu/Laudio
def ajax_scan_progress(request):
    """
    Returns the number of scanned and total songs
    """
    prog = ScanProgressor()
    scanned, total = prog.getScannedTracks()
    ctx = {
        'scanned': scanned,
        'total': total
    }
    return render(request, 'ajax/scan_count.json', ctx)
예제 #2
0
 def __init__(self, musicDir=LaudioConfig(settings.LAUDIO_CFG).collectionPath):
     """ Instances some attributes and sets the music directory
     
     Keyword arguments:
     musicDir -- the directory where musiccollection lies; the string
                 has to end with a slash because we save the relative
                 path
                 if not given, the collectionpath from the settings is used
     """
     self.musicDir = musicDir
     self.scanned = 0
     self.added = 0
     self.modified = 0
     self.broken = []
     self.noRights = []
     self._debugger = LaudioDebugger()
     self.scanLog = ScanProgressor()
예제 #3
0
class MusicScanner(object):
    """
    Class for scanning the files in your collection
    
    Usage:

    scanner = MusicScanner(path) # path is optional
    scanner.scan()
    """

    def __init__(self, musicDir=LaudioConfig(settings.LAUDIO_CFG).collectionPath):
        """ Instances some attributes and sets the music directory
        
        Keyword arguments:
        musicDir -- the directory where musiccollection lies; the string
                    has to end with a slash because we save the relative
                    path
                    if not given, the collectionpath from the settings is used
        """
        self.musicDir = musicDir
        self.scanned = 0
        self.added = 0
        self.modified = 0
        self.broken = []
        self.noRights = []
        self._debugger = LaudioDebugger()
        self.scanLog = ScanProgressor()

    def scan(self):
        """ Scans a directory recursively for ogg files """
        # scan all files
        fileList = []
        for root, directories, files in os.walk(self.musicDir):
            for name in files:
                if name.lower().endswith('.ogg') or name.lower().endswith('.oga') \
                                                 or name.lower().endswith('mp3'):
                    fileList.append( os.path.join( root, name ) )

        # add a new scan entry
        num_files = len(fileList)
        self.scanLog.setTotal(num_files)
        self._debugger.log('Music Scanner', 'Begin scan of %i songs' % num_files)
        
        # now add the files to the db
        for name in fileList:
            # ogg vorbis
            if name.lower().endswith('.ogg') or name.lower().endswith('.oga'):
                try:
                    self._addSong( VorbisSong(name) )
                except mutagen.oggvorbis.OggVorbisHeaderError:
                    self.broken.append(name)
            # mp3
            if name.lower().endswith('.mp3'):
                self._addSong( MP3Song(name) )
        
        self._debugger.log('Music Scanner', 'Finished scan')
        
        # reset count after finish
        self.scanLog.reset()
        

    def _addSong(self, musicFile):
        """ Add a song to the database.

        Keyword arguments:
        musicFile -- The song object

        """
        self.scanLog.updateScannedTracks()
        try:
            musicFile.save()
            if musicFile.modified:
                self.modified += 1
            if musicFile.added:
                self.added += 1
        except IOError:
            self.noRights.append(musicFile.path)


    def rmNonExist(self):
        """Removes tracks from the database which are
        not on the drive any more"""
        self._debugger.log('Music Scanner', 'Removing non existent songs from database')
        songs = Song.objects.all()
        for song in songs:
            if not os.path.exists(song.path):
                song.delete()
                self._debugger.log('Music Scanner', 'Removed %s from db: file does \
                                    not exist any more' % song.path)


    def reset(self):
        """Removes all scanned entries from the db
        """
        # TODO: delete via raw sql
        self._debugger.log('Music Scanner', 'Resetting Database')
        # Song.objects.all().delete() causes database errors
        for item in Song.objects.all():
            item.delete()
        for item in Artist.objects.all():
            item.delete()
        for item in Genre.objects.all():
            item.delete()
        for item in Album.objects.all():
            item.delete()
        for item in Playlist.objects.all():
            item.delete()
        self._debugger.log('Music Scanner', 'Resetted Database')