<file_sep>from PIL import ImageFont, ImageDraw


def make_great_cover(image, str):
    font_size = 30
    font = ImageFont.truetype("/home/KrozeRoll/mysite/wrift.ttf", font_size)
    text_size = font.getsize(str)

    draw = ImageDraw.Draw(image)

    h = image.size[1]
    w = image.size[0]

    draw.text(((w - text_size[0]) / 2, h - text_size[1] - 10), str,
              font=font, fill="white")
    return image
<file_sep>from PIL import Image, ImageDraw, ImageFont, ImageEnhance


def is_vowel(ch):
    return ch in ('А', 'О', 'У', 'Э', 'Ы', 'Я', 'Ё', 'Ю', 'Е', 'И')


def make_result_string(str):
    str = str.upper()
    if str.find('-ХУ') == -1 and str.find('- ХУ') == -1:
        pk = 0
        while pk < len(str) and is_vowel(str[pk]) == 0:
            pk = pk + 1

        if pk == len(str):
            pk = 0

        if str[pk:][0] == "Е" or str[pk:][0] == "Э":
            str = str + " - ХУЕ" + str[pk + 1:]
        elif str[pk:][0] == "У" or str[pk:][0] == "Ю":
            str = str + " - ХУЮ" + str[pk + 1:]
        elif str[pk:][0] == "А" or str[pk:][0] == "Я":
            str = str + " - ХУЯ" + str[pk + 1:]
        elif str[pk:][0] == "И" or str[pk:][0] == "Ы":
            str = str + " - ХУИ" + str[pk + 1:]
        elif str[pk:][0] == "О" or str[pk:][0] == "Ё":
            str = str + " - ХУЁ" + str[pk + 1:]
        else:
            str = str + " - ХУЕ" + str[pk:]

    return str


def make_normal_size(image):
    w = image.size[0]
    h = image.size[1]

    kf = 1024 / w
    if kf < 768 / h:
        kf = 768 / h

    image = image.resize((int(w * kf), int(h * kf)))

    w = image.size[0]
    h = image.size[1]

    if h > 768:
        area = (0, int((h - 768) / 2), w, int((h - 768) / 2) + 768)
        image = image.crop(area)
    elif w > 1024:
        area = (int((w - 1024) / 2), 0, int((w - 1024) / 2) + 1024, h)
        image = image.crop(area)
    return image


def bright(source):
    enhancer = ImageEnhance.Brightness(source)

    factor = 0.5
    result = enhancer.enhance(factor)

    return result


def make_result_image(image, str):
    w = image.size[0]
    h = image.size[1]

    if w * h > 1024 * 768:
        image = make_normal_size(image)
        image = bright(image)
    else:
        image = bright(image)
        image = make_normal_size(image)

    h = 768
    w = 1024

    draw = ImageDraw.Draw(image)
    str = make_result_string(str)

    font_size = 72
    font = ImageFont.truetype("/home/KrozeRoll/mysite/wrift.ttf", font_size)
    text_size = font.getsize(str)

    while text_size[0] > w - 10:
        font_size = font_size - 1
        font = ImageFont.truetype("/home/KrozeRoll/mysite/wrift.ttf", font_size)
        text_size = font.getsize(str)

    draw.text(((w - text_size[0]) / 2, (h - text_size[1]) / 2 - 30), str, font=font, fill="white")
    return image
<file_sep>from PIL import Image, ImageDraw, ImageFont
from flask import Flask, request, json
import requests
import vk
from settings import confirmation_token, token, user_token, admin_list
from urllib.request import urlopen
import wordMaking
import coverMaking

app = Flask(__name__)
GROUP_ID = 146177467


@app.route('/', methods=['POST'])
def processing():
    data = json.loads(request.data)
    if 'type' not in data.keys():
        return 'not vk'
    if data['type'] == 'confirmation':
        return confirmation_token
    elif data['type'] == 'message_new':
        session = vk.Session()
        api = vk.API(session, v=5.68)
        user_id = data['object']['user_id']
        if api.groups.isMember(group_id=GROUP_ID, user_id=user_id, access_token=token) == 0 and user_id != 75614210:
            api.messages.send(access_token=token, user_id=user_id,
                              message="Сервис доступен только подписчикам сообщества")
            return 'ok'

        elif data['object']['body'] == '&&&' and (user_id in admin_list):
            url = api.photos.getWallUploadServer(access_token=user_token, group_id=GROUP_ID)
            url = url['upload_url']
            files = {'photo': open("Adm_great.jpg", 'rb')}
            res = requests.post(url, files=files).json();

            server = res['server']
            photo = res['photo']
            res_hash = res['hash']
            save_wall_photo_result = api.photos.saveWallPhoto(access_token=user_token, group_id=GROUP_ID, server=server,
                                                              photo=photo, hash=res_hash)
            attach = "photo" + str(save_wall_photo_result[0]['owner_id']) + "_" + str(save_wall_photo_result[0]['id'])
            api.wall.post(access_token=user_token, owner_id=-GROUP_ID, from_group=1, attachment=attach)
            api.messages.send(access_token=token, user_id=user_id, message='Very nice!')
            return 'ok'

        elif ('attachments' in data['object'].keys() and 'photo' in data['object']['attachments'][0].keys() and
              data['object']['body'] != ""):
            word = data['object']['body']

            if 'photo_2560' in data['object']['attachments'][0]['photo'].keys():
                url = data['object']['attachments'][0]['photo']['photo_2560']
            elif 'photo_1280' in data['object']['attachments'][0]['photo'].keys():
                url = data['object']['attachments'][0]['photo']['photo_1280']
            elif 'photo_807' in data['object']['attachments'][0]['photo'].keys():
                url = data['object']['attachments'][0]['photo']['photo_807']
            elif 'photo_604' in data['object']['attachments'][0]['photo'].keys():
                url = data['object']['attachments'][0]['photo']['photo_604']
            else:
                url = data['object']['attachments'][0]['photo']['photo_130']

            url = url.replace("\/", '/')

            image = Image.open(urlopen(url))
            image = wordMaking.makeGreateImage(image, word)

            url = api.photos.getMessagesUploadServer(access_token=token, peer_id=user_id)
            url = url['upload_url']

            if user_id in admin_list:
                image.save("Adm_great.jpg")
                files = {'photo': open("Adm_great.jpg", 'rb')}
            else:
                image.save("great.jpg")
                files = {'photo': open("great.jpg", 'rb')}

            res = requests.post(url, files=files).json()

            server = res['server']
            photo = res['photo']
            res_hash = res['hash']
            save_wall_photo_result = api.photos.saveMessagesPhoto(access_token=token, server=server, photo=photo,
                                                                  hash=res_hash)
            attach = "photo" + str(save_wall_photo_result[0]['owner_id']) + "_" + str(save_wall_photo_result[0]['id'])
            api.messages.send(access_token=token, user_id=user_id, message="Вот, держи", attachment=attach)
            api.messages.send(access_token=token, user_id=user_id,
                              message='Можешь поделиться с друзьями или предложить новостью в паблик! ;-)')
            return 'ok'

        else:
            user_name = api.users.get(access_token=token, user_ids=user_id)
            name = user_name[0]['first_name']
            api.messages.send(access_token=token, user_id=user_id,
                              message='Привет - хуивет, ' + name + '!\nКак насчёт картинки?')
            api.messages.send(access_token=token, user_id=user_id, message='Отправь мне любое фото и слово!')
            return 'ok'
    elif data['type'] == 'group_join':
        session = vk.Session()
        api = vk.API(session, v=5.68)
        user_id = data['object']['user_id']
        user_name = api.users.get(access_token=token, user_ids=user_id)
        name = user_name[0]['first_name'] + ' ' + user_name[0]['last_name']
        word = name + ', <NAME>'
        image = Image.open('coverTemplate.jpg')
        image = coverMaking.makeGreatCover(image, word)
        image.save("cover.jpg")

        url = api.photos.getOwnerCoverPhotoUploadServer(access_token=token, group_id=GROUP_ID, crop_x2=1590,
                                                        crop_y2=400)
        url = url['upload_url']
        files = {'photo': open("cover.jpg", 'rb')}
        res = requests.post(url, files=files).json()
        photo = res['photo']
        res_hash = res['hash']

        api.photos.saveOwnerCoverPhoto(access_token=token, hash=res_hash, photo=photo)

        return 'ok'
<file_sep># vk-public-bot
Source of message-bot in vk.com public.
