Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rewrote indexer to use new MetadataManager

  • Loading branch information...
commit 965fdd18411b7f71f24cd6f6ab252de3149437f8 1 parent e937443
Danilo Bargen authored
Showing with 55 additions and 64 deletions.
  1. +34 −42 indexer.py
  2. +18 −19 shiva/models.py
  3. +2 −2 shiva/resources.py
  4. +1 −1  shiva/utils.py
76 indexer.py
View
@@ -21,7 +21,7 @@
from shiva import models as m
from shiva.app import app, db
-from shiva.utils import ID3Manager
+from shiva.utils import MetadataManager
q = db.session.query
@@ -38,7 +38,9 @@ def __init__(self, config=None, use_lastfm=False, no_metadata=False, verbose=Fal
self.session = db.session
self.media_dirs = config.get('MEDIA_DIRS', [])
- self.id3r = None
+
+ self._meta = None
+
self.artists = {}
self.albums = {}
@@ -49,7 +51,7 @@ def __init__(self, config=None, use_lastfm=False, no_metadata=False, verbose=Fal
api_key = config['LASTFM_API_KEY']
self.lastfm = self.pylast.LastFMNetwork(api_key=api_key)
- if len(self.media_dirs) == 0:
+ if not len(self.media_dirs):
print("Remember to set the MEDIA_DIRS option, otherwise I don't "
'know where to look for.')
@@ -91,14 +93,14 @@ def get_album(self, name, artist):
def get_release_year(self, lastfm_album=None):
if not self.use_lastfm or not lastfm_album:
- return self.get_id3_reader().release_year
+ return self.get_metadata_reader().release_year
_date = lastfm_album.get_release_date()
if not _date:
- if not self.get_id3_reader().release_year:
+ if not self.get_metadata_reader().release_year:
return None
- return self.get_id3_reader().release_year
+ return self.get_metadata_reader().release_year
return datetime.strptime(_date, '%d %b %Y, %H:%M').year
@@ -117,17 +119,15 @@ def save_track(self):
track = m.Track(full_path)
if self.no_metadata:
self.session.add(track)
-
return True
else:
if q(m.Track).filter_by(path=full_path).count():
return True
- use_prev = None
- id3r = self.get_id3_reader()
+ meta = self.get_metadata_reader()
- artist = self.get_artist(id3r.artist)
- album = self.get_album(id3r.album, artist)
+ artist = self.get_artist(meta.artist)
+ album = self.get_album(meta.album, artist)
if artist is not None and artist not in album.artists:
album.artists.append(artist)
@@ -136,13 +136,16 @@ def save_track(self):
track.artist = artist
self.session.add(track)
- return True
+ self.count += 1
+ if self.count % 10 == 0:
+ self.session.commit()
- def get_id3_reader(self):
- if not self.id3r or not self.id3r.same_path(self.file_path):
- self.id3r = ID3Manager(self.file_path)
+ return True
- return self.id3r
+ def get_metadata_reader(self):
+ if not self._meta or self._meta.origpath != self.file_path:
+ self._meta = MetadataManager(self.file_path)
+ return self._meta
def is_track(self):
"""Tries to guess whether the file is a valid track or not.
@@ -156,38 +159,27 @@ def is_track(self):
ext = self.file_path[self.file_path.rfind('.') + 1:]
if ext not in self.config.get('ACCEPTED_FORMATS', []):
if not self.quiet:
- print(self.file_path + "is not in ACCEPTED_FORMATS")
- return False
-
- if not self.get_id3_reader().is_valid():
- if not self.quiet:
- print(self.file_path + "fails id3 reader")
+ print(self.file_path + ' is not in ACCEPTED_FORMATS')
return False
return True
- def walk(self, dir_name):
- """Recursively walks through a directory looking for tracks.
- """
+ def walk(self, target):
+ """Recursively walks through a directory looking for tracks."""
- self.count += 1
- if self.count % 10 == 0:
- self.session.commit()
+ # If target is a file, try to save it as a track
+ if os.path.isfile(target):
+ self.file_path = target
+ if self.is_track():
+ self.save_track()
- if os.path.isdir(dir_name):
- for name in os.listdir(dir_name):
- self.file_path = os.path.join(dir_name, name)
- if os.path.isdir(self.file_path):
- self.walk(self.file_path)
- else:
+ # Otherwise, recursively walk the directory looking for files
+ else:
+ for root, dirs, files in os.walk(target):
+ for name in files:
+ self.file_path = os.path.join(root, name)
if self.is_track():
- try:
- self.save_track()
- except Exception, e:
- logging.warning("%s not imported - %s" % (
- self.file_path, e.message))
-
- return True
+ self.save_track()
def run(self):
for mobject in self.media_dirs:
@@ -195,7 +187,7 @@ def run(self):
self.walk(mdir)
-def main():
+if __name__ == '__main__':
from docopt import docopt
arguments = docopt(__doc__)
37 shiva/models.py
View
@@ -3,7 +3,7 @@
from flask.ext.sqlalchemy import SQLAlchemy
-from shiva.utils import slugify as do_slug, randstr, ID3Manager
+from shiva.utils import slugify as do_slug, randstr, MetadataManager
db = SQLAlchemy()
@@ -46,7 +46,7 @@ class Artist(db.Model):
__tablename__ = 'artists'
pk = db.Column(db.Integer, primary_key=True)
- # TODO: Update the files' ID3 tags when changing this info.
+ # TODO: Update the files' Metadata when changing this info.
name = db.Column(db.String(128), nullable=False)
slug = db.Column(db.String(128), unique=True, nullable=False)
image = db.Column(db.String(256))
@@ -98,8 +98,7 @@ def __repr__(self):
class Track(db.Model):
- """
- """
+ """Track model."""
__tablename__ = 'tracks'
@@ -110,6 +109,7 @@ class Track(db.Model):
bitrate = db.Column(db.Integer)
file_size = db.Column(db.Integer)
length = db.Column(db.Integer)
+ # TODO number should probably be renamed to track or track_number
number = db.Column(db.Integer)
lyrics = db.relationship('Lyrics', backref='track', uselist=False)
@@ -119,7 +119,7 @@ class Track(db.Model):
nullable=True)
def __init__(self, path):
- if type(path) not in (unicode, str, file):
+ if not isinstance(path, (basestring, file)):
raise ValueError('Invalid parameter for Track. Path or File '
'expected, got %s' % type(path))
@@ -128,7 +128,7 @@ def __init__(self, path):
_path = path.name
self.set_path(_path)
- self._id3r = None
+ self._meta = None
def __setattr__(self, attr, value):
if attr == 'title':
@@ -146,19 +146,18 @@ def set_path(self, path):
if path != self.get_path():
self.path = path
if os.path.exists(self.get_path()):
- self.file_size = self.get_id3_reader().size
- self.bitrate = self.get_id3_reader().bitrate
- self.length = self.get_id3_reader().length
- self.number = self.get_id3_reader().track_number
- self.title = self.get_id3_reader().title
-
- def get_id3_reader(self):
- """Returns an object with the ID3 info reader.
- """
- if not getattr(self, '_id3r', None):
- self._id3r = ID3Manager(self.get_path())
-
- return self._id3r
+ meta = self.get_metadata_reader()
+ self.file_size = meta.filesize
+ self.bitrate = meta.bitrate
+ self.length = meta.length
+ self.number = meta.track_number
+ self.title = meta.title
+
+ def get_metadata_reader(self):
+ """Return a MetadataManager object."""
+ if not getattr(self, '_meta', None):
+ self._meta = MetadataManager(self.get_path())
+ return self._meta
def __repr__(self):
return "<Track ('%s')>" % self.title
4 shiva/resources.py
View
@@ -499,8 +499,8 @@ def __init__(self, artist, json):
self.venue = json['venue']
def split_artists(self, json):
- if len(json) == 0:
- ([], [])
+ if not len(json):
+ return ([], [])
elif len(json) == 1:
artist = Artist.query.filter_by(name=json[0]['name']).first()
2  shiva/utils.py
View
@@ -114,7 +114,7 @@ def genre(self, value):
@property
def length(self):
"""The length of the song in seconds."""
- return self.reader.info.time_secs
+ return self.reader.info.length
@property
def bitrate(self):
Please sign in to comment.
Something went wrong with that request. Please try again.