more logging
This commit is contained in:
parent
e4523b5cb8
commit
fa754bfd76
145
main.py
145
main.py
@ -44,79 +44,82 @@ def format_tags(tags: Iterable[str]):
|
|||||||
|
|
||||||
|
|
||||||
async def send_post(post: E621Post, tag_list: List[str]):
|
async def send_post(post: E621Post, tag_list: List[str]):
|
||||||
logging.warning(f'Sending post #{post.id}')
|
try:
|
||||||
await bot.send_chat_action(int(os.environ['SEND_CHAT']), action=ChatAction.TYPING)
|
logging.warning(f'Sending post #{post.id}')
|
||||||
monitored_tags = set(post.tags.flatten()) & set(tag_list)
|
await bot.send_chat_action(int(os.environ['SEND_CHAT']), action=ChatAction.TYPING)
|
||||||
artist_tags = post.tags.artist
|
monitored_tags = set(post.tags.flatten()) & set(tag_list)
|
||||||
character_tags = post.tags.character
|
artist_tags = post.tags.artist
|
||||||
copyright_tags = post.tags.copyright
|
character_tags = post.tags.character
|
||||||
caption = '\n'.join(l for l in [
|
copyright_tags = post.tags.copyright
|
||||||
f'Monitored tags: <b>{format_tags(monitored_tags) or "None"}</b>',
|
caption = '\n'.join(l for l in [
|
||||||
artist_tags and f'Artist: <b>{format_tags(artist_tags)}</b>',
|
f'Monitored tags: <b>{format_tags(monitored_tags) or "None"}</b>',
|
||||||
character_tags and f'Character: <b>{format_tags(character_tags)}</b>',
|
artist_tags and f'Artist: <b>{format_tags(artist_tags)}</b>',
|
||||||
copyright_tags and f'Copyright: <b>{format_tags(copyright_tags)}</b>',
|
character_tags and f'Character: <b>{format_tags(character_tags)}</b>',
|
||||||
f'\nhttps://e621.net/posts/{post.id}'
|
copyright_tags and f'Copyright: <b>{format_tags(copyright_tags)}</b>',
|
||||||
] if l)
|
f'\nhttps://e621.net/posts/{post.id}'
|
||||||
if post.file.url:
|
] if l)
|
||||||
try:
|
if post.file.url:
|
||||||
logging.warning(post.file.url)
|
try:
|
||||||
async with httpx.AsyncClient() as client:
|
logging.warning(post.file.url)
|
||||||
file = BytesIO()
|
async with httpx.AsyncClient() as client:
|
||||||
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')
|
|
||||||
file = BytesIO()
|
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)
|
file.seek(0)
|
||||||
await bot.send_photo(int(os.environ['SEND_CHAT']),
|
if post.file.ext in ('webm', 'gif'):
|
||||||
BufferedInputFile(file.read(), 'file.jpg'),
|
with TemporaryDirectory() as td:
|
||||||
caption=caption,
|
src_path = Path(td) / f'video.{post.file.ext}'
|
||||||
parse_mode=ParseMode.HTML,
|
mp4_path = Path(td) / 'video.mp4'
|
||||||
reply_markup=markup)
|
with open(src_path, 'wb') as webm:
|
||||||
await redis.sadd('e621:sent', post.id)
|
webm.write(file.read())
|
||||||
except Exception as e:
|
video_input = ffmpeg\
|
||||||
logging.exception(e)
|
.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():
|
async def check_updates():
|
||||||
|
Loading…
Reference in New Issue
Block a user