Advertisement
jalla2000

Untitled

Sep 2nd, 2011
49,991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import dbus, gobject
  5. from dbus.mainloop.glib import DBusGMainLoop
  6. from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
  7. from cleverbot import CleverbotSession
  8. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  9. bus = dbus.SessionBus()
  10. obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
  11. purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
  12.  
  13. conversations = {}
  14.  
  15. def htmlClean(pidginHtml):
  16.     soup = BeautifulSoup(pidginHtml)
  17.     pidginHtml = soup.findAll('font')[-1].contents
  18.     return str(BeautifulStoneSoup(pidginHtml[0], convertEntities=BeautifulStoneSoup.ALL_ENTITIES))
  19.  
  20. def my_func(account, who, message, conversation, flags):
  21.     global purple
  22.     direction = "Incoming"
  23.     if flags != 2:
  24.         direction = "Outgoing"
  25.     print "Account/ConvId(%s/%s): %s message - %s said: \"%s\"" % (account, conversation, direction, who, message)
  26.     username = purple.PurpleAccountGetUsername(account)
  27.     if flags == 2 and username == "myusername@hotmail.com":
  28.         if conversation not in conversations.keys():
  29.             conversations[conversation] = CleverbotSession()
  30.             purple.PurpleConvImSend(purple.PurpleConvIm(conversation), u'Hi! I\'m Amy! PĆ„l is not around right now...')
  31.         else:
  32.             question = htmlClean(message)
  33.             response = conversations[conversation].Ask(question)
  34.             print 'Asked: "%s" Response: "%s"' % (question, response)
  35.             purple.PurpleConvImSend(purple.PurpleConvIm(conversation), response)
  36.  
  37. bus.add_signal_receiver(my_func,
  38.                         dbus_interface="im.pidgin.purple.PurpleInterface",
  39.                         signal_name="DisplayedImMsg")
  40.  
  41. loop = gobject.MainLoop()
  42. loop.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement