platypush/plugins/media/search/local.py
def scan(self, media_dir, session=None, dir_record=None):
        """
        Scans a media directory and stores the search results in the internal
        SQLite index
        """

        if not session:
            session = self._get_db_session()

        self.logger.info('Indexing directory {}'.format(media_dir))
        index_start_time = time.time()

        if not dir_record:
            dir_record = self._get_or_create_dir_entry(session, media_dir)

        if not os.path.isdir(media_dir):
            self.logger.info('Directory {} is no longer accessible, removing it'.
                             format(media_dir))
            session.query(MediaDirectory) \
                .filter(MediaDirectory.path == media_dir) \
                .delete(synchronize_session='fetch')
            return

        stored_file_records = {
            f.path: f for f in self._get_file_records(dir_record, session)}

        for path, dirs, files in os.walk(media_dir):
            for filename in files:
                filepath = os.path.join(path, filename)

                if filepath in stored_file_records:
                    # stored_file_records will be used to keep track of the
                    # files that have been removed from media_dir. If the file
                    # is still there, don't index it again
                    del stored_file_records[filepath]
                    continue

                if not MediaPlugin.is_video_file(filename) and \
                        not MediaPlugin.is_audio_file(filename):
                    continue

                self.logger.debug('Syncing item {}'.format(filepath))
                tokens = [_.lower() for _ in re.split(self._filename_separators,
                                                      filename.strip())]

                token_records = self._sync_token_records(session, *tokens)
                file_record = MediaFile.build(directory_id=dir_record.id,
                                              path=filepath)

                session.add(file_record)
                session.commit()
                file_record = session.query(MediaFile).filter_by(
                    directory_id=dir_record.id, path=filepath).one()

                for token_record in token_records:
                    file_token = MediaFileToken.build(file_id=file_record.id,
                                                      token_id=token_record.id)
                    session.add(file_token)

        # stored_file_records should now only contain the records of the files
        # that have been removed from the directory
        if stored_file_records:
            self.logger.info(
                'Removing references to {} deleted media items from {}'.format(
                    len(stored_file_records), media_dir))

            session.query(MediaFile).filter(MediaFile.id.in_(
                [record.id for record in stored_file_records.values()]
            )).delete(synchronize_session='fetch')

        dir_record.last_indexed_at = datetime.datetime.now()
        self.logger.info('Scanned {} in {} seconds'.format(
            media_dir, int(time.time() - index_start_time)))

        session.commit()

        Scans a media directory and stores the search results in the internal
        SQLite index
        
Scans a media directory and stores the search results in the internal
SQLite index
Scans a media directory and stores the search results in the internal SQLite index
def scan(self, media_dir, session=None, dir_record=None):
        if not session:
            session = self._get_db_session()
        self.logger.info('Indexing directory {}'.format(media_dir))
        index_start_time = time.time()
        if not dir_record:
            dir_record = self._get_or_create_dir_entry(session, media_dir)
        if not os.path.isdir(media_dir):
            self.logger.info('Directory {} is no longer accessible, removing it'.
                             format(media_dir))
            session.query(MediaDirectory) \
                .filter(MediaDirectory.path == media_dir) \
                .delete(synchronize_session='fetch')
            return
        stored_file_records = {
            f.path: f for f in self._get_file_records(dir_record, session)}
        for path, dirs, files in os.walk(media_dir):
            for filename in files:
                filepath = os.path.join(path, filename)
                if filepath in stored_file_records:
                    del stored_file_records[filepath]
                    continue
                if not MediaPlugin.is_video_file(filename) and \
                        not MediaPlugin.is_audio_file(filename):
                    continue
                self.logger.debug('Syncing item {}'.format(filepath))
                tokens = [_.lower() for _ in re.split(self._filename_separators,
                                                      filename.strip())]
                token_records = self._sync_token_records(session, *tokens)
                file_record = MediaFile.build(directory_id=dir_record.id,
                                              path=filepath)
                session.add(file_record)
                session.commit()
                file_record = session.query(MediaFile).filter_by(
                    directory_id=dir_record.id, path=filepath).one()
                for token_record in token_records:
                    file_token = MediaFileToken.build(file_id=file_record.id,
                                                      token_id=token_record.id)
                    session.add(file_token)
        if stored_file_records:
            self.logger.info(
                'Removing references to {} deleted media items from {}'.format(
                    len(stored_file_records), media_dir))
            session.query(MediaFile).filter(MediaFile.id.in_(
                [record.id for record in stored_file_records.values()]
            )).delete(synchronize_session='fetch')
        dir_record.last_indexed_at = datetime.datetime.now()
        self.logger.info('Scanned {} in {} seconds'.format(
            media_dir, int(time.time() - index_start_time)))
        session.commit()
def scan ( self , media_dir , session = None , dir_record = None ) : if not session : session = self . _get_db_session ( ) self . logger . info ( 'Indexing directory {}' . format ( media_dir ) ) index_start_time = time . time ( ) if not dir_record : dir_record = self . _get_or_create_dir_entry ( session , media_dir ) if not os . path . isdir ( media_dir ) : self . logger . info ( 'Directory {} is no longer accessible, removing it' . format ( media_dir ) ) session . query ( MediaDirectory ) . filter ( MediaDirectory . path == media_dir ) . delete ( synchronize_session = 'fetch' ) return stored_file_records = { f . path : f for f in self . _get_file_records ( dir_record , session ) } for path , dirs , files in os . walk ( media_dir ) : for filename in files : filepath = os . path . join ( path , filename ) if filepath in stored_file_records : del stored_file_records [ filepath ] continue if not MediaPlugin . is_video_file ( filename ) and not MediaPlugin . is_audio_file ( filename ) : continue self . logger . debug ( 'Syncing item {}' . format ( filepath ) ) tokens = [ _ . lower ( ) for _ in re . split ( self . _filename_separators , filename . strip ( ) ) ] token_records = self . _sync_token_records ( session , * tokens ) file_record = MediaFile . build ( directory_id = dir_record . id , path = filepath ) session . add ( file_record ) session . commit ( ) file_record = session . query ( MediaFile ) . filter_by ( directory_id = dir_record . id , path = filepath ) . one ( ) for token_record in token_records : file_token = MediaFileToken . build ( file_id = file_record . id , token_id = token_record . id ) session . add ( file_token ) if stored_file_records : self . logger . info ( 'Removing references to {} deleted media items from {}' . format ( len ( stored_file_records ) , media_dir ) ) session . query ( MediaFile ) . filter ( MediaFile . id . in_ ( [ record . id for record in stored_file_records . values ( ) ] ) ) . delete ( synchronize_session = 'fetch' ) dir_record . last_indexed_at = datetime . datetime . now ( ) self . logger . info ( 'Scanned {} in {} seconds' . format ( media_dir , int ( time . time ( ) - index_start_time ) ) ) session . commit ( )
Scans a media directory and stores the search results in the internal
SQLite index
Scans a media directory and stores the search results in the internal SQLite index
"""
        Scans a media directory and stores the search results in the internal
        SQLite index
        """ # stored_file_records will be used to keep track of the # files that have been removed from media_dir. If the file # is still there, don't index it again # stored_file_records should now only contain the records of the files # that have been removed from the directory
self
media_dir
session
dir_record