export to OK

This commit is contained in:
Mikhail Guseletov 2023-07-13 16:44:53 +07:00
parent 09cffdbac9
commit 46859f798c
2 changed files with 53 additions and 11 deletions

View File

@ -11,6 +11,7 @@ pip install webuiapi
pip install translate pip install translate
pip install transformers pip install transformers
pip install vk_api pip install vk_api
pip install ok_api
cd C:\stable-diffusion-webui\extensions\sd_telegram\ cd C:\stable-diffusion-webui\extensions\sd_telegram\
python bot.py python bot.py
`````` ``````
@ -77,7 +78,7 @@ Please pay attention to the file size limit in Telegram API:
_**Chat History** _**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 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. Before that, you need to set up a token and enter the ID of the album where the photo will be sent.
<img src="https://raw.githubusercontent.com/partyfind/sd_bot/master/trash/photo_2023-06-22_15-29-24.jpg" alt="drawing" width="400"/> <img src="https://raw.githubusercontent.com/partyfind/sd_bot/master/trash/photo_2023-06-22_15-29-24.jpg" alt="drawing" width="400"/>

61
bot.py
View File

@ -32,7 +32,8 @@ import base64
from pathlib import Path from pathlib import Path
import logging import logging
import vk_api 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", logging.basicConfig(format="[%(asctime)s] %(levelname)s : %(name)s : %(message)s",
@ -49,8 +50,27 @@ API_BOT_TOKEN = "TOKEN_HERE"
# photos - зона доступа. # photos - зона доступа.
# После перехода и подтверждения выцепляем access_token из адресной строки # После перехода и подтверждения выцепляем access_token из адресной строки
# TODO auto requests # TODO auto requests
VK_TOKEN = 'TOKEN_HERE' # OK https://ok.ru/vitrine/myuploaded
vk_album_id = '789' # брать с адресной строки, когда открываешь ВК. Пример https://vk.com/album123_789 # Добавить приложение - 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) bot = Bot(token=API_BOT_TOKEN)
storage = MemoryStorage() storage = MemoryStorage()
@ -516,9 +536,15 @@ async def show_thumbs(chat_id, res):
chat_id=chat_id, media=pilToImages(res, "tg") chat_id=chat_id, media=pilToImages(res, "tg")
) )
if dataParams["img_real"] == "true" or dataParams["img_real"] == "True": 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") 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 ---------- # -------- COMMANDS ----------
@ -756,7 +782,7 @@ async def inl_gen(message: Union[types.Message, types.CallbackQuery]) -> None:
# TODO long message # TODO long message
await bot.send_message( await bot.send_message(
chat_id=chatId, chat_id=chatId,
text="↓ send to VK", text="⬇ send to VK and OK ⬇",
reply_markup=InlineKeyboardMarkup( reply_markup=InlineKeyboardMarkup(
inline_keyboard=[[InlineKeyboardButton(mes_file[0].document.file_id, callback_data='send_vk')]]) 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) await getKeyboardUnion("Turn on SD"+sd, message, keyboard)
# upload in VK # upload in VK
# TODO actual prompt
@dp.callback_query_handler(text="send_vk") @dp.callback_query_handler(text="send_vk")
async def inl_samplers(callback: types.CallbackQuery) -> None: async def send_vk(callback: types.CallbackQuery) -> None:
try: 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_id = callback.message.reply_markup.inline_keyboard[0][0].text #TODO
file_obj = await bot.get_file(file_id) file_obj = await bot.get_file(file_id)
vk_session = vk_api.VkApi(token=VK_TOKEN) 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) file.write(requests.get(file_url).content)
vk_upload.photo( vk_upload.photo(
photos='temp.png', photos='temp.png',
album_id=vk_album_id, album_id=VK_ALBUM_ID,
caption=get_prompt_settings(0) 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 # clear garbage
os.remove('temp.png') os.remove('temp.png')
await callback.message.edit_text( await callback.message.edit_text(
'Фотка в VK загружена' 'Фотка в VK и OK загружена'
) )
except Exception as e: except Exception as e:
await bot.send_message( await bot.send_message(