From 46859f798cab28ff082b6e1c4ca53110ca839e4b Mon Sep 17 00:00:00 2001 From: Mikhail Guseletov Date: Thu, 13 Jul 2023 16:44:53 +0700 Subject: [PATCH] export to OK --- README.md | 3 ++- bot.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e1749e9..ce8f18e 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ pip install webuiapi pip install translate pip install transformers pip install vk_api +pip install ok_api cd C:\stable-diffusion-webui\extensions\sd_telegram\ python bot.py `````` @@ -77,7 +78,7 @@ Please pay attention to the file size limit in Telegram API: _**Chat History** We go into any Telegram chat with prompts (individual messages), click three dots in the upper right corner, upload only text messages in json format. We get the result.json file, which we throw into the bot and select "Chat History". We get a random prompt, which we can save in data -If you chose _/img_real = true_, the document will be unloaded for you, and after it the social network **VK** upload button. +If you chose _/img_real = true_, the document will be unloaded for you, and after it the social network **VK** and **OK** upload button. Before that, you need to set up a token and enter the ID of the album where the photo will be sent. drawing diff --git a/bot.py b/bot.py index 91f3f83..ee691e6 100644 --- a/bot.py +++ b/bot.py @@ -32,7 +32,8 @@ import base64 from pathlib import Path import logging import vk_api -from vk_api import VkUpload +from vk_api import VkUpload #https://github.com/python273/vk_api +from ok_api import OkApi, Upload # https://github.com/needkirem/ok_api # Настройка логгера logging.basicConfig(format="[%(asctime)s] %(levelname)s : %(name)s : %(message)s", @@ -49,8 +50,27 @@ API_BOT_TOKEN = "TOKEN_HERE" # photos - зона доступа. # После перехода и подтверждения выцепляем access_token из адресной строки # TODO auto requests -VK_TOKEN = 'TOKEN_HERE' -vk_album_id = '789' # брать с адресной строки, когда открываешь ВК. Пример https://vk.com/album123_789 +# OK https://ok.ru/vitrine/myuploaded +# Добавить приложение - https://ok.ru/app/setup +# дбавить платформу - OAUTH +# VALUABLE_ACCESS = Обязательно +# PHOTO_CONTENT = Обязательно +# Ссылка на страницу = https://apiok.ru/oauth_callback +# Список разрешённых redirect_uri = https://apiok.ru/oauth_callback +# сохранить, перезайти +# Ищем ID приложения справа от "Основные настройки приложения" - ID 123 +# Открываем в браузере https://connect.ok.ru/oauth/authorize?client_id=123&scope=PHOTO_CONTENT;VALUABLE_ACCESS&response_type=token&redirect_uri=https://apiok.ru/oauth_callback +# С адресной строки копируем token в access_token ниже +# application_key = Публичный ключ справа от "Основные настройки приложения" +# Вечный access_token - Получить новый +# application_secret_key = Session_secret_key +VK_TOKEN = 'VK_TOKEN_HERE' +API_BOT_TOKEN = 'API_BOT_TOKEN_HERE' +VK_ALBUM_ID = 'VK_ALBUM_ID' # брать с адресной строки, когда открываешь ВК. Пример https://vk.com/album123_789 +OK_ACCESS_TOKEN = 'OK_ACCESS_TOKEN_HERE' +OK_APPLICATION_KEY = 'OK_APPLICATION_KEY_HERE' +OK_APPLICATION_SECRET_KEY = 'OK_APPLICATION_SECRET_KEY_HERE' +OK_GROUP_ID = 'OK_GROUP_ID_HERE' bot = Bot(token=API_BOT_TOKEN) storage = MemoryStorage() @@ -516,9 +536,15 @@ async def show_thumbs(chat_id, res): chat_id=chat_id, media=pilToImages(res, "tg") ) if dataParams["img_real"] == "true" or dataParams["img_real"] == "True": - await bot.send_media_group( + mes_file = await bot.send_media_group( chat_id=chat_id, media=pilToImages(res, "real") ) + await bot.send_message( + chat_id=chat_id, + text="⬇ send to VK and OK ⬇", + reply_markup=InlineKeyboardMarkup( + inline_keyboard=[[InlineKeyboardButton(mes_file[0].document.file_id, callback_data='send_vk')]]) + ) # -------- COMMANDS ---------- @@ -756,7 +782,7 @@ async def inl_gen(message: Union[types.Message, types.CallbackQuery]) -> None: # TODO long message await bot.send_message( chat_id=chatId, - text="↓ send to VK", + text="⬇ send to VK and OK ⬇", reply_markup=InlineKeyboardMarkup( inline_keyboard=[[InlineKeyboardButton(mes_file[0].document.file_id, callback_data='send_vk')]]) ) @@ -785,10 +811,12 @@ async def inl_gen(message: Union[types.Message, types.CallbackQuery]) -> None: await getKeyboardUnion("Turn on SD"+sd, message, keyboard) # upload in VK +# TODO actual prompt @dp.callback_query_handler(text="send_vk") -async def inl_samplers(callback: types.CallbackQuery) -> None: +async def send_vk(callback: types.CallbackQuery) -> None: try: - global VK_TOKEN, vk_album_id + # Export VK + global VK_TOKEN, VK_ALBUM_ID, OK_ACCESS_TOKEN, OK_APPLICATION_KEY, OK_APPLICATION_SECRET_KEY, OK_GROUP_ID file_id = callback.message.reply_markup.inline_keyboard[0][0].text #TODO file_obj = await bot.get_file(file_id) vk_session = vk_api.VkApi(token=VK_TOKEN) @@ -800,13 +828,26 @@ async def inl_samplers(callback: types.CallbackQuery) -> None: file.write(requests.get(file_url).content) vk_upload.photo( photos='temp.png', - album_id=vk_album_id, - caption=get_prompt_settings(0) + album_id=VK_ALBUM_ID, + caption=data['prompt'] #TODO actual from ID message ) + # Export OK + ok = OkApi( + access_token=OK_ACCESS_TOKEN, + application_key=OK_APPLICATION_KEY, + application_secret_key=OK_APPLICATION_SECRET_KEY) + group_id = OK_GROUP_ID + upload = Upload(ok) + upload_response = upload.photo(photos=['temp.png'], album=group_id) + for photo_id in upload_response['photos']: + token = upload_response['photos'][photo_id]['token'] + response = ok.photosV2.commit(photo_id=photo_id, token=token, comment=data['prompt']) + print(response.text) + # clear garbage os.remove('temp.png') await callback.message.edit_text( - 'Фотка в VK загружена' + 'Фотка в VK и OK загружена' ) except Exception as e: await bot.send_message(