recover file url if not present
This commit is contained in:
parent
6cb3948fa5
commit
992493aa15
3
e621.py
3
e621.py
@ -136,9 +136,8 @@ class E621:
|
||||
def __init__(self):
|
||||
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()
|
||||
logging.warning(json.dumps(r))
|
||||
return [E621Post.from_dict(p) for p in r['posts']]
|
||||
|
||||
async def get_post(self, post_id: str) -> E621Post:
|
||||
|
11
main.py
11
main.py
@ -25,7 +25,7 @@ from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, B
|
||||
CallbackQuery
|
||||
import dotenv
|
||||
|
||||
from e621 import E621, E621Post
|
||||
from e621 import E621, E621Post, E621PostFile
|
||||
|
||||
dotenv.load_dotenv('.env')
|
||||
|
||||
@ -43,6 +43,10 @@ def format_tags(tags: Iterable[str]):
|
||||
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]):
|
||||
try:
|
||||
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>',
|
||||
f'\nhttps://e621.net/posts/{post.id}'
|
||||
] if l)
|
||||
if post.file.url:
|
||||
if not post.file.url:
|
||||
post.file.url = recover_url(post.file)
|
||||
try:
|
||||
logging.warning(post.file.url)
|
||||
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)
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
else:
|
||||
logging.warning('Not sending because url is None', post.file)
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user