recover file url if not present

This commit is contained in:
bakatrouble 2023-11-03 15:57:29 +03:00
parent 6cb3948fa5
commit 992493aa15
2 changed files with 65 additions and 63 deletions

View File

@ -136,9 +136,8 @@ class E621:
def __init__(self): def __init__(self):
self.client = httpx.AsyncClient(headers={'user-agent': 'bot/1.0 (bakatrouble)'}, base_url='https://e621.net') self.client = httpx.AsyncClient(headers={'user-agent': 'bot/1.0 (bakatrouble)'}, base_url='https://e621.net')
async def get_posts(self, tags='', page=1, limit=100) -> List[E621Post]: async def get_posts(self, tags='', page=1, limit=320) -> List[E621Post]:
r = (await self.client.get('/posts.json', params={'tags': tags, 'page': page, 'limit': limit})).json() r = (await self.client.get('/posts.json', params={'tags': tags, 'page': page, 'limit': limit})).json()
logging.warning(json.dumps(r))
return [E621Post.from_dict(p) for p in r['posts']] return [E621Post.from_dict(p) for p in r['posts']]
async def get_post(self, post_id: str) -> E621Post: async def get_post(self, post_id: str) -> E621Post:

11
main.py
View File

@ -25,7 +25,7 @@ from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, B
CallbackQuery CallbackQuery
import dotenv import dotenv
from e621 import E621, E621Post from e621 import E621, E621Post, E621PostFile
dotenv.load_dotenv('.env') dotenv.load_dotenv('.env')
@ -43,6 +43,10 @@ def format_tags(tags: Iterable[str]):
return ' '.join(f'#{tag}' for tag in tags) or 'None' return ' '.join(f'#{tag}' for tag in tags) or 'None'
def recover_url(file: E621PostFile):
return f'https://static1.e621.net/data/{file.md5[:2]}/{file.md5[:2:4]}/{file.md5}.{file.ext}'
async def send_post(post: E621Post, tag_list: List[str]): async def send_post(post: E621Post, tag_list: List[str]):
try: try:
logging.warning(f'Sending post #{post.id}') logging.warning(f'Sending post #{post.id}')
@ -58,7 +62,8 @@ async def send_post(post: E621Post, tag_list: List[str]):
copyright_tags and f'Copyright: <b>{format_tags(copyright_tags)}</b>', copyright_tags and f'Copyright: <b>{format_tags(copyright_tags)}</b>',
f'\nhttps://e621.net/posts/{post.id}' f'\nhttps://e621.net/posts/{post.id}'
] if l) ] if l)
if post.file.url: if not post.file.url:
post.file.url = recover_url(post.file)
try: try:
logging.warning(post.file.url) logging.warning(post.file.url)
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
@ -118,8 +123,6 @@ async def send_post(post: E621Post, tag_list: List[str]):
await redis.sadd('e621:sent', post.id) await redis.sadd('e621:sent', post.id)
except Exception as e: except Exception as e:
logging.exception(e) logging.exception(e)
else:
logging.warning('Not sending because url is None', post.file)
except Exception as e: except Exception as e:
logging.exception(e) logging.exception(e)