This file is indexed.

/usr/share/beets/beetsplug/metasync/amarok.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
# -*- coding: utf-8 -*-
# This file is part of beets.
# Copyright 2016, Heinz Wiesinger.
#
# 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.

"""Synchronize information from amarok's library via dbus
"""

from __future__ import division, absolute_import, print_function

from os.path import basename
from datetime import datetime
from time import mktime
from xml.sax.saxutils import quoteattr

from beets.util import displayable_path
from beets.dbcore import types
from beets.library import DateType
from beetsplug.metasync import MetaSource


def import_dbus():
    try:
        return __import__('dbus')
    except ImportError:
        return None

dbus = import_dbus()


class Amarok(MetaSource):

    item_types = {
        'amarok_rating':      types.INTEGER,
        'amarok_score':       types.FLOAT,
        'amarok_uid':         types.STRING,
        'amarok_playcount':   types.INTEGER,
        'amarok_firstplayed': DateType(),
        'amarok_lastplayed':  DateType(),
    }

    queryXML = u'<query version="1.0"> \
                    <filters> \
                        <and><include field="filename" value=%s /></and> \
                    </filters> \
                </query>'

    def __init__(self, config, log):
        super(Amarok, self).__init__(config, log)

        if not dbus:
            raise ImportError('failed to import dbus')

        self.collection = \
            dbus.SessionBus().get_object('org.kde.amarok', '/Collection')

    def sync_from_source(self, item):
        path = displayable_path(item.path)

        # amarok unfortunately doesn't allow searching for the full path, only
        # for the patch relative to the mount point. But the full path is part
        # of the result set. So query for the filename and then try to match
        # the correct item from the results we get back
        results = self.collection.Query(
            self.queryXML % quoteattr(basename(path))
        )
        for result in results:
            if result['xesam:url'] != path:
                continue

            item.amarok_rating = result['xesam:userRating']
            item.amarok_score = result['xesam:autoRating']
            item.amarok_playcount = result['xesam:useCount']
            item.amarok_uid = \
                result['xesam:id'].replace('amarok-sqltrackuid://', '')

            if result['xesam:firstUsed'][0][0] != 0:
                # These dates are stored as timestamps in amarok's db, but
                # exposed over dbus as fixed integers in the current timezone.
                first_played = datetime(
                    result['xesam:firstUsed'][0][0],
                    result['xesam:firstUsed'][0][1],
                    result['xesam:firstUsed'][0][2],
                    result['xesam:firstUsed'][1][0],
                    result['xesam:firstUsed'][1][1],
                    result['xesam:firstUsed'][1][2]
                )

                if result['xesam:lastUsed'][0][0] != 0:
                    last_played = datetime(
                        result['xesam:lastUsed'][0][0],
                        result['xesam:lastUsed'][0][1],
                        result['xesam:lastUsed'][0][2],
                        result['xesam:lastUsed'][1][0],
                        result['xesam:lastUsed'][1][1],
                        result['xesam:lastUsed'][1][2]
                    )
                else:
                    last_played = first_played

                item.amarok_firstplayed = mktime(first_played.timetuple())
                item.amarok_lastplayed = mktime(last_played.timetuple())