diff --git a/mastobot.py b/mastobot.py index c8534ee..244a4c9 100644 --- a/mastobot.py +++ b/mastobot.py @@ -4,6 +4,9 @@ from mastodon import Mastodon import re import time import sys +import os +import requests +import base64 import pdb def cleanhtml(raw_html): @@ -102,37 +105,94 @@ if __name__ == '__main__': if reply == True: - answer = xat.completions(prompt=query_word) + prompt = query_word - answer = answer.choices[0]['text'] + if prompt[:7] == 'imatge:': - if 'com.example.demo.controller' not in answer: + try: - post_text = f'@{username} {answer}' + imatge = xat.create_image(prompt=prompt[7:]) - if len(post_text) > 500: + if not 'error' in imatge: - len_post1 = 500 - len(username) - 4 + with open("images/imatge.png", "wb") as fh: + fh.write(base64.b64decode(imatge['data'][0]['b64_json'])) - post_text1 = post_text[:len_post1] + '... ' + post_text = f'@{username} {prompt[8:]}' - post_text2 = f'@{username} ...{post_text[len_post1:]}' + image_id = mastodon.media_post('images/imatge.png', "image/png", description=prompt[8:]).id - if len(post_text2) > 500: + mastodon.status_post(post_text, in_reply_to_id=status_id, media_ids={image_id}, visibility=visibility) - len_post2 = 500 - len(username) - 8 + else: - post_text2 = f'{post_text2[:len_post2]}...' + post_text = f'@{username} {prompt[8:]}' - post_text3 = f'@{username} ...{post_text2[len_post2:]}' + post_text += f"\n\n{imatge['error']['message']}" - try: + mastodon.status_post(post_text, in_reply_to_id=status_id, visibility=visibility) + + except: + + pass + + post_text = f'@{username} {prompt[8:]}\n\n{imatge}' + + mastodon.status_post(post_text, in_reply_to_id=status_id, visibility=visibility) + + else: + + answer = xat.completions(prompt=prompt) + + answer = answer.choices[0]['text'] + + if 'com.example.demo.controller' not in answer: + + post_text = f'@{username} {answer}' + + if len(post_text) > 500: + + len_post1 = 500 - len(username) - 4 + + post_text1 = post_text[:len_post1] + '... ' + + post_text2 = f'@{username} ...{post_text[len_post1:]}' + + if len(post_text2) > 500: + + len_post2 = 500 - len(username) - 8 + + post_text2 = f'{post_text2[:len_post2]}...' + + post_text3 = f'@{username} ...{post_text2[len_post2:]}' + + try: + + post_id = mastodon.status_post(post_text1, in_reply_to_id=status_id,visibility=visibility).id + + post_id = mastodon.status_post(post_text2, in_reply_to_id=post_id,visibility=visibility).id + + mastodon.status_post(post_text3, in_reply_to_id=post_id,visibility=visibility) + + except: + + print(f'Notification {notification_id} dissmised') + + mastodon.notifications_dismiss(notification_id) + + pass + + else: post_id = mastodon.status_post(post_text1, in_reply_to_id=status_id,visibility=visibility).id - post_id = mastodon.status_post(post_text2, in_reply_to_id=post_id,visibility=visibility).id + mastodon.status_post(post_text2, in_reply_to_id=post_id,visibility=visibility) - mastodon.status_post(post_text3, in_reply_to_id=post_id,visibility=visibility) + else: + + try: + + mastodon.status_post(post_text, in_reply_to_id=status_id,visibility=visibility) except: @@ -142,26 +202,6 @@ if __name__ == '__main__': pass - else: - - post_id = mastodon.status_post(post_text1, in_reply_to_id=status_id,visibility=visibility).id - - mastodon.status_post(post_text2, in_reply_to_id=post_id,visibility=visibility) - - else: - - try: - - mastodon.status_post(post_text, in_reply_to_id=status_id,visibility=visibility) - - except: - - print(f'Notification {notification_id} dissmised') - - mastodon.notifications_dismiss(notification_id) - - pass - time.sleep(5) print(f'Notification {notification_id} replied') diff --git a/xatapi.py b/xatapi.py index 3c0882a..4ff011c 100644 --- a/xatapi.py +++ b/xatapi.py @@ -86,6 +86,23 @@ class Xat: return response + def create_image(self, model=None, prompt=None): + + data = { + 'prompt': prompt, + 'n': 1, + 'size': "512x512", + 'response_format': "b64_json" + } + + endpoint = f'{self.api_base_url}/v1/images/generations' + + response = self.__api_request('POST', endpoint, data) + + response = self.__json_allow_dict_attrs(response.json()) + + return response + @staticmethod def __check_setup(self): @@ -165,6 +182,8 @@ class Xat: # this is for compatibility with older versions # which raised OpenaiAPIError('Endpoint not found.') # on any 404 + elif response.status_code == 400: + return response elif response.status_code == 401: ex_type = OpenaiUnauthorizedError elif response.status_code == 422: