Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: jalla2000 on Sep 2nd, 2011  |  syntax: Python  |  size: 1.77 KB  |  views: 46,432  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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()