Monday, March 8, 2021

Example(3) to user list

 #!/usr/bin/env python


import dbus

import os

from dbus.mainloop.glib import DBusGMainLoop


class CheckedObject:

    def __init__(self, obj):

        self.obj = obj


    def __getattr__(self, attr):

        return CheckedAttribute(self, attr)


class CheckedAttribute:

    def __init__(self, cobj, attr):

        self.cobj = cobj

        self.attr = attr


    def __call__(self, *args):

        result = self.cobj.obj.__getattr__(self.attr)(*args)

        if result == 0:

            raise Exception("Error: %s %s returned %s" %

                            (self.attr, args, result))

        return result


dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

bus = dbus.SessionBus()

obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")

purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")

f=open('bblist.txt','w')

for a in purple.PurpleAccountsGetAllActive():

    protocolName = purple.PurpleAccountGetProtocolName(a)

    account = a

    name = purple.PurpleAccountGetUsername(account)

    # cmd="echo 1 '{}|{}' ".format(protocolName.encode('utf-8'),name.encode('utf-8'))

    #os.system(cmd)

    f.write("1 %s|%s \n" % (protocolName.encode('utf-8'),name.encode('utf-8')))

    for online in purple.PurpleFindBuddies( account, '' ):

       name_buddy=purple.PurpleBuddyGetAlias( online ).encode('utf-8')

       buddy=purple.PurpleBuddyGetName( online )

       #cmd="echo 2 '{}|{}' ".format(name_buddy,buddy.encode('utf-8'))

       #os.system(cmd)

       f.write("2 %s|%s \n" % (name_buddy,buddy.encode('utf-8')))

f.close