1

Решил себе сделать авто обновления баннера с информацией для Вконтакте на Python, но к сожалению, что-то идет не так.

Код

import random
import time
import vk_api
import os
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont

vk_session = vk_api.VkApi(token='мой токен')
vk = vk_session.get_api()

def generate_banner():
    width = 911
    height = 1920

    image = Image.new('RGB', (width, height))
    color1 = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    color2 = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    draw = ImageDraw.Draw(image)
    for y in range(height):
        ratio = y / (height - 1)
        r = int(color1[0] * (1 - ratio) + color2[0] * ratio)
        g = int(color1[1] * (1 - ratio) + color2[1] * ratio)
        b = int(color1[2] * (1 - ratio) + color2[2] * ratio)
        draw.line((0, y, width, y), fill=(r, g, b))

    now = datetime.now()
    day = now.strftime('%d')
    month = now.strftime('%B')
    weekday = now.strftime('%A')

    script_dir = os.path.dirname(__file__)
    font_path = os.path.join(script_dir, 'font.otf')
    font = ImageFont.truetype(font_path, 40)

    date_text = f"{day} {month}, {weekday}."
    time_text = now.strftime('%H:%M:%S')
    draw.text((860, 0), date_text, font=font, fill=(255, 255, 255))
    draw.text((900, 50), time_text, font=font, fill=(255, 255, 255))
    draw.text((-0, 0), '@blinchikvk', font=font, fill=(255, 255, 255))

    image.save('background.png')
    return image

last_time = None
while True:
    if datetime.time(datetime.now()) != last_time:
        image = generate_banner()
        upload_url = vk.photos.getOwnerCoverPhotoUploadServer(group_id='214830809')['upload_url']
        with open('background.png', 'rb') as f:
            response = vk_api.vk_api.VkUpload().photo(cover_file=f, upload_url=upload_url)
        photo = response[0]['id']
        vk.photos.saveOwnerCoverPhoto(hash=response[0]['hash'], photo_id=response[0]['id'])

        print(f"Updated profile cover photo with photo_id {photo} at {datetime.now()}")
        last_time = None
        
        while True:
            now = datetime.now()
            if datetime.time(now) != datetime.time(datetime.now()):
                if last_time is None:
                    last_time = datetime.time(now)
                else:
                    if last_time == datetime.time(now):
                        continue  # Если время не изменилось, пропускаем итерацию
                    else:
                        last_time = datetime.time(now)

                image = generate_banner()
                upload_url = vk.photos.getOwnerCoverPhotoUploadServer(group_id='123456789')['upload_url']
                with open('background.png', 'rb') as f:
                    response = vk_api.VkUpload().photo(cover_file=f, upload_url=upload_url)
                photo = response[0]['id']
                vk.photos.saveOwnerCoverPhoto(hash=response[0]['hash'], photo_id=response[0]['id'])

                print(f"Updated profile cover photo with photo_id {photo} at {datetime.now()}")

                time.sleep(60)  # Ждем 1 минуту перед следующей итерацией.

при запуске в терминал пишет это: vk_api.exceptions.ApiError: [100] One of the parameters specified was missing or invalid: min width of crop is 911

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.