Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

monkeys, action stars, better at-message processing, less sleep

  • Loading branch information...
commit 8870278f49a2a8fab6418968ee855484680ecc7c 1 parent 1a0059e
Jan Schaumann authored

Showing 2 changed files with 104 additions and 21 deletions. Show diff stats Hide diff stats

  1. +8 1 CHANGES
  2. +96 20 src/jbot.py
9 CHANGES
... ... @@ -1,3 +1,10 @@
1 1
2   -Version 0.1 (2011-03-19):
  2 +2011-03-21:
  3 + * monkeys
  4 + * Facts about Mr. T, Vin Diesel, Chuck Norris and of course your mom
  5 + * greetings and goodbyes
  6 + * consider all mentions AtMessages
  7 + * don't sleep on every single api failure
  8 +
  9 +2011-03-19:
3 10 * Initial version published
116 src/jbot.py
@@ -48,7 +48,7 @@
48 48 }
49 49
50 50 NEW = [
51   - "I now also have #tigerblood, know some Crypto and various factlets."
  51 + "I know all about Mr.T, Vin Diesel and Chuck Norris. And of course your mom."
52 52 ]
53 53
54 54 ###
@@ -102,6 +102,21 @@ def cmd_feature(msg):
102 102 return "@%s Feature request relayed to my owner. Thank you!" % msg.user.screen_name
103 103
104 104
  105 +def cmd_factlet(msg, url):
  106 + """Get a factlet about a certain personality."""
  107 +
  108 + pattern = re.compile('.*<summary>(?P<fact>.*)</summary>', re.I)
  109 + try:
  110 + for line in urllib2.urlopen(url).readlines():
  111 + match = pattern.match(line)
  112 + if match:
  113 + return match.group('fact')
  114 +
  115 + sys.stderr.write("Tried to get a fact from %s but found nothing." % url)
  116 + except urllib2.URLError, e:
  117 + sys.stderr.write("Unable to get %s\n\t%s\n" % (url, e))
  118 +
  119 +
105 120 def cmd_help(msg):
106 121 """Return a helpful message."""
107 122
@@ -237,6 +252,22 @@ def cmd_tool(msg):
237 252 return "You're a tool, @%s." % tool
238 253
239 254
  255 +def cmd_yourmom(msg, url):
  256 + """Generate a yo-momma joke."""
  257 +
  258 + url = "%s/ym%02d.html" % (url, random.randint(1,28))
  259 + pattern = re.compile('(?P<yomomma>.*)<br><br>', re.I)
  260 + try:
  261 + for line in urllib2.urlopen(url).readlines():
  262 + match = pattern.match(line)
  263 + if match:
  264 + return match.group('yomomma')
  265 +
  266 + sys.stderr.write("Tried to get a yo momma joke from %s but found nothing." % url)
  267 + except urllib2.URLError, e:
  268 + sys.stderr.write("Unable to get %s\n\t%s\n" % (url, e))
  269 +
  270 +
240 271 ###
241 272 ### Classes
242 273 ###
@@ -380,6 +411,26 @@ def getHelp(self):
380 411 "worldcup" : time.mktime(time.strptime("2014-06-13 00:00:00", "%Y-%m-%d %H:%M:%S"))
381 412 }
382 413
  414 +# If we have a new follower, pick one of these. %user will be replaced
  415 +# with the username.
  416 +GREETINGS = [
  417 + "Hello %user! I look forward to brightening your day!",
  418 + "I sincerely welcome %user to the list of jbotters.",
  419 + "Yo yo yo, ma homie %user in da house!",
  420 + "Look at that, %user found me! Hooray!",
  421 + "Good day, %user. I hope you will find my services to your liking."
  422 + ]
  423 +
  424 +# If we stop following somebody, pick one of these. %user will be replaced
  425 +# with the username.
  426 +GOODBYES = [
  427 + "Awww. I'm sad to see you leave, %user. Farewell!",
  428 + "Ooops, I guess I shouldn't have said that about %user.",
  429 + "Smell ya later, %user. (I still can't believe 'Smell ya' later' replaced 'Goodbye'...)",
  430 + "Goodbye, %user. It was nice following you.",
  431 + "It's a sad day - we've lost %user. Oh well, more jbot for the rest of you."
  432 + ]
  433 +
383 434 ##
384 435 ## Regex trigger fall into a number of categories:
385 436 ##
@@ -555,7 +606,16 @@ def getHelp(self):
555 606 "I really like the vest."
556 607 ],
557 608 # Vikings
558   - re.compile("viking", re.I) : "Spam, lovely Spam, wonderful Spam."
  609 + re.compile("viking", re.I) : "Spam, lovely Spam, wonderful Spam.",
  610 + # Monkeys
  611 + re.compile("(monkey|orangutan|gorilla|macaque|chimp|\bape\blemur|simian|primate)", re.I) : [
  612 + "Bababooey bababooey bababooey!",
  613 + "Fafa Fooey.",
  614 + "Mama Monkey.",
  615 + "Fla Fla Flo Fly.",
  616 + "Fafa Fooey.",
  617 + "FaFa Fo Hi."
  618 + ]
559 619 }
560 620
561 621 # Map a regex to a URL function - URL tuple
@@ -567,7 +627,15 @@ def getHelp(self):
567 627 re.compile(".*(trivia|factual|factlet)", re.I) :
568 628 ( cmd_trivia, "http://www.nicefacts.com/quickfacts/index.php" ),
569 629 re.compile("(shakespear|hamlet|macbeth|romeo and juliet|merchant of venice|midsummer nicht's dream|henry V|as you like it|All's Well That Ends Well|Comedy of Errors|Cymbeline|Love's Labours Lost|Measure for Measure|Merry Wives of Windsor|Much Ado About Nothing|Pericles|Prince of Tyre|Taming of the Shrew|Tempest|Troilus|Cressida|Twelfth Night|two gentleman of verona|Winter's tale|henry IV|king john|richard II|antony and cleopatra|coriolanus|julius caesar|kind lear|othello|timon of athens|titus|andronicus)", re.I) :
570   - ( cmd_shakespear, "http://www.pangloss.com/seidel/Shaker/index.html" )
  630 + ( cmd_shakespear, "http://www.pangloss.com/seidel/Shaker/index.html" ),
  631 + re.compile("(chuck|norris|walker|texas ranger|karate)", re.I) :
  632 + ( cmd_factlet, "http://4q.cc/index.php?pid=atom&person=chuck" ),
  633 + re.compile("(a-?team|mr(\.? )?t|hannibal|murdock|Baracus)", re.I) :
  634 + ( cmd_factlet, "http://4q.cc/index.php?pid=atom&person=mrt" ),
  635 + re.compile("(\bvin\b|diesel|fast and (the )?furious|riddick)", re.I) :
  636 + ( cmd_factlet, "http://4q.cc/index.php?pid=atom&person=vin" ),
  637 + re.compile("(ur([ _])mom|yourmom|m[oa]mma|[^ ]+'s mom)", re.I) :
  638 + ( cmd_yourmom, "http://www.ahajokes.com" )
571 639 }
572 640
573 641 ###
@@ -682,7 +750,7 @@ def getList(self, what, user):
682 750
683 751 wanted.sort()
684 752 except tweepy.error.TweepError, e:
685   - self.handleTweepError(e, "Unable to get list of % for %s" % (what, user))
  753 + self.handleTweepError(e, "Unable to get list of %s for %s" % (what, user))
686 754
687 755 return wanted
688 756
@@ -747,7 +815,6 @@ def handleTweepError(self, tweeperr, info):
747 815 errmsg = ""
748 816
749 817 rate_limit = self.api.rate_limit_status()
750   - diff = rate_limit["reset_time_in_seconds"] - time.time()
751 818
752 819 if tweeperr and tweeperr.response.status:
753 820 if tweeperr.response.status == TWITTER_RESPONSE_STATUS["FailWhale"]:
@@ -756,8 +823,18 @@ def handleTweepError(self, tweeperr, info):
756 823 errmsg = "Twitter is busted again: %s\n" % time.asctime()
757 824 elif tweeperr.response.status == TWITTER_RESPONSE_STATUS["RateLimited"]:
758 825 errmsg = "Fully rate limited until %s.\n" % rate_limit["reset_time"]
  826 + diff = rate_limit["reset_time_in_seconds"] - time.time()
  827 + sys.stderr.write("Hits left: %d\n" % rate_limit["remaining_hists"])
  828 + if diff > 3500:
  829 + sys.stderr.write("%d\n%s\n" % (diff,str(rate_limit)))
  830 + sys.exit(EXIT_ERROR)
759 831 elif tweeperr.response.status == TWITTER_RESPONSE_STATUS["SearchRateLimited"]:
760 832 errmsg = "SearchRate limited until %s.\n" % rate_limit["reset_time"]
  833 + diff = rate_limit["reset_time_in_seconds"] - time.time()
  834 + sys.stderr.write("Hits left: %d\n" % rate_limit["remaining_hists"])
  835 + if diff > 3500:
  836 + sys.stderr.write("%d\n%s\n" % (diff,str(rate_limit)))
  837 + sys.exit(EXIT_ERROR)
761 838 else:
762 839 errmsg = "On %s Twitter told me:\n'%s'\n" % (time.asctime(), tweeperr)
763 840
@@ -776,12 +853,14 @@ def followOrUnfollow(self, action, users):
776 853 self.verbose("Now %sing %s...", u)
777 854 try:
778 855 if action == "follow":
  856 + reply = GREETINGS[random.randint(0,len(GREETINGS)-1)]
  857 + reply = re.sub(r'%user', u, reply)
  858 + self.tweet(reply)
779 859 self.api.create_friendship(screen_name=u)
780   - #self.tweet("@%s Hello! I look forward to brightening your day!" % u)
781   - sys.stderr.write("@%s Hello! I look forward to brightening your day!" % u)
782 860 elif action == "unfollow":
783   - #self.tweet("@%s Awwww. Goodbye..." % u)
784   - sys.stderr.write("@%s Awwww. Goodbye..." % u)
  861 + reply = GOODBYES[random.randint(0,len(GOODBYES)-1)]
  862 + reply = re.sub(r'%user', u, reply)
  863 + self.tweet(reply)
785 864 self.api.destroy_friendship(screen_name=u)
786 865 else:
787 866 sys.stderr.write("Illegal action for 'followOrUnfollow': %s\n" % action)
@@ -880,17 +959,14 @@ def processAtMessages(self):
880 959 results = self.api.mentions(since_id=self.lastmessage)
881 960 for msg in results:
882 961 if not self.processMessage(msg):
883   - pattern = re.compile('^@%s .*' % self.getOpt("user"))
884   - match = pattern.match(msg.text)
885   - if match:
886   - # XXX: this needs to go into a function somewhere else
887   - # instead of being crammed in here
888   - ip = re.compile("(damm?n? you|shut ?up|die|(cram|stuff) it|piss ?off|(fuck|screw|hate) you|stupid|you (stink|blow)|go to hell|stfu|idiot|(you are|is) annoying|down boy)", re.I)
889   - m = ip.match(msg.text)
890   - if m:
891   - self.tweet(cmd_insult("!insult %s" % msg.user.screen_name))
892   - else:
893   - self.tweet("@%s %s" % (msg.user.screen_name,
  962 + # XXX: this needs to go into a function somewhere else
  963 + # instead of being crammed in here
  964 + ip = re.compile("(damm?n? you|shut ?up|die|(cram|stuff) it|piss ?off|(fuck|screw|hate) you|stupid|you (stink|blow)|go to hell|stfu|idiot|(you are|is) annoying|down boy)", re.I)
  965 + m = ip.match(msg.text)
  966 + if m:
  967 + self.tweet(cmd_insult("!insult %s" % msg.user.screen_name))
  968 + else:
  969 + self.tweet("@%s %s" % (msg.user.screen_name,
894 970 MISC_RESPONSES[random.randint(0,len(MISC_RESPONSES)-1)]))
895 971 except tweepy.error.TweepError, e:
896 972 self.handleTweepError(e, "API mentions error for myself.")

0 comments on commit 8870278

Please sign in to comment.
Something went wrong with that request. Please try again.