gpt4 book ai didi

python - 我如何使用 python 通过 D-Bus 从 Skype 接收事件?

转载 作者:太空狗 更新时间:2023-10-30 02:47:14 27 4
gpt4 key购买 nike

(我知道这类似于 Python and d-bus: How to set up main loop? ,但在“答案”中没有完整的代码,我无法弄清楚我哪里出错了。它可能只是 Skype 中的一个更改)

这是我的程序:

import gobject
import dbus
import dbus.mainloop.glib
dbus_gmainloop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
sessbus = dbus.SessionBus()
skype = sessbus.get_object('com.Skype.API', '/com/Skype')
skypec = sessbus.get_object('com.Skype.API', '/com/Skype/Client')
skype_iface = dbus.Interface(skype, dbus_interface='com.Skype.API')
skype_iface.Invoke("NAME py1")
# ... waits for user click in Skype ...
#==> dbus.String(u'OK')
skype_iface.Invoke("PROTOCOL 7")
#==> dbus.String(u'PROTOCOL 7')
def got_signal(sender, destination, member, interface, path):
print "got_signal(sender=%s, dest=%s, member=%s, iface=%s, path=%s)" \
% (sender, destination, member, interface, path)

skypec.connect_to_signal('Notify', got_signal, sender_keyword='sender', \
destination_keyword='destination', member_keyword='member', \
interface_keyword='interface', path_keyword='path')

mainloop = gobject.MainLoop()
mainloop.run()

运行时(例如 python skype-call.py),它会在向 Skype 发送 NAME py1 命令后暂停并等待 Skype UI 中的交互式确认,然后继续。因此,skype_iface 对象显然至少在一定程度上起作用。

但是,python 会发出以下错误:

ERROR:dbus.proxies:Introspect error on :1.152:/com/Skype/Client: dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownObject: No such object path '/com/Skype/Client'

我还尝试了以下方法(而不是 connect_to_signal,就在最后开始 gobject 主循环之前):

def receiver(x, **kwargs):
print "receiver(%s)" % (x,)

sessbus.add_signal_receiver(receiver, signal_name='Notify', \
dbus_interface='com.Skype.API', bus_name='com.Skype.API', path='/com/Skype/Client')

虽然它没有提示,但它永远不会被调用。我尝试向 Skype 用户发送消息。什么样的事件应该触发它?

文档位于 https://dev.skype.com/desktop-api-reference#DBUSUsage不是很有帮助。

这是基于 Debian 7.0 多架构 (amd64/i386) 的 Linux 4.2.0.11 版 Skype。

最佳答案

回顾http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html#exporting-methods-with-dbus-service-method ,我试着以他们为例:

import gobject
import dbus
from dbus.decorators import method
import dbus.mainloop.glib
import dbus.service

dbus_gmainloop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
sessbus = dbus.SessionBus()

class Example(dbus.service.Object):
def __init__(self, bus):
dbus.service.Object.__init__(self, bus, '/com/Skype/Client')

@method(dbus_interface='com.Skype.API.Client', in_signature='s', \
sender_keyword='sender', destination_keyword='dest', \
rel_path_keyword='rel_path', path_keyword='path', \
message_keyword='message', connection_keyword='conn')
def Notify(self, s, sender=None, dest=None, rel_path=None, path=None, \
message=None, conn=None):
print "Notify(%s, sender=%s, dest=%s, rel_path=%s, path=%s, message=%s, conn=%s)" \
% (s, sender, path, dest, message, rel_path, conn,)

# make one:
ex = Example(sessbus)

skype = sessbus.get_object('com.Skype.API', '/com/Skype')
skype_iface = dbus.Interface(skype, dbus_interface='com.Skype.API')
skype_iface.Invoke("NAME py1")
##=> dbus.String(u'OK')
skype_iface.Invoke("PROTOCOL 7")
##=> dbus.String(u'PROTOCOL 7')

mainloop = gobject.MainLoop()
mainloop.run()

你瞧,我接到了 Notify 电话:

Notify(CONNSTATUS ONLINE, sender=:1.152, path=/com/Skype/Client, dest=:1.275, message=<dbus.lowlevel.MethodCallMessage path: /com/Skype/Client, iface: com.Skype.API.Client, member: Notify dest: :1.275>, rel_path=/, conn=<dbus._dbus.SessionBus (session) at 0x2118e90>)

Notify(CURRENTUSERHANDLE ******, sender=:1.152, path=/com/Skype/Client, dest=:1.275, message=<dbus.lowlevel.MethodCallMessage path: /com/Skype/Client, iface: com.Skype.API.Client, member: Notify dest: :1.275>, rel_path=/, conn=<dbus._dbus.SessionBus (session) at 0x2118e90>)

Notify(USERSTATUS ONLINE, sender=:1.152, path=/com/Skype/Client, dest=:1.275, message=<dbus.lowlevel.MethodCallMessage path: /com/Skype/Client, iface: com.Skype.API.Client, member: Notify dest: :1.275>, rel_path=/, conn=<dbus._dbus.SessionBus (session) at 0x2118e90>)

显然,这不是一个结构良好的程序的好例子,但 d-bus 位似乎确实连接了。

关于python - 我如何使用 python 通过 D-Bus 从 Skype 接收事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16755042/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com