def main(): arguments = docopt(__doc__) kwargs = { 'use_lastfm': arguments['--lastfm'], 'no_metadata': arguments['--nometadata'], 'reindex': arguments['--reindex'], 'verbose': arguments['--verbose'], 'quiet': arguments['--quiet'], } if kwargs['no_metadata']: kwargs['use_lastfm'] = False if kwargs['use_lastfm'] and not app.config.get('LASTFM_API_KEY'): sys.stderr.write('ERROR: You need a Last.FM API key if you set the ' '--lastfm flag.\n') sys.exit(1) # Generate database db.create_all() lola = Indexer(app.config, **kwargs) lola.run() # Petit performance hack: Every track will be added to the session but they # will be written down to disk only once, at the end. if lola.verbose: print('Writing to database...') lola.session.commit() if lola.verbose: print('Checking for duplicated tracks...') lola.make_slugs_unique()
def __init__(self, config=None, use_lastfm=False, no_metadata=False, low_ram=False, reindex=False): self.config = config self.use_lastfm = use_lastfm self.no_metadata = no_metadata self.low_ram = low_ram self.reindex = reindex self.empty_db = reindex and not low_ram self.session = db.session self.media_dirs = config.get('MEDIA_DIRS', []) self.allowed_extensions = app.config.get('ALLOWED_FILE_EXTENSIONS', self.VALID_FILE_EXTENSIONS) self._ext = None self._meta = None self.track_count = 0 self.skipped_tracks = 0 self.count_by_extension = {} for extension in self.allowed_extensions: self.count_by_extension[extension] = 0 self.artists = {} self.albums = {} if self.use_lastfm: import pylast self.pylast = pylast api_key = config['LASTFM_API_KEY'] self.lastfm = self.pylast.LastFMNetwork(api_key=api_key) if not len(self.media_dirs): log.error("Remember to set the MEDIA_DIRS option, otherwise I " "don't know where to look for.") if reindex: log.info('Dropping database...') confirmed = raw_input('This will destroy all the information. ' 'Proceed? [y/N] ') in ('y', 'Y') if not confirmed: log.error('Aborting.') sys.exit(1) db.drop_all() log.info('Recreating database...') db.create_all() # This is useful to know if the DB is empty, and avoid some checks if not self.reindex and not self.low_ram: try: m.Artist.query.limit(1).all() except OperationalError: self.empty_db = True
def __init__(self, config=None, use_lastfm=False, hash_files=False, no_metadata=False, reindex=False, write_every=0): self.config = config self.use_lastfm = use_lastfm self.hash_files = hash_files self.no_metadata = no_metadata self.reindex = reindex self.write_every = write_every self.empty_db = reindex # If we are going to have only 1 track in cache at any time we might as # well just ignore it completely. use_cache = (write_every != 1) self.cache = CacheManager(ram_cache=use_cache, use_db=not use_cache) self.session = db.session self.media_dirs = config.get('MEDIA_DIRS', []) self.allowed_extensions = app.config.get('ALLOWED_FILE_EXTENSIONS', self.VALID_FILE_EXTENSIONS) self._ext = None self._meta = None self.track_count = 0 self.skipped_tracks = 0 self.count_by_extension = {} for extension in self.allowed_extensions: self.count_by_extension[extension] = 0 if self.use_lastfm: self.lastfm = LastFM(api_key=config['LASTFM_API_KEY'], use_cache=(write_every > 1)) if not len(self.media_dirs): log.error("Remember to set the MEDIA_DIRS option, otherwise I " "don't know where to look for.") if reindex: log.info('Dropping database...') confirmed = raw_input('This will destroy all the information. ' 'Proceed? [y/N] ') in ('y', 'Y') if not confirmed: log.error('Aborting.') sys.exit(1) db.drop_all() log.info('Recreating database...') db.create_all() # This is useful to know if the DB is empty, and avoid some checks if not self.reindex: try: m.Track.query.limit(1).all() except OperationalError: self.empty_db = True
def __init__(self, config=None, use_lastfm=False, no_metadata=False, reindex=False): self.config = config self.use_lastfm = use_lastfm self.no_metadata = no_metadata self.reindex = reindex self.empty_db = reindex self.session = db.session self.media_dirs = config.get('MEDIA_DIRS', []) self.allowed_extensions = app.config.get('ALLOWED_FILE_EXTENSIONS', self.VALID_FILE_EXTENSIONS) self._ext = None self._meta = None self.track_count = 0 self.skipped_tracks = 0 self.count_by_extension = {} for extension in self.allowed_extensions: self.count_by_extension[extension] = 0 self.artists = {} self.albums = {} if self.use_lastfm: import pylast self.pylast = pylast api_key = config['LASTFM_API_KEY'] self.lastfm = self.pylast.LastFMNetwork(api_key=api_key) if not len(self.media_dirs): log.error("Remember to set the MEDIA_DIRS option, otherwise I " "don't know where to look for.") if reindex: log.info('Dropping database...') confirmed = raw_input('This will destroy all the information. ' 'Proceed? [y/N] ') in ('y', 'Y') if not confirmed: log.error('Aborting.') sys.exit(1) db.drop_all() log.info('Recreating database...') db.create_all() # This is useful to know if the DB is empty, and avoid some checks if not self.reindex: try: m.Artist.query.limit(1).all() except OperationalError: self.empty_db = True
def main(): arguments = docopt(__doc__) if arguments['--quiet']: log.setLevel(logging.ERROR) elif arguments['--verbose']: log.setLevel(logging.DEBUG) else: log.setLevel(logging.INFO) if arguments['--verbose-sql']: app.config['SQLALCHEMY_ECHO'] = True kwargs = { 'use_lastfm': arguments['--lastfm'], 'hash_files': arguments['--hash'], 'no_metadata': arguments['--nometadata'], 'reindex': arguments['--reindex'], 'write_every': arguments['--write-every'], } if kwargs['no_metadata']: kwargs['use_lastfm'] = False if kwargs['use_lastfm'] and not app.config.get('LASTFM_API_KEY'): sys.stderr.write('ERROR: You need a Last.FM API key if you set the ' '--lastfm flag.\n') sys.exit(2) try: if kwargs['write_every'] is not None: kwargs['write_every'] = int(kwargs['write_every']) except TypeError: sys.stderr.write('ERROR: Invalid value for --write-every, expected ' '<int>, got "%s" <%s>. instead' % ( kwargs['write_every'], type(kwargs['write_every']))) sys.exit(3) # Generate database db.create_all() lola = Indexer(app.config, **kwargs) lola.run() lola.print_stats() # Petit performance hack: Every track will be added to the session but they # will be written down to disk only once, at the end. Unless the # --write-every flag is set, then tracks are persisted in batch. lola.commit(force=True) log.debug('Checking for duplicated tracks...') lola.make_slugs_unique()
def main(): arguments = docopt(__doc__) if arguments['--quiet']: log.setLevel(logging.ERROR) elif arguments['--verbose']: log.setLevel(logging.DEBUG) else: log.setLevel(logging.INFO) if arguments['--verbose-sql']: app.config['SQLALCHEMY_ECHO'] = True kwargs = { 'use_lastfm': arguments['--lastfm'], 'no_metadata': arguments['--nometadata'], 'low_ram': arguments['--low-ram'], 'reindex': arguments['--reindex'], } if kwargs['no_metadata']: kwargs['use_lastfm'] = False if kwargs['use_lastfm'] and not app.config.get('LASTFM_API_KEY'): sys.stderr.write('ERROR: You need a Last.FM API key if you set the ' '--lastfm flag.\n') sys.exit(1) # Generate database db.create_all() lola = Indexer(app.config, **kwargs) lola.run() lola.print_stats() # Petit performance hack: Every track will be added to the session but they # will be written down to disk only once, at the end. Unless the --low-ram # flag is set, then every track is immediately persisted. log.debug('Writing to database...') lola.session.commit() log.debug('Checking for duplicated tracks...') lola.make_slugs_unique()
def main(): use_lastfm = '--lastfm' in sys.argv no_metadata = '--nometadata' in sys.argv if no_metadata: use_lastfm = False if use_lastfm and not app.config.get('LASTFM_API_KEY'): print('ERROR: You need a Last.FM API key if you set the --lastfm ' 'flag.\n') sys.exit(1) # Generate database db.create_all() lola = Indexer(app.config, use_lastfm=use_lastfm, no_metadata=no_metadata) lola.run() # Petit performance hack: Every track will be added to the session but they # will be written down to disk only once, at the end. lola.session.commit()
def setUp(self): db.create_all()
def create_db(): db.create_all() log.info('DB created.')