This file is indexed.

/usr/share/beets/beetsplug/absubmit.py is in beets 1.4.6-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- coding: utf-8 -*-
# This file is part of beets.
# Copyright 2016, Pieter Mulder.
#
# 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.

"""Calculate acoustic information and submit to AcousticBrainz.
"""

from __future__ import division, absolute_import, print_function

import errno
import hashlib
import json
import os
import subprocess
import tempfile

from distutils.spawn import find_executable
import requests

from beets import plugins
from beets import util
from beets import ui


class ABSubmitError(Exception):
    """Raised when failing to analyse file with extractor."""


def call(args):
    """Execute the command and return its output.

    Raise a AnalysisABSubmitError on failure.
    """
    try:
        return util.command_output(args)
    except subprocess.CalledProcessError as e:
        raise ABSubmitError(
            u'{0} exited with status {1}'.format(args[0], e.returncode)
        )


class AcousticBrainzSubmitPlugin(plugins.BeetsPlugin):

    def __init__(self):
        super(AcousticBrainzSubmitPlugin, self).__init__()

        self.config.add({'extractor': u''})

        self.extractor = self.config['extractor'].as_str()
        if self.extractor:
            self.extractor = util.normpath(self.extractor)
            # Expicit path to extractor
            if not os.path.isfile(self.extractor):
                raise ui.UserError(
                    u'Extractor command does not exist: {0}.'.
                    format(self.extractor)
                )
        else:
            # Implicit path to extractor, search for it in path
            self.extractor = 'streaming_extractor_music'
            try:
                call([self.extractor])
            except OSError:
                raise ui.UserError(
                    u'No extractor command found: please install the '
                    u'extractor binary from http://acousticbrainz.org/download'
                )
            except ABSubmitError:
                # Extractor found, will exit with an error if not called with
                # the correct amount of arguments.
                pass

            # Get the executable location on the system, which we need
            # to calculate the SHA-1 hash.
            self.extractor = find_executable(self.extractor)

        # Calculate extractor hash.
        self.extractor_sha = hashlib.sha1()
        with open(self.extractor, 'rb') as extractor:
            self.extractor_sha.update(extractor.read())
        self.extractor_sha = self.extractor_sha.hexdigest()

    base_url = 'https://acousticbrainz.org/api/v1/{mbid}/low-level'

    def commands(self):
        cmd = ui.Subcommand(
            'absubmit',
            help=u'calculate and submit AcousticBrainz analysis'
        )
        cmd.func = self.command
        return [cmd]

    def command(self, lib, opts, args):
        # Get items from arguments
        items = lib.items(ui.decargs(args))
        for item in items:
            analysis = self._get_analysis(item)
            if analysis:
                self._submit_data(item, analysis)

    def _get_analysis(self, item):
        mbid = item['mb_trackid']
        # If file has no mbid skip it.
        if not mbid:
            self._log.info(u'Not analysing {}, missing '
                           u'musicbrainz track id.', item)
            return None

        # Temporary file to save extractor output to, extractor only works
        # if an output file is given. Here we use a temporary file to copy
        # the data into a python object and then remove the file from the
        # system.
        tmp_file, filename = tempfile.mkstemp(suffix='.json')
        try:
            # Close the file, so the extractor can overwrite it.
            os.close(tmp_file)
            try:
                call([self.extractor, util.syspath(item.path), filename])
            except ABSubmitError as e:
                self._log.warning(
                    u'Failed to analyse {item} for AcousticBrainz: {error}',
                    item=item, error=e
                )
                return None
            with open(filename, 'rb') as tmp_file:
                analysis = json.load(tmp_file)
            # Add the hash to the output.
            analysis['metadata']['version']['essentia_build_sha'] = \
                self.extractor_sha
            return analysis
        finally:
            try:
                os.remove(filename)
            except OSError as e:
                # ENOENT means file does not exist, just ignore this error.
                if e.errno != errno.ENOENT:
                    raise

    def _submit_data(self, item, data):
        mbid = item['mb_trackid']
        headers = {'Content-Type': 'application/json'}
        response = requests.post(self.base_url.format(mbid=mbid),
                                 json=data, headers=headers)
        # Test that request was successful and raise an error on failure.
        if response.status_code != 200:
            try:
                message = response.json()['message']
            except (ValueError, KeyError) as e:
                message = u'unable to get error message: {}'.format(e)
            self._log.error(
                u'Failed to submit AcousticBrainz analysis of {item}: '
                u'{message}).', item=item, message=message
            )
        else:
            self._log.debug(u'Successfully submitted AcousticBrainz analysis '
                            u'for {}.', item)