Created
May 11, 2012 01:42
-
-
Save anroots/2656981 to your computer and use it in GitHub Desktop.
This program chooses a random line from solvangud.txt and sends it as a reply to Pidgin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# This program chooses a random line from solvangud.txt and sends it as a reply to Pidgin | |
# Author: Ando Roots | |
from random import choice | |
import difflib | |
import sys | |
import dbus, gobject | |
from dbus.mainloop.glib import DBusGMainLoop | |
import re | |
# Generates and returns a new insult. If baseline is defined, some semantic searching will occur. | |
def insult_me(baseline=None): | |
file = open('solvangud.txt','r') | |
lines = file.readlines() | |
insult = [] | |
if (baseline): | |
insult = difflib.get_close_matches(baseline, lines, cutoff=0.4) | |
if not insult: | |
return choice(lines) | |
else: | |
return '->' + choice(insult) | |
file.close() | |
# This happens when Pidgin gets a (new) instant message | |
def new_incoming_message(account, sender, message, conversation, flags): | |
clean_message = StripHTML(message.encode('utf-8')) | |
msg = insult_me(clean_message) | |
purple.PurpleConvImSend(purple.PurpleConvIm(conversation), msg) | |
print sender + ' said something.' | |
# Strip out any HTML tags found in input string | |
def StripHTML(data): | |
data = str(data) | |
p = re.compile(r'<.*?>') | |
return p.sub('', data).replace('n','') | |
# Set up the dbus listener and looping | |
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') | |
bus.add_signal_receiver(new_incoming_message, | |
dbus_interface="im.pidgin.purple.PurpleInterface", | |
signal_name="ReceivedImMsg") | |
loop = gobject.MainLoop() | |
loop.run() | |
# -- End of file -- # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment