beets / beetsplug / albumify.py

# This file is part of beets.
# Copyright 2010, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Provides the albumify command, which "upgrades" databases created
with beets <1.0b3. It walks through the database and groups all items
not yet associated with albums into albums based on metadata.
"""

from collections import defaultdict
import logging

from beets.plugins import BeetsPlugin
import beets.ui

log = logging.getLogger('beets')

# Main functionality.
def albumify(lib):
    # Associate every (albumless) item with a unique artist/album
    # pair.
    albums = defaultdict(list)
    for item in lib.get():
        if item.album_id is None:
            albums[(item.artist, item.album)].append(item)
    
    # Create an album for each group.
    for (artist, album), items in albums.items():
        log.info('%s - %s (%i tracks)' % (artist, album, len(items)))
        lib.add_album(items)

# Plugin hook.
class AlbumifyPlugin(BeetsPlugin):
    def commands(self):
        cmd = beets.ui.Subcommand('albumify', help='group items into albums')
        def func(lib, config, opts, args):
            if args:
                raise beets.ui.UserError('albumify takes no arguments')
            albumify(lib)
            lib.save()
        cmd.func = func
        return [cmd]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.