Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
PLUGIN_NAME = u"Add Track As Recording"
PLUGIN_AUTHOR = u"Freso, Lukáš Lalinský, Philip Jägenstedt"
PLUGIN_DESCRIPTION = ""
PLUGIN_VERSION = "0.1"
PLUGIN_API_VERSIONS = ["1.0.0"]
from picard import log
#from picard.metadata import register_file_metadata_processor
from picard.file import File
from picard.util import webbrowser2
from picard.ui.itemviews import BaseAction, register_file_action
import pprint
import codecs
import os
import tempfile
HTML_HEAD = """<!doctype html>
<meta charset="UTF-8">
<title>Add Track As Recording</title>
<form action="http://musicbrainz.org/recording/create" method="post">
"""
HTML_INPUT = """<input type="hidden" name="%s" value="%s">
"""
HTML_TAIL = """<input type="submit" value="Add Recording">
</form>
<script>document.forms[0].submit()</script>
"""
HTML_ATTR_ESCAPE = {
"&": "&amp;",
'"': "&quot;"
}
def _introspect(obj):
log.debug(dir(obj))
log.debug(pprint.pformat(obj.metadata))
class AddTrackAsRecording(BaseAction):
NAME = "Add Track As Recording..."
def callback(self, objs):
if len(objs) != 1 or not isinstance(objs[0], File):
return
track = objs[0]
#_introspect(track)
(fd, fp) = tempfile.mkstemp(suffix=".html")
f = codecs.getwriter("utf-8")(os.fdopen(fd, "w"))
def esc(s):
return "".join(HTML_ATTR_ESCAPE.get(c, c) for c in s)
# add a global (release-level) name-value
def nv(n, v):
f.write(HTML_INPUT % (esc(n), esc(v)))
f.write(HTML_HEAD)
nv("edit-recording.name", track.metadata["title"])
nv("edit-recording.artist_credit.names.0.artist.name", track.metadata["artist"])
if track.metadata["length"]:
nv("edit-recording.length", str(track.metadata["length"]))
elif track.metadata["~length"]:
nv("edit-recording.length", str(track.metadata["~length"]))
# Edit note
editnote = u"""Recording added by the "Add File As Recording" Picard \
plugin, using the file »%s« as input.""" % (
track.metadata['~filename'] + '.' + track.metadata['~extension']
)
nv("edit-recording.edit_note", editnote)
f.write(HTML_TAIL)
f.close()
webbrowser2.open("file://" + fp)
register_file_action(AddTrackAsRecording())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.