如何检索 Google Talk 用户状态消息

发布于 2024-09-25 17:27:50 字数 63 浏览 4 评论 0原文

我希望能够使用 Python 检索用户的 Google Talk 状态消息,但很难找到有关如何使用某些库的文档。

I'd like to be able to retrieve a users Google Talk Status Message with Python, it's really hard to find documentation on how to use some of the libraries out there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2024-10-02 17:27:50

我没有安装 xmpp 的任何东西,但这里有一些我手头上的旧代码可能会对您有所帮助。您需要将用户名/密码更新为您自己的值以进行测试。

需要注意的事项:登录 Google Talk 的用户会在其用户 ID 上获得一个随机存在字符串:如果您试图获取其他用户的状态,那么这并不重要,但如果您想编写一些代码以便与其他用户进行通信您自己需要区分从 GMail 或 GTalk 客户端登录的用户与测试程序。因此,代码搜索用户 ID。

另外,如果您在登录后立即查看状态,您可能不会得到任何信息。代码存在延迟,因为状态需要一段时间才能变为可用。

"""Send a single GTalk message to myself"""

import xmpp
import time

_SERVER = 'talk.google.com', 5223
USERNAME = '[email protected]'
PASSWORD = 'whatever'


def sendMessage(tojid, text, username=USERNAME, password=PASSWORD):
    jid = xmpp.protocol.JID(username)
    client = xmpp.Client(jid.getDomain(), debug=[])
    #self.client.RegisterHandler('message', self.message_cb)
    if not client:
      print 'Connection failed!'
      return
    con = client.connect(server=_SERVER)
    print 'connected with', con
    auth = client.auth(jid.getNode(), password, 'botty')
    if not auth:
      print 'Authentication failed!'
      return
    client.RegisterHandler('message', message_cb)
    roster = client.getRoster()
    client.sendInitPresence()
    if '/' in tojid:
        tail = tojid.split('/')[-1]
        t = time.time() + 1
        while time.time() < t:
            client.Process(1)
            time.sleep(0.1)
            if [ res for res in roster.getResources(tojid) if res.startswith(tail) ]:
                break
        for res in roster.getResources(tojid):
            if res.startswith(tail):
                tojid = tojid.split('/', 1)[0] + '/' + res

    print "sending to", tojid
    id = client.send(xmpp.protocol.Message(tojid, text))
    t = time.time() + 1
    while time.time() < t:
        client.Process(1)
        time.sleep(0.1)
    print "status", roster.getStatus(tojid)
    print "show", roster.getShow(tojid)
    print "resources", roster.getResources(tojid)
    client.disconnect()

def message_cb(session, message):
    print ">", message

sendMessage(USERNAME + '/Talk', "This is an automatically generated gtalk message: did you get it?")

I don't have anything to hand with xmpp installed, but here's some old code I had lying around that might help you. You'll want to update the USERNAME/PASSWORD to your own values for test purposes.

Things to note: users logged in to Google Talk get a random presence string on their userid: that doesn't matter if you are trying to get the status of some other user, but if you want to write some code so want to communicate with yourself you need to distinguish the user logged in from GMail or a GTalk client from the test program. Hence the code searches through the userids.

Also, if you read the status immediately after logging in you probably won't get anything. There's a delay in the code because it takes a little while for the status to become available.

"""Send a single GTalk message to myself"""

import xmpp
import time

_SERVER = 'talk.google.com', 5223
USERNAME = '[email protected]'
PASSWORD = 'whatever'


def sendMessage(tojid, text, username=USERNAME, password=PASSWORD):
    jid = xmpp.protocol.JID(username)
    client = xmpp.Client(jid.getDomain(), debug=[])
    #self.client.RegisterHandler('message', self.message_cb)
    if not client:
      print 'Connection failed!'
      return
    con = client.connect(server=_SERVER)
    print 'connected with', con
    auth = client.auth(jid.getNode(), password, 'botty')
    if not auth:
      print 'Authentication failed!'
      return
    client.RegisterHandler('message', message_cb)
    roster = client.getRoster()
    client.sendInitPresence()
    if '/' in tojid:
        tail = tojid.split('/')[-1]
        t = time.time() + 1
        while time.time() < t:
            client.Process(1)
            time.sleep(0.1)
            if [ res for res in roster.getResources(tojid) if res.startswith(tail) ]:
                break
        for res in roster.getResources(tojid):
            if res.startswith(tail):
                tojid = tojid.split('/', 1)[0] + '/' + res

    print "sending to", tojid
    id = client.send(xmpp.protocol.Message(tojid, text))
    t = time.time() + 1
    while time.time() < t:
        client.Process(1)
        time.sleep(0.1)
    print "status", roster.getStatus(tojid)
    print "show", roster.getShow(tojid)
    print "resources", roster.getResources(tojid)
    client.disconnect()

def message_cb(session, message):
    print ">", message

sendMessage(USERNAME + '/Talk', "This is an automatically generated gtalk message: did you get it?")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文