def addReleases(artist_id, update_artist = True):
  artist_record = Artist.get(id=artist_id)
  musicbrainz_artist = musicbrainz.getBestArtistMatch(artist_record.name)
  release_ids = []

  for release in musicbrainz_artist.getReleases():
    release_ids.append(utils.extractUuid(release.id))

  # These release results do not contain all the information, we must re-query for that info...
  for rid in release_ids:
    release = musicbrainz.getRelease(rid)

    if not release: continue

    release_group_id = utils.extractUuid(release.getReleaseGroup().id)

    try:
      release_group_tracked = Album.get(release_group_id=release_group_id)
    except peewee.DoesNotExist:
      release_group_tracked = None

    if release_group_tracked: continue

    release_record = Album.create(
        musicbrainz_id = rid,
        asin = release.getAsin(),
        release_group_id = release_group_id,
        artist_id = artist_id,
        name = release.getTitle(),
        type = release.getType(),
        released_on = release.getEarliestReleaseDate(),
        state = 'wanted')

    track_number = 1

    for track in release.getTracks():
      Track.create(
          album_id = release_record.id,
          number = track_number,
          title = track.getTitle(),
          length = track.getDuration(),
          state = 'wanted')

      track_number += 1

  # Rescan the Music Library after adding new releases to see if the user has 
  # them or not. Will not run if explicitly told not to by the caller.
  if(update_artist): ThreadPool.put(updateArtist, {'artist_id': artist_id})
def scan():
  logger.info(u"Now scanning the music library located at %s." % config.music.directory)

  artists_being_added = []

  for dirpath, dirnames, filenames in os.walk(config.music.directory):
    logger.debug(u'Now scanning the directory "%s"' % unicode(dirpath, errors="ignore"))

    # Scan all of the files in this directory:
    for filename in filenames:
      # Only scan music files...
      if any(filename.lower().endswith('.' + x.lower()) for x in config.music.formats.split(',')):
        logger.debug(u'Now scanning the file "%s"' % unicode(filename, errors="ignore"))

        full_path = os.path.join(dirpath, filename)

        # Try to read the tags from the file, move on if we can't.
        try:
          media_file = MediaFile(full_path)
        except Exception, e:
          logger.debug(u'Cannot read tags of file "%s" because of the exception "%s"' % (unicode(filename, errors="ignore"), str(e)))
          break

        # If we did read the tags, but the artist can't be found, move on to the next file...
        if media_file.albumartist:
          id3_artist = media_file.albumartist
        elif media_file.artist:
          id3_artist = media_file.artist
        else:
          break

        logger.debug(u'Found the artist name "%s" in the ID3 tag of "%s" file.' % (id3_artist, unicode(full_path, errors="ignore")))

        artist_path_parts = []

        for part in dirpath.split('/'):
          artist_path_parts.append(part)

          if id3_artist.lower() in part.lower(): break

        if artist_path_parts:
          artist_path = os.sep.join(artist_path_parts)
        else:
          artist_path = config.music.directory

        # If we already have this artist in the DB only add their releases,
        # we do not need to re-add them to the Music Library.
        try:
          tracked_artist = Artist.get(name=id3_artist)
        except peewee.DoesNotExist:
          tracked_artist = None

        if tracked_artist and tracked_artist.id not in artists_being_added:
          logger.debug(u'Artist name "%s" is already tracked by Headphones, moving on but adding releases...' % id3_artist)

          ThreadPool.put(addReleases, {'artist_id': tracked_artist.id})
          artists_being_added.append(tracked_artist.id)

          break
        else:
          artist_record = addArtist(id3_artist, artist_path)

          if artist_record is not None:
            if artist_record.id not in artists_being_added:
              logger.debug('We have a new artist! Adding them now: artist_id %s' % artist_record.id)

              ThreadPool.put(addReleases, {'artist_id': artist_record.id})
              artists_being_added.append(artist_record.id)

          break