|  |   | 
| Line 394: | Line 394: | 
|  |      return text |  |      return text | 
|  | </source> |  | </source> | 
|  | 
 |  | 
|  | === Source Code: ===
 |  | 
|  | 
 |  | 
|  | ==== Mainbot ====
 |  | 
|  | <pre style="font-size:smaller" >
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | #mainbot.py
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | #Import Classes + Modules
 |  | 
|  | 
 |  | 
|  | from twitterbot import TwitterBot
 |  | 
|  | import keys
 |  | 
|  | import random
 |  | 
|  | from io import BytesIO
 |  | 
|  | from PIL import Image
 |  | 
|  | from image import get_image_file
 |  | 
|  | 
 |  | 
|  | #Import aus Dateien
 |  | 
|  | from textposts import make_text
 |  | 
|  | from textposts import no_image
 |  | 
|  | from imagefuncs import filter
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | ############################
 |  | 
|  | ### Twitterbot functions ###
 |  | 
|  | ############################
 |  | 
|  | 
 |  | 
|  | class MyTwitterBot(TwitterBot):
 |  | 
|  |     
 |  | 
|  |     # Keys
 |  | 
|  |     def bot_init(self):
 |  | 
|  |         """ Initialize and configure the bot """
 |  | 
|  | 
 |  | 
|  |         ############################
 |  | 
|  |         # REQUIRED: LOGIN DETAILS! #
 |  | 
|  |         ############################
 |  | 
|  |         self.config['api_key'] = keys.consumer_key
 |  | 
|  |         self.config['api_secret'] = keys.consumer_secret
 |  | 
|  |         self.config['access_key'] = keys.access_token
 |  | 
|  |         self.config['access_secret'] = keys.access_token_secret
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  |         ######################################
 |  | 
|  |         # SEMI-OPTIONAL: OTHER CONFIG STUFF! #
 |  | 
|  |         ######################################
 |  | 
|  | 
 |  | 
|  |         # how often to tweet, in seconds
 |  | 
|  |         self.config['tweet_interval'] = 1 * 5     # default: 1 minutes
 |  | 
|  | 
 |  | 
|  |         # use this to define a (min, max) random range of how often to tweet
 |  | 
|  |         # e.g., self.config['tweet_interval_range'] = (5*60, 10*60) # tweets every 5-10 minutes
 |  | 
|  |         self.config['tweet_interval_range'] = None
 |  | 
|  | 
 |  | 
|  |         # only reply to tweets that specifically mention the bot
 |  | 
|  |         self.config['reply_direct_mention_only'] = True
 |  | 
|  | 
 |  | 
|  |         # only include bot followers (and original tweeter) in @-replies
 |  | 
|  |         self.config['reply_followers_only'] = False
 |  | 
|  | 
 |  | 
|  |         # fav any tweets that mention this bot?
 |  | 
|  |         self.config['autofav_mentions'] = False
 |  | 
|  | 
 |  | 
|  |         # fav any tweets containing these keywords?
 |  | 
|  |         self.config['autofav_keywords'] = []
 |  | 
|  | 
 |  | 
|  |         # follow back all followers?
 |  | 
|  |         self.config['autofollow'] = False
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  |     def on_scheduled_tweet(self):
 |  | 
|  |         """ Make a public tweet to the bot's own timeline. """
 |  | 
|  |         # We might take senteces from somewhere and tweet them on a regular basis ...
 |  | 
|  |         pass # don't do anything here ...
 |  | 
|  | 
 |  | 
|  |     def on_mention(self, tweet, prefix):
 |  | 
|  |         if tweet:
 |  | 
|  |             # get image from tweet
 |  | 
|  |             try:
 |  | 
|  |                 image_file = get_image_file(tweet)
 |  | 
|  | 
 |  | 
|  |                 if image_file is None:
 |  | 
|  |                     text = ("{},"+no_image()).format(prefix)
 |  | 
|  |                 else:
 |  | 
|  |                     # create a tweet and make sure to cut it off at 140 chars
 |  | 
|  |                     text = ("{},"+make_text()).format(prefix)
 |  | 
|  | 
 |  | 
|  |             except Exception as e:
 |  | 
|  |                     print(e)
 |  | 
|  | 
 |  | 
|  |             # do the tweeting based on wether we have an image
 |  | 
|  |             tweetsize = 140 - len(prefix) - 1
 |  | 
|  |             text = text[:140]
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  |             if image_file is None:
 |  | 
|  | 
 |  | 
|  |                     print("No Image")
 |  | 
|  |                     self.post_tweet(text, reply_to=tweet)
 |  | 
|  |                     return
 |  | 
|  | 
 |  | 
|  |             else:
 |  | 
|  |                 filename, file = image_file
 |  | 
|  |                 img = Image.open(file)
 |  | 
|  |                 img = filter(img)
 |  | 
|  |                 format = filename.split(".", 1)[1].upper()
 |  | 
|  |                 print("Image format: {}".format(format))
 |  | 
|  |                 output = BytesIO()
 |  | 
|  |                 img.save(output, format="PNG")
 |  | 
|  |                 
 |  | 
|  |             try:
 |  | 
|  |                 # Post
 |  | 
|  |                 
 |  | 
|  |                 self.post_tweet(text, reply_to=tweet, media=filename, file=output)
 |  | 
|  | 
 |  | 
|  |                 output.close()
 |  | 
|  | 
 |  | 
|  |             except Exception as e:
 |  | 
|  |                 # did anything go wrong when we tried to create and post the tweet?
 |  | 
|  |                 print(e)
 |  | 
|  |     
 |  | 
|  |     def on_timeline(self, tweet, prefix):
 |  | 
|  |         """ Actions to take on a timeline tweet. """
 |  | 
|  |         pass # Don't do anything here ...
 |  | 
|  | 
 |  | 
|  | if __name__ == '__main__':
 |  | 
|  |     bot = MyTwitterBot()
 |  | 
|  |     bot.run()
 |  | 
|  | 
 |  | 
|  | </pre>
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | ==== Bildfunktionen + Random Auswahl ====
 |  | 
|  | <pre style="font-size:smaller" >
 |  | 
|  | 
 |  | 
|  | imagefuncs.py
 |  | 
|  | 
 |  | 
|  | from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageColor, ImageEnhance
 |  | 
|  | import random
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | #verschiedene Bildbearbeitungfunktionen
 |  | 
|  | 
 |  | 
|  | def blur (image):
 |  | 
|  |     return image.filter(ImageFilter.BLUR)
 |  | 
|  | 
 |  | 
|  | def emboss (image):
 |  | 
|  |     return image.filter(ImageFilter.EMBOSS)
 |  | 
|  | 
 |  | 
|  | def edges (image):
 |  | 
|  |     return image.filter(ImageFilter.FIND_EDGES)
 |  | 
|  | 
 |  | 
|  | def transpose(image):
 |  | 
|  |     return image.transpose(Image.FLIP_LEFT_RIGHT)
 |  | 
|  | 
 |  | 
|  | def invert(image):
 |  | 
|  |     return image.point(lambda x: 255-x)
 |  | 
|  | 
 |  | 
|  | def pixelit (image):
 |  | 
|  |     xsize, ysize = image.size
 |  | 
|  |     width = xsize
 |  | 
|  |     height = ysize
 |  | 
|  |     minwidth = int(xsize * 0.05)
 |  | 
|  |     minheight = int(ysize * 0.05)
 |  | 
|  |     image = image.resize((minwidth, minheight) , Image.NEAREST)
 |  | 
|  |     image = image.resize((width , height), Image.NEAREST)
 |  | 
|  |     return image
 |  | 
|  | 
 |  | 
|  | #Random Filter List
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | def filter (image):
 |  | 
|  | 
 |  | 
|  |     functions_list = [blur, emboss, edges, invert, transpose, pixelit]
 |  | 
|  |     filename = "final.png"
 |  | 
|  |     
 |  | 
|  |     x = 1
 |  | 
|  |     y = 2
 |  | 
|  |     z = 3
 |  | 
|  |     list = [x, y, z]
 |  | 
|  |     anzahl = random.choice(list)
 |  | 
|  |     print (anzahl)
 |  | 
|  |     
 |  | 
|  |     if anzahl == 1:
 |  | 
|  |         #Random eine Funktion
 |  | 
|  |         imagefunc = random.choice(functions_list)
 |  | 
|  |         image = imagefunc(image)
 |  | 
|  |         
 |  | 
|  |     if anzahl == 2:
 |  | 
|  |         #Random eine Funktion
 |  | 
|  |         imagefunc = random.choice(functions_list)
 |  | 
|  |         image = imagefunc(image)
 |  | 
|  |         #2.Runde Random Filter anwenden
 |  | 
|  |         imagefunc = random.choice(functions_list)
 |  | 
|  |         image = imagefunc(image)
 |  | 
|  |         
 |  | 
|  |     if anzahl == 3:
 |  | 
|  |         #Random eine Funktion
 |  | 
|  |         imagefunc = random.choice(functions_list)
 |  | 
|  |         image = imagefunc(image)
 |  | 
|  |         #2.Runde Random Filter anwenden
 |  | 
|  |         imagefunc = random.choice(functions_list)
 |  | 
|  |         image = imagefunc(image)
 |  | 
|  |         #3.Runde Random Filter anwenden
 |  | 
|  |         imagefunc = random.choice(functions_list)
 |  | 
|  |         image = imagefunc(image)
 |  | 
|  |     
 |  | 
|  |     return image
 |  | 
|  | 
 |  | 
|  | </pre>
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | ==== Textfunktion ====
 |  | 
|  | <pre style="font-size:smaller" >
 |  | 
|  | 
 |  | 
|  | textposts.py
 |  | 
|  | 
 |  | 
|  | #Import Modules for Project
 |  | 
|  | import random
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | def make_text():
 |  | 
|  |     a="Yeah, a new Image is Ready"
 |  | 
|  |     b="What do you think about?"
 |  | 
|  |     c="Uh, which Effekt"
 |  | 
|  |     d="Nice"
 |  | 
|  |     e="Thats what I do"
 |  | 
|  |     f="Mhmmm, what you mean?"
 |  | 
|  |     g="My next I post!"
 |  | 
|  |     h="I should do more of this."
 |  | 
|  |     i="Twitter is my life."
 |  | 
|  |     j="This is my job!"
 |  | 
|  |     k="Funky"
 |  | 
|  | 
 |  | 
|  |     text_list = [a, b, c, d, e, f, g, h, i, j, k]
 |  | 
|  |     
 |  | 
|  |     text = random.choice(text_list)
 |  | 
|  |     
 |  | 
|  |     return text
 |  | 
|  | 
 |  | 
|  | def no_image():
 |  | 
|  |     a = "No Image there"
 |  | 
|  |     b = "What you do? Where is my Image?"
 |  | 
|  |     c = "I can't read! Send me a picture"
 |  | 
|  |     d = "I like Images!"
 |  | 
|  |     e = "Boring!"
 |  | 
|  |     f = "Okay, I don't know what you want!"
 |  | 
|  |     g = "Pictures! Thats what I want!"
 |  | 
|  |     noimagelist = [a, b, c, d, e, f, g]
 |  | 
|  |     
 |  | 
|  |     text = random.choice(noimagelist)
 |  | 
|  |     
 |  | 
|  |     return text
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | </pre>
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | === Bilder des Bots: ===
 |  | 
|  | 
 |  | 
|  | [[File:Bot01.JPG]]
 |  | 
|  | 
 |  | 
|  | [[File:Bot02.JPG]]
 |  | 
|  | 
 |  | 
|  | [[File:Bot03.JPG]]
 |  | 
|  | 
 |  | 
|  | [[File:Bot04.JPG]]
 |  | 
|  | 
 |  | 
|  | [[File:Bot05.JPG]]
 |  | 
|  | 
 |  | 
|  | [[File:Bot06.JPG]]
 |  | 
|  | 
 |  | 
|  | 
 |  | 
|  | === Thanks for Help ===
 |  | 
|  | 
 |  | 
|  | Thanks to [http://stackoverflow.com/ Stackoverflow], thanks to the smaller Blogs and Forums I arrieved via Google, thanks to [http://effbot.org/ effbot], thanks to [https://pillow.readthedocs.org// Pillow] and last but not least thanks to Martin and Sebastian.
 |  |