diff --git a/CHANGELOG.md b/CHANGELOG.md index 960ef442a2454c7bf1c194dcd9dc9c5c18a7d729..e900e86271331b046dc8d86256431a47e3321a17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ **Ajout:** - script bot.py quasi complet - quizz marche +- le bot fait des memes !! --- diff --git a/bot.py b/bot.py index 4689c571f741472ffe7a7568d43a1c04280ce184..cfc582d08061d1763c35f6a55bf23d4178c98b9f 100755 --- a/bot.py +++ b/bot.py @@ -38,7 +38,7 @@ class Bot(Client): !useless \n \ !chuck \n\ !nordpress \n \ - !quizz [start|new|reset|scoreboard|indice|round|question] ... not yet implemented\n \ + !quizz [start|new|reset|scoreboard|indice|round|question] \n \ !q <reponse> pour répondre au quizz\n\ !haddock \n \ !philo \n \ @@ -84,11 +84,21 @@ class Bot(Client): return random.choice(Bot.what_liste) elif '!memes ' in texte: texte = texte.replace('!memes ', '').split(' ; ') - useless.memes(texte) - self.sendLocalImage('meme.png', thread_id=thread_id, thread_type=thread_type) + if len(texte) == 3: + image, text1, text2 = texte + elif len(text) == 2: + image, text1 = texte + text2 = '' + try: + meme = useless.Memes(image, text1, text2) + meme.process() + self.sendLocalImage(meme.get_result(), thread_id=thread_id, thread_type=thread_type) + except: + self.send(Message(text='Ya un petit problème dans la plantation...'), thread_id=thread_id, thread_type=thread_type) + elif any(word.lower() in texte.lower() for word in ["let\'s go", "c'est parti", "go ", "allons "]): return 'é zé bardi !' - elif any(word.lower() in texte.lower() for word in ['noice ', 'nice ', 'ok']): + elif any(word.lower() in texte.lower() for word in ['noice ', 'nice ']): self.send(Message(text="👍", emoji_size=EmojiSize.LARGE), thread_id=thread_id, thread_type=thread_type) # self.send(Message(emoji_size=EmojiSize.LARGE), thread_id=thread_id, thread_type=thread_type) elif '!kohlanta' in texte: diff --git a/impact.ttf b/impact.ttf new file mode 100644 index 0000000000000000000000000000000000000000..114e6c1968b663086409144e50e7a592bff1d6c2 Binary files /dev/null and b/impact.ttf differ diff --git a/requirements.txt b/requirements.txt index 7a3fb236f05b38f8d1dacdf722176951ea463fe2..fe18c53d6b03910edfcfe6f1ffc56b7c139af2c2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ requests fbchat wikipedia -googletrans \ No newline at end of file +googletrans +textwrap3 +pillow \ No newline at end of file diff --git a/sample_out.jpg b/sample_out.jpg new file mode 100644 index 0000000000000000000000000000000000000000..427ddd56b43091d5c7b9fa7e7ad3eb2512f64e26 Binary files /dev/null and b/sample_out.jpg differ diff --git a/useless/__init__.py b/useless/__init__.py index f9df4fc916d4abbcfe9cb4db7ec2b63a68fe4fff..c87179628d79daad8a6e757173bc88025ca63e9e 100644 --- a/useless/__init__.py +++ b/useless/__init__.py @@ -8,4 +8,5 @@ from .philotron import genererSujet from .pipotron import pipo from .haddock import haddock from .quizz import Quizz -from .kohlanta import * \ No newline at end of file +from .kohlanta import * +from .memes import Memes \ No newline at end of file diff --git a/useless/impact.ttf b/useless/impact.ttf new file mode 100644 index 0000000000000000000000000000000000000000..114e6c1968b663086409144e50e7a592bff1d6c2 Binary files /dev/null and b/useless/impact.ttf differ diff --git a/useless/memes.py b/useless/memes.py old mode 100644 new mode 100755 index eda359858faddea1159d46c4bf1c590618f58a6a..1a3ddf13f42c0485a3bf6bcf0fd9f359f2f5420c --- a/useless/memes.py +++ b/useless/memes.py @@ -5,45 +5,53 @@ import PIL from PIL import ImageFont from PIL import Image from PIL import ImageDraw +import urllib +import urllib.request +import textwrap3 class Memes(object): images = { - "10guy": ["10guy.jpg"], - "euh": ["images/euh.jpg"], - "gangbang": ["gangbang.jpg"], - "killyou": ["killyou.jpg"], - "maths": ["maths.jpg"], - "mdr": ["mdr.jpg"], - "ohlala": ["ohlala.jpg"], - "ohlala2": ["ohlala2.jpg"], - "salt": ["salt.jpg"], - "smart": ["smart.jpg"], - "speechless": ["speechless.jpg"], - "suicide": ["suicide.jpg"], - "surprised": ["suprised.jpg"], - "goodchoice": ["goodchoice.jpg"], - "nanithefuck": ["nanithefuck.jpg"], - "badsurprise": ["badsurprise.jpg"], - "boyfriend": ["boyfriend.jpg"], - "thisisfine": ["thisisfine.jpg"], - "thisisnotfine": ["thisisnotfine.jpg"], - "depressing": ["depressing.jpg"], - "avengers": ["avengers.jpg"], - "godblessing": ["godblessings.jpg"], - "homer": ["homer.jpg"], - "thanos": ["thanos.jpg"], - "trash": ["trash.jpg"], - "whatisthis": ["whatisthis.png"] + "10guy": "10guy.jpg", + "euh": "images/euh.jpg", + "gangbang": "gangbang.jpg", + "killyou": "killyou.jpg", + "maths": "maths.jpg", + "mdr": "mdr.jpg", + "ohlala": "ohlala.jpg", + "ohlala2": "ohlala2.jpg", + "salt": "salt.jpg", + "smart": "smart.jpg", + "speechless": "speechless.jpg", + "suicide": "suicide.jpg", + "surprised": "suprised.jpg", + "goodchoice": "goodchoice.jpg", + "nanithefuck": "nanithefuck.jpg", + "badsurprise": "badsurprise.jpg", + "boyfriend": "boyfriend.jpg", + "thisisfine": "thisisfine.jpg", + "thisisnotfine": "thisisnotfine.jpg", + "depressing": "depressing.jpg", + "avengers": "avengers.jpg", + "godblessing": "godblessings.jpg", + "homer": "homer.jpg", + "thanos": "thanos.jpg", + "trash": "trash.jpg", + "whatisthis": "whatisthis.png" } - def __init__(self, name, text1, text2='', rep='images/memes_images/'): - self.name = name + def __init__(self, image, text1, text2='', rep='images/memes_images/', out='sample_out.jpg'): + if image.startswith("http://") or image.startswith("https://"): + urllib.request.urlretrieve(image, "sample_in.jpg") + self.fichier = "sample_in.jpg" + else: + assert image in Memes.images, "Can\'t open image..." + self.fichier = rep + Memes.images[image] self.text1 = text1 self.text2 = text2 + self.out = out - @staticmethod - def ecrire(draw, text, font, pos): + def ecrire(self, draw, text, font, pos): x, y = pos for adj in range(3): # move right @@ -66,9 +74,36 @@ class Memes(object): # create normal text on image draw.text(pos, text, fill="white", font=font) - def get_image(self): - pass + def process(self, textsize=30): + # lettres_par_ligne = int(40 / textsize * 30) + img = Image.open(self.fichier) # on ouvre l'image + img = img.resize((500, int(img.size[1] * 500 / img.size[0]))) # on la redimensionne propotionnellement pour quelle soit de largeur 500 + draw = ImageDraw.Draw(img) # pour pouvoir modifier l'image + print(img.size) + W, H = img.size + font = ImageFont.truetype("impact.ttf", textsize) + para1 = textwrap3.wrap(self.text1, width=30) # le dessin du texte 1 + current_h1, pad = 10, 10 + para2 = textwrap3.wrap(self.text2, width=30) # le dessin du texte 2 + w, h = draw.textsize(self.text2, font=font) # just to get the next line + current_h2, pad = H - 10 - len(para2) * h, 10 + + # là on ecrit pour de vrai + for line in para1: + w, h = draw.textsize(line, font=font) + self.ecrire(draw, line, font, ((W - w) / 2, current_h1)) + current_h1 += h + pad + for line in para2: + w, h = draw.textsize(line, font=font) + self.ecrire(draw, line, font, ((W - w) / 2, current_h2)) + current_h2 += h + pad + + img.save(self.out) # bon ben on enregistre + + def get_result(self): + return self.out + if __name__ == '__main__': - meme1 = Memes(smart, 'test1', 'test2') - img = meme1.get_image() + meme1 = Memes('smart', 'test1 lol !', 'test2 mdr ?') + file = meme1.process()