def add_file(filename, add_labels=None, owner=None, quiet=False, artist=None, title=None, dlink=None): """Adds the file to the database. Returns track id. """ if not os.path.exists(filename): raise Exception('File not found: %s' % filename) if not quiet: logging.info('Adding from %s' % filename) ardj.replaygain.update(filename) tags = ardj.tags.get(str(filename)) or {} duration = tags.get('length', 0) labels = tags.get('labels', []) if artist is None: artist = tags.get('artist', 'Unknown Artist') if title is None: title = tags.get('title', 'Untitled') if add_labels and not labels: labels = add_labels abs_filename, rel_filename = gen_filename(os.path.splitext(filename)[1]) if not ardj.util.copy_file(filename, abs_filename): raise Exception('Could not copy %s to %s' % (filename, abs_filename)) track_id = ardj.database.execute('INSERT INTO tracks (artist, title, filename, length, last_played, owner, weight, real_weight, count, download) VALUES (?, ?, ?, ?, ?, ?, 1, 1, 0, ?)', (artist, title, rel_filename, duration, 0, owner or 'ardj', dlink, )) for label in labels: ardj.database.execute('INSERT INTO labels (track_id, label, email) VALUES (?, ?, ?)', (track_id, label, (owner or 'ardj').lower(), )) return track_id
def cmd_tags(*args): """display tags from files""" if not args: print "Files not specified." return False from ardj import tags for fn in args: if os.path.exists(fn): print "Tags in %s" % fn for k, v in sorted(tags.get(arg).items(), key=lambda x: x[0]): print ' %s = %s' % (k, v)
def from_file(cls, filename, labels=None): """Creates a new track from file, saves it. If no labels were specified, adds the default ones.""" filepath = os.path.join(ardj.settings.get_music_dir(), filename) tags = ardj.tags.get(filepath) t = cls(filename=filename.decode("utf-8")) if "length" not in tags: raise Exception("No length in %s" % filename) t["artist"] = tags.get("artist", "Unknown Artist") t["title"] = tags.get("title", os.path.basename(filename).decode("utf-8")) t["length"] = tags["length"] t["weight"] = 1.0 t["real_weight"] = 1.0 t.put() if labels is None: labels = ardj.settings.get("default_labels") if labels: t.set_labels(labels) return t