Advertisement
shubsbhatt

graffittiBot

Oct 21st, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.75 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*- #
  3.  
  4. from sys import argv
  5. from PIL import Image
  6. import cv2
  7. import StringIO
  8.  
  9. from newsGraffiti_1 import comic_element
  10. from twitterbot import TwitterBot
  11. from picbot import PicBot
  12. import keys
  13.  
  14.  
  15.  
  16. class graffitiBot2(PicBot, TwitterBot):
  17.     """
  18.     Whenever you tweet a photo at this bot, it will tweet back at you with an image
  19.     with the faces flipped
  20.     """
  21.     def bot_init(self):
  22.         """ Initialize and configure the bot """
  23.  
  24.         ############################
  25.         # SETUP  FACE DETECTION    #
  26.         ############################
  27.         classifier = "haarcascade_frontalface_default.xml"
  28.         self.faceCascade = cv2.CascadeClassifier(classifier)
  29.  
  30.         ############################
  31.         # REQUIRED: LOGIN DETAILS! #
  32.         ############################
  33.         self.config['api_key'] = keys.consumer_key 
  34.         self.config['api_secret'] = keys.consumer_secret
  35.         self.config['access_key'] = keys.access_token
  36.         self.config['access_secret'] = keys.access_token_secret
  37.  
  38.  
  39.         ######################################
  40.         # SEMI-OPTIONAL: OTHER CONFIG STUFF! #
  41.         ######################################
  42.  
  43.         # how often to tweet, in seconds
  44.         self.config['tweet_interval'] = 1 * 60     # default: 1 minutes
  45.  
  46.         # use this to define a (min, max) random range of how often to tweet
  47.         # e.g., self.config['tweet_interval_range'] = (5*60, 10*60) # tweets every 5-10 minutes
  48.         self.config['tweet_interval_range'] = None
  49.  
  50.         # only reply to tweets that specifically mention the bot
  51.         self.config['reply_direct_mention_only'] = True
  52.  
  53.         # the bot will listen to everything on the timeline
  54.         self.config['ignore_timeline_mentions'] = False
  55.  
  56.         # only include bot followers (and original tweeter) in @-replies
  57.         self.config['reply_followers_only'] = False
  58.  
  59.         # fav any tweets that mention this bot?
  60.         self.config['autofav_mentions'] = False
  61.  
  62.         # fav any tweets containing these keywords?
  63.         self.config['autofav_keywords'] = []
  64.  
  65.         # follow back all followers?
  66.         self.config['autofollow'] = False
  67.  
  68.  
  69.  
  70.     def on_scheduled_tweet(self):
  71.         """ Make a public tweet to the bot's own timeline. """
  72.         # We might take senteces from somewhere and tweet them on a regular basis ...
  73.         pass # don't do anything here ...
  74.  
  75.  
  76.     def on_timeline(self, tweet, prefix):
  77.         """ Actions to take on a timeline tweet. """
  78.  
  79.         #pass # Don't do anything here ...
  80.         print "I am watching the timeline"
  81.  
  82.         #watch timeline and grab images form martins bot
  83.         image = self.get_image(tweet)
  84.  
  85.         if image is not None:
  86.             print "1"
  87.  
  88.             text = "{} This is this what you really think ?.".format(prefix)
  89.  
  90.             image, rectangles = comic_element(image)
  91.            
  92.             num = len(rectangles)
  93.             if num == 1:
  94.                 comment = "Now I know why did clicked this picture".format(prefix)
  95.             elif num > 1:
  96.                 comment = "{} You look like a character from a comic strip  ".format(prefix)
  97.             else:
  98.                 print "2"
  99.                 return
  100.             print comment
  101.                
  102.             # filename and format for uplaoding
  103.             filename, format = "result.jpg", "JPEG"
  104.  
  105.             image.show()
  106.  
  107.             # write image to a StringIO file as JPG
  108.             file = StringIO.StringIO()
  109.             image.save(file, format= format)
  110.  
  111.             self.post_tweet(text[:140], reply_to=tweet, media=image, file=file)
  112.        
  113.  
  114.     def on_mention_without_image(self, tweet, prefix):
  115.         """
  116.         React to text-only mentions
  117.         """
  118.         text = "{} Come on, give me a picture and I'll tell you what I think about it".format(prefix)
  119.         try:
  120.             self.post_tweet(text[:140], reply_to=tweet)
  121.         except Exception as e:
  122.             print(e)
  123.  
  124.  
  125.  
  126.     def process_image(self, image, prefix=None, fn=comic_element):
  127.         """
  128.         Process the image, place the speech bubble
  129.         """
  130.  
  131.         image, rectangles = fn(image)
  132.  
  133.         # return new images with comment
  134.         num = len(rectangles)
  135.         if num == 1:
  136.             comment = "{} Cool pic :) Do you have one with more faces in it?".format(prefix)
  137.         elif num > 1:
  138.             comment = "{} your photo says something more! ".format(prefix)
  139.         else:
  140.             comment = "Nice pic. But I couldn't spot any faces :("
  141.         return image, comment
  142.  
  143.  
  144. if __name__ == '__main__':
  145.  
  146.     bot = graffitiBot2()
  147.    
  148.     if len(argv) == 1:
  149.         # just run the bot
  150.         print("Running the bot.")
  151.         bot.run()
  152.         print bot.state['recent_timeline']
  153.  
  154.     elif argv[1] == "test":
  155.         # various tests
  156.         if(len(argv) == 2):
  157.             print "USAGE: graffiti Bot.py test [image|mention]"
  158.         else:
  159.             test = argv[2]
  160.             if test == "image":
  161.                 print("Testing image ")
  162.                 img = Image.open("test2.jpg")
  163.                 img, comment = bot.process_image(img, "@test", fn=comic_element )
  164.                 img.show()
  165.                 print("COMMENT: {}".format(comment))
  166.             elif test == "mention":
  167.                 print("Testing on_mention hook of the bot")
  168.                 api = bot.api
  169.                 tweet = api.get_status("655719153102401537")
  170.                 prefix = bot.get_mention_prefix(tweet)
  171.                 bot.on_mention(tweet, prefix)
  172.             else:
  173.                 print("Unknown test: {}".format(test))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement