Passed
Push — master ( 22931c...acbb17 )
by Anas
02:01
created

modules.quote   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 116
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 22

9 Functions

Rating   Name   Duplication   Size   Complexity  
A fit_text() 0 12 3
B get_message() 0 25 6
A quote() 0 14 3
A circle() 0 10 1
A module_init() 0 10 2
A get_profile_pic() 0 8 2
A img_to_bytes() 0 5 1
A generate_profile_pic() 0 13 3
A make_quote() 0 9 1
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from modules.logging import logging_decorator
4
from telegram.ext.dispatcher import run_async
5
from telegram import ChatAction
6
from telegram.ext import MessageHandler, PrefixHandler
7
from modules.utils import Caption_Filter, send_image
8
from PIL import Image, ImageDraw, ImageOps
9
from wand.image import Image as wandImage
10
from wand.drawing import Drawing
11
from wand.color import Color
12
from wand.font import Font
13
from datetime import datetime
14
import random
15
import io
16
import os
17
18
19
def module_init(gd):
20
    global path, resources_path, font_path
21
    path = gd.config["path"]
22
    resources_path = gd.config["resources_path"]
23
    font_path = gd.config["font"]
24
    commands = gd.config["commands"]
25
    for command in commands:
26
        caption_filter = Caption_Filter("/"+command)
27
        gd.dp.add_handler(MessageHandler(caption_filter, quote))
28
        gd.dp.add_handler(PrefixHandler("/", commands, quote))
29
30
31
@run_async
32
@logging_decorator("quote")
33
def quote(update, context):
34
    filename = datetime.now().strftime("%d%m%y-%H%M%S%f")
35
    id, name, text = get_message(update, context)
36
    if text == "" or text == None:
37
        update.message.reply_text("Type in some text!")
38
        return
39
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
40
    profile_pic = get_profile_pic(context, filename, id, name)
41
    make_quote(profile_pic, filename, text, name)
42
    send_image(update, path, filename, ".jpg")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable path does not seem to be defined.
Loading history...
43
    os.remove(path+filename+".jpg")
44
    os.remove(path+filename+"pfp.jpg")
45
46
47
def make_quote(image, filename, text, author):
48
    background = Image.open(resources_path+"bg.jpg")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable resources_path does not seem to be defined.
Loading history...
49
    avatar = Image.open(image)
50
    avatar_circle = circle(avatar)
51
    background.paste(avatar_circle, (50, 110), avatar_circle)
52
    watermark = Image.open(resources_path+"watermark.png").convert("RGBA")
53
    background.paste(watermark, (20, 540), watermark)
54
    binary_image = img_to_bytes(background, ".jpg")
55
    fit_text(binary_image, filename, text, author)
56
57
58
def get_message(update, context):
59
    reply = update.message.reply_to_message
60
    if reply is None:
61
        id = update.message.from_user.id
62
        name = update.message.from_user.full_name
63
        if update.message.caption is not None:
64
            text = update.message.caption[3:]
65
        else:
66
            text = " ".join(context.args)
67
    else:
68
        if reply.forward_from is None:
69
            id = reply.from_user.id
70
            name = reply.from_user.full_name
71
            if len(reply.photo) < 1:
72
                text = reply.text
73
            else:
74
                text = reply.caption
75
        else:
76
            id = reply.forward_from.id
77
            name = reply.forward_from.full_name
78
            if len(reply.photo) < 1:
79
                text = reply.text
80
            else:
81
                text = reply.caption
82
    return id, name, text
83
84
85
def get_profile_pic(context, filename, id, name):
86
    pfp_path = path+filename+"pfp.jpg"
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable path does not seem to be defined.
Loading history...
87
    if len(context.bot.getUserProfilePhotos(id, limit = 1).photos) == 0:
88
        generate_profile_pic(pfp_path, name)
89
    else:
90
        pfp = context.bot.getUserProfilePhotos(id, limit = 1).photos[0][-1].file_id
91
        context.bot.getFile(pfp).download(pfp_path)
92
    return pfp_path
93
94
95
def generate_profile_pic(save_path, name):
96
    words = name.split()
97
    letters = [word[0] for word in words]
98
    initials = "".join(letters)
99
    colors =["#c75650", "#d67a27", "#7e6ccf", "#4eb331", "#2ea4ca"]
100
    with wandImage(width = 756, height = 756, background = Color(random.choice(colors))) as img:
101
        left, top, width, height = 200, 265, 340, 240
102
        with Drawing() as context:
103
            font = Font(font_path, color="white")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable font_path does not seem to be defined.
Loading history...
104
            context(img)
105
            img.caption(initials, left=left, top=top, width=width, height=height, font=font, gravity="center")
106
            img.save(filename=save_path)
107
    return save_path
108
109
110
def circle(image):
111
    mask = Image.open(resources_path+"mask.png").convert("L")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable resources_path does not seem to be defined.
Loading history...
112
    circle = ImageOps.fit(image, mask.size, centering=(0.5, 0.5))
113
    circle.putalpha(mask)
114
    stroke = Image.new("RGBA", (756,756), (255,255,255,0))
115
    draw = ImageDraw.Draw(stroke)
116
    draw.ellipse((0, 0, 755, 755), width=18, outline ="white")
117
    stroke = stroke.resize((378, 378), resample=Image.ANTIALIAS)
118
    circle.paste(stroke, (0,0), stroke)
119
    return circle
120
121
122
def img_to_bytes(file, ext):
123
	fp = io.BytesIO()
124
	format = Image.registered_extensions()[ext]
125
	file.save(fp, format)
126
	return fp.getvalue()
127
128
129
def fit_text(img, filename, text, name):
130
    text = "«" + text + "»"
131
    name = "—  " + name
132
    with wandImage(blob=img) as canvas:
133
        text_left, text_top, text_width, text_height = 475, 50, 675, 410
134
        name_left, name_top, name_width, name_height = 530, 460, 570, 50
135
        with Drawing() as context:
136
            font = Font(font_path, color="white")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable font_path does not seem to be defined.
Loading history...
137
            context(canvas)
138
            canvas.caption(text, left=text_left, top=text_top, width=text_width, height=text_height, font=font, gravity="center")
139
            canvas.caption(name, left=name_left, top=name_top, width=name_width, height=name_height, font=font, gravity="center")
140
            canvas.save(filename=path+filename+".jpg")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable path does not seem to be defined.
Loading history...
141