| 1 |
"""Synchronize information from amarok's library via dbus""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from datetime import datetime |
| 6 |
from os.path import basename |
| 7 |
from time import mktime |
| 8 |
from typing import TYPE_CHECKING, ClassVar |
| 9 |
from xml.sax.saxutils import quoteattr |
| 10 |
|
| 11 |
from beets.dbcore import types |
| 12 |
from beets.util import displayable_path |
| 13 |
from beetsplug.metasync import MetaSource |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from types import ModuleType |
| 17 |
|
| 18 |
from confuse import ConfigView |
| 19 |
|
| 20 |
from beets.library import Item |
| 21 |
from beets.logging import BeetsLogger as Logger |
| 22 |
|
| 23 |
|
| 24 |
def import_dbus() -> ModuleType | None: |
| 25 |
try: |
| 26 |
return __import__("dbus") |
| 27 |
except ImportError: |
| 28 |
return None |
| 29 |
|
| 30 |
|
| 31 |
dbus = import_dbus() |
| 32 |
|
| 33 |
|
| 34 |
class Amarok(MetaSource): |
| 35 |
item_types: ClassVar[dict[str, types.Type]] = { |
| 36 |
"amarok_rating": types.INTEGER, |
| 37 |
"amarok_score": types.FLOAT, |
| 38 |
"amarok_uid": types.STRING, |
| 39 |
"amarok_playcount": types.INTEGER, |
| 40 |
"amarok_firstplayed": types.DATE, |
| 41 |
"amarok_lastplayed": types.DATE, |
| 42 |
} |
| 43 |
|
| 44 |
query_xml = """ |
| 45 |
<query version="1.0"> |
| 46 |
<filters> |
| 47 |
<and><include field="filename" value={} /></and> |
| 48 |
</filters> |
| 49 |
</query>""" |
| 50 |
|
| 51 |
def __init__(self, config: ConfigView, log: Logger) -> None: |
| 52 |
super().__init__(config, log) |
| 53 |
|
| 54 |
if not dbus: |
| 55 |
raise ImportError("failed to import dbus") |
| 56 |
|
| 57 |
self.collection = dbus.SessionBus().get_object( |
| 58 |
"org.kde.amarok", "/Collection" |
| 59 |
) |
| 60 |
|
| 61 |
def sync_from_source(self, item: Item) -> None: |
| 62 |
path = displayable_path(item.path) |
| 63 |
|
| 64 |
# amarok unfortunately doesn't allow searching for the full path, only |
| 65 |
# for the patch relative to the mount point. But the full path is part |
| 66 |
# of the result set. So query for the filename and then try to match |
| 67 |
# the correct item from the results we get back |
| 68 |
results = self.collection.Query( |
| 69 |
self.query_xml.format(quoteattr(basename(path))) |
| 70 |
) |
| 71 |
for result in results: |
| 72 |
if result["xesam:url"] != path: |
| 73 |
continue |
| 74 |
|
| 75 |
item.amarok_rating = result["xesam:userRating"] |
| 76 |
item.amarok_score = result["xesam:autoRating"] |
| 77 |
item.amarok_playcount = result["xesam:useCount"] |
| 78 |
item.amarok_uid = result["xesam:id"].replace( |
| 79 |
"amarok-sqltrackuid://", "" |
| 80 |
) |
| 81 |
|
| 82 |
if result["xesam:firstUsed"][0][0] != 0: |
| 83 |
# These dates are stored as timestamps in amarok's db, but |
| 84 |
# exposed over dbus as fixed integers in the current timezone. |
| 85 |
first_played = datetime( |
| 86 |
result["xesam:firstUsed"][0][0], |
| 87 |
result["xesam:firstUsed"][0][1], |
| 88 |
result["xesam:firstUsed"][0][2], |
| 89 |
result["xesam:firstUsed"][1][0], |
| 90 |
result["xesam:firstUsed"][1][1], |
| 91 |
result["xesam:firstUsed"][1][2], |
| 92 |
) |
| 93 |
|
| 94 |
if result["xesam:lastUsed"][0][0] != 0: |
| 95 |
last_played = datetime( |
| 96 |
result["xesam:lastUsed"][0][0], |
| 97 |
result["xesam:lastUsed"][0][1], |
| 98 |
result["xesam:lastUsed"][0][2], |
| 99 |
result["xesam:lastUsed"][1][0], |
| 100 |
result["xesam:lastUsed"][1][1], |
| 101 |
result["xesam:lastUsed"][1][2], |
| 102 |
) |
| 103 |
else: |
| 104 |
last_played = first_played |
| 105 |
|
| 106 |
item.amarok_firstplayed = mktime(first_played.timetuple()) |
| 107 |
item.amarok_lastplayed = mktime(last_played.timetuple()) |