| 1 |  |  | #!/usr/bin/python | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # -*- coding: utf-8 -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from modules.utils import Caption_Filter, get_param, get_image, send_image, mp4_fix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from modules.logging import logging_decorator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from telegram.ext import PrefixHandler, MessageHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from telegram.ext.dispatcher import run_async | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from telegram import ChatAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from datetime import datetime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from wand.image import Image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from itertools import chain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | coords_by_frame = ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | [(58, 28), (164, 24), (168, 106), (63, 114)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | [(59, 28), (165, 24), (169, 106), (64, 114)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | [(60, 28), (166, 24), (170, 106), (65, 114)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | [(60, 28), (166, 24), (170, 106), (65, 114)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | [(60, 27), (166, 23), (170, 105), (65, 113)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | [(61, 26), (167, 22), (171, 104), (66, 112)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | [(59, 25), (163, 21), (171, 100), (67, 107)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | [(57, 26), (155, 23), (171, 93), (68, 99)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | [(57, 26), (154, 26), (169, 87), (66, 88)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | [(55, 30), (149, 33), (165, 78), (67, 73)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | [(49, 40), (144, 52), (168, 76), (66, 63)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | [(57, 51), (143, 66), (162, 76), (67, 58)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | [(63, 51), (138, 67), (163, 75), (72, 55)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | [(63, 40), (150, 58), (168, 72), (68, 51)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | [(59, 36), (152, 54), (170, 73), (69, 52)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | [(57, 43), (148, 51), (170, 75), (72, 66)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | [(48, 47), (141, 59), (166, 83), (68, 69)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | [(37, 46), (133, 49), (140, 82), (36, 77)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | [(40, 40), (133, 50), (137, 79), (32, 65)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | [(43, 36), (137, 45), (144, 74), (40, 62)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | [(46, 32), (139, 42), (150, 70), (47, 58)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | [(45, 32), (141, 40), (157, 70), (51, 59)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | [(43, 32), (137, 41), (154, 71), (48, 58)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | def module_init(gd): | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     global path, launchpad_gif | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     path = gd.config["path"] | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     launchpad_gif = gd.config["launchpad_path"] | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     commands = gd.config["commands"] | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     for command in commands: | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         caption_filter = Caption_Filter("/"+command) | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         gd.dp.add_handler(MessageHandler(caption_filter, fap)) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         gd.dp.add_handler(PrefixHandler("/", command, fap)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | @run_async | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | @logging_decorator("fap") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | def fap(update, context): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     filename = datetime.now().strftime("%d%m%y-%H%M%S%f") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         extension = get_image(update, context, path, filename) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     except: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         update.message.reply_text("I can't get the image! :(") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     with Image(filename=path+filename+extension) as decal: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         decal.resize(320, 172) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         w, h = decal.size | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         decal.virtual_pixel = 'transparent' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         source_points = ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             (0, 0), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             (w, 0), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             (w, h), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             (0, h) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         with Image(filename=launchpad_gif) as template_gif: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             new = Image() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             for i in range(len(template_gif.sequence)): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 with template_gif.sequence[i] as frame:  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     img = Image(image=frame) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     img.delay = 6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 destination_points = (coords_by_frame[i]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 order = chain.from_iterable(zip(source_points, destination_points)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 arguments = list(chain.from_iterable(order)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 decal_current = Image(image=decal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 decal_current.matte_color = "rgba(255, 255, 255, 0)" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 decal_current.distort('perspective', arguments) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 img.composite(decal_current,left=0,top=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 new.sequence.append(img) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 decal_current.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 img.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             new.save(filename=path+"result.mp4") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             result_filename = mp4_fix(path, "result") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             send_image(update, path, result_filename, ".mp4") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             new.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             os.remove(path+result_filename+".mp4") | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |             os.remove(path+filename+extension) | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |