开发者

Error setting status to empathy with dbus

I'm getting error when I try setting status to empathy with dbus using python, this is the code I've got from different sources

## getting status from clementine music player

import dbus

# Clementine lives on the Session bus
session_bus = dbus.SessionBus()

# Get Clementine's player object, and then get an interface from that object,
# otherwise we'd have to type out the full interface name on every method call.
player = session_bus.get_object('org.mpris.clementine', '/Player')
iface = dbus.Interface(player, dbus_interface='org.freedesktop.MediaPlayer')

# Call a method on the interface
metadata = iface.GetMetadata()
print metadata["title"]+' - '+metadata["artist"]
status = metadata["title"]+' - '+metadata["artist"]

## the below code is from https://github.com/engla/kupfer/blob/master/kupfer/plugin/empathy.py
import os
import subprocess
import sys
import time

import pynotify as pn

# it takes a long time before empathy is willing to accept statuses
EMPATHY_STARTUP_SECONDS = 20

def show_usage():
    print "\nUsage:"
    print sys.argv[0], "|".join(_STATUSES.keys())

def set_status(status):
    try:

开发者_如何学编程        activate(status)
        notify_set_status(status)
    except IndexError:
        print "Missing required parameter."
        show_usage()
    except ValueError as err:
        print err
        show_usage()

def notify_set_status(status):
    success = pn.init("icon-summary-body")
    if not success:
        raise Error()

    # I like this icon, even if it's not relevant
    icon = 'notification-keyboard-brightness-low'
    pn.Notification("Empathy", "Tried to set status to "+ status, icon).show()

def main():# give empathy some time to start up
    set_status(status)


def _(text):
    return text

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# All code below was derived from https://github.com/engla/kupfer/blob/master/kupfer/plugin/empathy.py
ACCOUNTMANAGER_PATH = "/org/freedesktop/Telepathy/AccountManager"
ACCOUNTMANAGER_IFACE = "org.freedesktop.Telepathy.AccountManager"
ACCOUNT_IFACE = "org.freedesktop.Telepathy.Account"
CHANNEL_GROUP_IFACE = "org.freedesktop.Telepathy.Channel.Interface.Group"
CONTACT_IFACE = "org.freedesktop.Telepathy.Connection.Interface.Contacts"
SIMPLE_PRESENCE_IFACE = "org.freedesktop.Telepathy.Connection.Interface.SimplePresence"
DBUS_PROPS_IFACE = "org.freedesktop.DBus.Properties"
CHANNELDISPATCHER_IFACE = "org.freedesktop.Telepathy.ChannelDispatcher"
CHANNELDISPATCHER_PATH = "/org/freedesktop/Telepathy/ChannelDispatcher"
CHANNEL_TYPE = "org.freedesktop.Telepathy.Channel.ChannelType"
CHANNEL_TYPE_TEXT = "org.freedesktop.Telepathy.Channel.Type.Text"
CHANNEL_TARGETHANDLE = "org.freedesktop.Telepathy.Channel.TargetHandle"
CHANNEL_TARGETHANDLETYPE = "org.freedesktop.Telepathy.Channel.TargetHandleType"
EMPATHY_CLIENT_IFACE = "org.freedesktop.Telepathy.Client.Empathy"

EMPATHY_ACCOUNT_KEY = "EMPATHY_ACCOUNT"
EMPATHY_CONTACT_ID = "EMPATHY_CONTACT_ID"


_ATTRIBUTES = {
    'alias': 'org.freedesktop.Telepathy.Connection.Interface.Aliasing/alias',
    'presence': 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence/presence',
    'contact_caps': 'org.freedesktop.Telepathy.Connection.Interface.ContactCapabilities.DRAFT/caps',
    'jid': 'org.freedesktop.Telepathy.Connection/contact-id',
    'caps': 'org.freedesktop.Telepathy.Connection.Interface.Capabilities/caps',
}
def _create_dbus_connection():
        sbus = dbus.SessionBus()
        proxy_obj = sbus.get_object(ACCOUNTMANAGER_IFACE, ACCOUNTMANAGER_PATH)
        dbus_iface = dbus.Interface(proxy_obj, DBUS_PROPS_IFACE)
        return dbus_iface

def activate(status):

    bus = dbus.SessionBus()
    interface = _create_dbus_connection()
    for valid_account in interface.Get(ACCOUNTMANAGER_IFACE, "ValidAccounts"):
        account = bus.get_object(ACCOUNTMANAGER_IFACE, valid_account)
        connection_status = account.Get(ACCOUNT_IFACE, "ConnectionStatus")
        if connection_status != 0:
            continue

        connection_path = account.Get(ACCOUNT_IFACE, "Connection")
        connection_iface = connection_path.replace("/", ".")[1:]
        connection = bus.get_object(connection_iface, connection_path)
        simple_presence = dbus.Interface(connection, SIMPLE_PRESENCE_IFACE)
        try:
            simple_presence.SetPresence(status, _(status))
        except dbus.exceptions.DBusException:
            print(status + ' is not supported by ' + valid_account)
            print simple_presence

main()

when I run this script, I get the following error.

phanindra@phanindra:~$ python clementine.py 
onelove - Blue

(clementine.py:6142): Gtk-WARNING **: Unable to locate theme engine in module_path: "pixmap",
onelove - Blue is not supported by /org/freedesktop/Telepathy/Account/gabble/jabber/abcd_40gmail_2ecom0

I did something wrong? or the functions are deprecated?


Try this to update the status of Empathy with the current track playing in Clementine:

import dbus
session_bus = dbus.SessionBus()
player = session_bus.get_object('org.mpris.clementine', '/Player')
iface = dbus.Interface(player, dbus_interface='org.freedesktop.MediaPlayer')
metadata = iface.GetMetadata()
status = "♫ ".decode('utf8')+metadata["title"]+' - '+metadata["album"]+" ♫".decode('utf8')
print status
from gi.repository import TelepathyGLib as Tp
from gi.repository import GObject
loop = GObject.MainLoop()
am = Tp.AccountManager.dup()
am.prepare_async(None, lambda *args: loop.quit(), None)
loop.run()
am.set_all_requested_presences(Tp.ConnectionPresenceType.AVAILABLE,
'available', status)

Thank you for updating the Format in my original post ! - Harsha


Here is the API specification for the SimplePresence API's SetPresence call: http://telepathy.freedesktop.org/spec/Connection_Interface_Simple_Presence.html#Method:SetPresence

I think the problem is that you're trying to set the presence to an invalid status - you are only allow to set the status to one which the connection manager recognises - e.g. Available. You can set whatever you like for the message.

So your code should read something like:

simple_presence.SetPresence("Available", status)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜