Commits

Joseph Tate committed 13b5655

Add a module for emergencycompliment.com

  • Participants
  • Parent commits 402c25e
  • Branches emergencycompliment

Comments (0)

Files changed (2)

pmxbot/commands.py

 				'%s has' % complimentee, compliment)
 		return compliment
 
+@command('emergencycompliment', aliases=('ec','emercomp'),
+	doc="Return a random compliment from http://emergencycompliment.com/")
+def emer_comp(client, event, channel, nick, rest):
+	if hasattr(phrases, 'emer_comp'):
+		comps = phrases.emer_comp
+	else:
+		comps = util.emergency_complement()
+		if comps is None:
+			return "Sorry %s, I can't do that right now." % nick
+		comps = [x[u'phrase'] for x in json.loads(comps)]
+		#Cache the results
+		phrases.emer_comp = comps
+	compliment = random.choice(comps)
+	if rest:
+		complimentee = rest.strip()
+		karma.Karma.store.change(complimentee, 1)
+		return "%s: %s" % (complimentee, compliment)
+	return compliment
+
 @command("gettowork", aliases=("gtw",), doc="You really ought to, ya know...")
 def gettowork(client, event, channel, nick, rest):
 	suggestions = [u"Um, might I suggest working now",
 def_exp1 = re.compile(r"<div><span class=f>.*?</span>(.+?)</div>", re.MULTILINE)
 def_exp2 = re.compile(r"Definition for.*<div class=s><div>(.+?)<", re.MULTILINE)
 urbd_exp = re.compile(r"""<td class=['"]word['"]>(.+?)^</td>$(?:.+?)<div class=['"]definition['"]>(.+?)</div>""", re.MULTILINE | re.DOTALL)
+ecomp_exp = re.compile(r"""\[.*\]""", re.MULTILINE|re.DOTALL)
 
 def strip_tags(string):
 	"""
 
 	return all
 
+def emergency_complement():
+	compurl = 'http://emergencycompliment.com/js/compliments.js'
+	comps = open_url(compurl).read()
+	match = ecomp_exp.search(comps)
+	if not match:
+		return None
+	complist = match.group()
+	return complist
+	
+
 def passagg(recipient='', sender=''):
 	adj = random.choice(pmxbot.phrases.adjs)
 	if random.randint(0,1):
 	end = "%s." % end
 	final = " ".join([start, end])
 	return final
+
+if __name__ == '__main__':
+	print emergency_complement()