From 473fe04f91c58f1542c92621ce9b72ed954e9f47 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Fri, 24 Aug 2012 19:20:27 -0700 Subject: [PATCH] web: add size to item JSON in API This is to be used by the Tomahawk resolver, which wants file sizes. --- beetsplug/web/__init__.py | 16 +++++++++++++++- docs/changelog.rst | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/beetsplug/web/__init__.py b/beetsplug/web/__init__.py index 34d55e1ab..4697ffd65 100644 --- a/beetsplug/web/__init__.py +++ b/beetsplug/web/__init__.py @@ -1,5 +1,5 @@ # This file is part of beets. -# Copyright 2011, Adrian Sampson. +# Copyright 2012, Adrian Sampson. # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -15,9 +15,11 @@ """A Web interface to beets.""" from beets.plugins import BeetsPlugin from beets import ui +from beets import util import beets.library import flask from flask import g +import os DEFAULT_HOST = '' DEFAULT_PORT = 8337 @@ -26,10 +28,22 @@ DEFAULT_PORT = 8337 # Utilities. def _rep(obj): + """Get a flat -- i.e., JSON-ish -- representation of a beets Item or + Album object. + """ if isinstance(obj, beets.library.Item): out = dict(obj.record) del out['path'] + + # Get the size (in bytes) of the backing file. This is useful + # for the Tomahawk resolver API. + try: + out['size'] = os.path.getsize(util.syspath(obj.path)) + except OSError: + out['size'] = 0 + return out + elif isinstance(obj, beets.library.Album): out = dict(obj._record) del out['artpath'] diff --git a/docs/changelog.rst b/docs/changelog.rst index 005f9733f..c59fa8ea9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -15,10 +15,14 @@ Changelog * :doc:`/plugins/fetchart`: Fix a bug where cover art filenames could lack a ``.jpg`` extension. * :doc:`/plugins/lyrics`: Fix an exception with non-ASCII lyrics. +* :doc:`/plugins/web`: The API now reports file sizes (for use with the + `Tomahawk resolver`_). * Add the track mapping dictionary to the ``album_distance`` plugin function. * Fix an assertion failure when the MusicBrainz main database and search server disagree. +.. _Tomahawk resolver: http://beets.radbox.org/blog/tomahawk-resolver.html + 1.0b15 (July 26, 2012) ----------------------