Get next high quality pic if it can

This commit is contained in:
blank X 2020-12-24 23:16:37 +07:00
parent 7b9ba65f1b
commit ba62714955
1 changed files with 10 additions and 5 deletions

View File

@ -55,12 +55,14 @@ async def saucenao(client, message):
minimum_similarity = Decimal(json['header']['minimum_similarity']) minimum_similarity = Decimal(json['header']['minimum_similarity'])
caption = text = '' caption = text = ''
to_image = False to_image = False
to_thumbnail = None
filename = os.path.join(tempdir, '0') filename = os.path.join(tempdir, '0')
for result in json['results']: for result in json['results']:
if not result['data'].get('ext_urls'): if not result['data'].get('ext_urls'):
continue continue
atext = f'<b>{html.escape(result["header"]["index_name"])}' atext = f'<b>{html.escape(result["header"]["index_name"])}'
if Decimal(result['header']['similarity']) < minimum_similarity: low_similarity = Decimal(result['header']['similarity']) < minimum_similarity
if low_similarity:
atext += ' (low similarity result)' atext += ' (low similarity result)'
atext += '</b>' atext += '</b>'
atext += '\n<b>URL' atext += '\n<b>URL'
@ -105,16 +107,19 @@ async def saucenao(client, message):
to_image = True to_image = True
break break
else: else:
await download_file(result['header']['thumbnail'], filename) if not to_thumbnail:
to_image = True to_thumbnail = result['header'].get('thumbnail')
atext += '\n\n' atext += '\n\n'
length = len((await client.parser.parse(caption + atext, 'html'))['message']) length = len((await client.parser.parse(caption + atext, 'html'))['message'])
if length <= 1024: if length <= 1024:
caption += atext caption += atext
if length > 4096: if length < 4096:
break
text += atext text += atext
elif low_similarity:
break
try: try:
if to_thumbnail and not to_image:
await download_file(to_thumbnail, filename)
ext = await get_file_ext(filename) ext = await get_file_ext(filename)
os.rename(filename, filename + ext) os.rename(filename, filename + ext)
await message.reply_photo(filename + ext, caption=caption) await message.reply_photo(filename + ext, caption=caption)