diff --git a/main.py b/main.py
index 13826fe..4f6b188 100644
--- a/main.py
+++ b/main.py
@@ -44,79 +44,82 @@ def format_tags(tags: Iterable[str]):
async def send_post(post: E621Post, tag_list: List[str]):
- logging.warning(f'Sending post #{post.id}')
- await bot.send_chat_action(int(os.environ['SEND_CHAT']), action=ChatAction.TYPING)
- monitored_tags = set(post.tags.flatten()) & set(tag_list)
- artist_tags = post.tags.artist
- character_tags = post.tags.character
- copyright_tags = post.tags.copyright
- caption = '\n'.join(l for l in [
- f'Monitored tags: {format_tags(monitored_tags) or "None"}',
- artist_tags and f'Artist: {format_tags(artist_tags)}',
- character_tags and f'Character: {format_tags(character_tags)}',
- copyright_tags and f'Copyright: {format_tags(copyright_tags)}',
- f'\nhttps://e621.net/posts/{post.id}'
- ] if l)
- if post.file.url:
- try:
- logging.warning(post.file.url)
- async with httpx.AsyncClient() as client:
- file = BytesIO()
- file.write((await client.get(post.file.url)).content)
- file.name = f'file.{post.file.ext}'
- file.seek(0)
- if post.file.ext in ('webm', 'gif'):
- with TemporaryDirectory() as td:
- src_path = Path(td) / f'video.{post.file.ext}'
- mp4_path = Path(td) / 'video.mp4'
- with open(src_path, 'wb') as webm:
- webm.write(file.read())
- video_input = ffmpeg\
- .input(str(src_path))
- cmd = video_input \
- .output(str(mp4_path),
- vf='pad=width=ceil(iw/2)*2:height=ceil(ih/2)*2:x=0:y=0:color=Black',
- vcodec='libx264',
- crf='26')
- logging.info('ffmpeg ' + ' '.join(cmd.get_args()))
- cmd.run()
- s3 = boto3.client('s3', aws_access_key_id=os.environ['AWS_ACCESS_KEY'], aws_secret_access_key=os.environ['AWS_SECRET_KEY'])
- bucket = os.environ['AWS_S3_BUCKET']
- upload_filename = f'e621-{post.id}-{int(time())}.mp4'
- s3.upload_file(mp4_path, bucket, upload_filename, ExtraArgs={'ACL': 'public-read', 'ContentType': 'video/mp4'})
- await bot.send_message(int(os.environ['SEND_CHAT']),
- f'https://{bucket}.s3.amazonaws.com/{upload_filename}\n\n' + caption,
- parse_mode=ParseMode.HTML)
- src_path.unlink()
- mp4_path.unlink()
- elif post.file.ext in ('png', 'jpg'):
- markup = InlineKeyboardMarkup(inline_keyboard=[[
- InlineKeyboardButton(text='NSFW', callback_data='send nsfw'),
- InlineKeyboardButton(text='Safe', callback_data='send pics'),
- ]])
- # if post.file.size > 10000000:
- logging.warning('compressing')
- dl_im = Image.open(file).convert('RGBA')
- size = dl_im.size
- if size[0] > 2000 or size[1] > 2000:
- larger_dimension = max(size)
- ratio = 2000 / larger_dimension
- dl_im = dl_im.resize((int(size[0] * ratio), int(size[1] * ratio)),
- Image.LANCZOS)
- print(f'Resizing from {size[0]}x{size[1]} to {dl_im.size[0]}x{dl_im.size[1]}')
- im = Image.new('RGBA', dl_im.size, (255, 255, 255))
- composite = Image.alpha_composite(im, dl_im).convert('RGB')
+ try:
+ logging.warning(f'Sending post #{post.id}')
+ await bot.send_chat_action(int(os.environ['SEND_CHAT']), action=ChatAction.TYPING)
+ monitored_tags = set(post.tags.flatten()) & set(tag_list)
+ artist_tags = post.tags.artist
+ character_tags = post.tags.character
+ copyright_tags = post.tags.copyright
+ caption = '\n'.join(l for l in [
+ f'Monitored tags: {format_tags(monitored_tags) or "None"}',
+ artist_tags and f'Artist: {format_tags(artist_tags)}',
+ character_tags and f'Character: {format_tags(character_tags)}',
+ copyright_tags and f'Copyright: {format_tags(copyright_tags)}',
+ f'\nhttps://e621.net/posts/{post.id}'
+ ] if l)
+ if post.file.url:
+ try:
+ logging.warning(post.file.url)
+ async with httpx.AsyncClient() as client:
file = BytesIO()
- composite.save(file, format='JPEG')
+ file.write((await client.get(post.file.url)).content)
+ file.name = f'file.{post.file.ext}'
file.seek(0)
- await bot.send_photo(int(os.environ['SEND_CHAT']),
- BufferedInputFile(file.read(), 'file.jpg'),
- caption=caption,
- parse_mode=ParseMode.HTML,
- reply_markup=markup)
- await redis.sadd('e621:sent', post.id)
- except Exception as e:
- logging.exception(e)
+ if post.file.ext in ('webm', 'gif'):
+ with TemporaryDirectory() as td:
+ src_path = Path(td) / f'video.{post.file.ext}'
+ mp4_path = Path(td) / 'video.mp4'
+ with open(src_path, 'wb') as webm:
+ webm.write(file.read())
+ video_input = ffmpeg\
+ .input(str(src_path))
+ cmd = video_input \
+ .output(str(mp4_path),
+ vf='pad=width=ceil(iw/2)*2:height=ceil(ih/2)*2:x=0:y=0:color=Black',
+ vcodec='libx264',
+ crf='26')
+ logging.info('ffmpeg ' + ' '.join(cmd.get_args()))
+ cmd.run()
+ s3 = boto3.client('s3', aws_access_key_id=os.environ['AWS_ACCESS_KEY'], aws_secret_access_key=os.environ['AWS_SECRET_KEY'])
+ bucket = os.environ['AWS_S3_BUCKET']
+ upload_filename = f'e621-{post.id}-{int(time())}.mp4'
+ s3.upload_file(mp4_path, bucket, upload_filename, ExtraArgs={'ACL': 'public-read', 'ContentType': 'video/mp4'})
+ await bot.send_message(int(os.environ['SEND_CHAT']),
+ f'https://{bucket}.s3.amazonaws.com/{upload_filename}\n\n' + caption,
+ parse_mode=ParseMode.HTML)
+ src_path.unlink()
+ mp4_path.unlink()
+ elif post.file.ext in ('png', 'jpg'):
+ markup = InlineKeyboardMarkup(inline_keyboard=[[
+ InlineKeyboardButton(text='NSFW', callback_data='send nsfw'),
+ InlineKeyboardButton(text='Safe', callback_data='send pics'),
+ ]])
+ # if post.file.size > 10000000:
+ logging.warning('compressing')
+ dl_im = Image.open(file).convert('RGBA')
+ size = dl_im.size
+ if size[0] > 2000 or size[1] > 2000:
+ larger_dimension = max(size)
+ ratio = 2000 / larger_dimension
+ dl_im = dl_im.resize((int(size[0] * ratio), int(size[1] * ratio)),
+ Image.LANCZOS)
+ print(f'Resizing from {size[0]}x{size[1]} to {dl_im.size[0]}x{dl_im.size[1]}')
+ im = Image.new('RGBA', dl_im.size, (255, 255, 255))
+ composite = Image.alpha_composite(im, dl_im).convert('RGB')
+ file = BytesIO()
+ composite.save(file, format='JPEG')
+ file.seek(0)
+ await bot.send_photo(int(os.environ['SEND_CHAT']),
+ BufferedInputFile(file.read(), 'file.jpg'),
+ caption=caption,
+ parse_mode=ParseMode.HTML,
+ reply_markup=markup)
+ await redis.sadd('e621:sent', post.id)
+ except Exception as e:
+ logging.exception(e)
+ except Exception as e:
+ logging.exception(e)
async def check_updates():