TelegramIndex-Fork/app/views/info_view.py

108 lines
3.8 KiB
Python
Raw Normal View History

2020-12-12 15:16:44 +01:00
import logging
import aiohttp_jinja2
from telethon.tl import types
from telethon.tl.custom import Message
from jinja2 import Markup
from app.util import get_file_name, get_human_size
2020-12-12 17:31:21 +01:00
from app.config import block_downloads
2020-12-12 15:16:44 +01:00
log = logging.getLogger(__name__)
class InfoView:
2021-05-22 16:00:08 +02:00
@aiohttp_jinja2.template("info.html")
2020-12-12 15:16:44 +01:00
async def info(self, req):
file_id = int(req.match_info["id"])
2021-05-22 16:00:08 +02:00
alias_id = req.match_info["chat"]
chat = self.chat_ids[alias_id]
chat_id = chat["chat_id"]
2020-12-12 15:16:44 +01:00
try:
message = await self.client.get_messages(entity=chat_id, ids=file_id)
2021-05-22 16:00:08 +02:00
except Exception:
2020-12-12 15:16:44 +01:00
log.debug(f"Error in getting message {file_id} in {chat_id}", exc_info=True)
message = None
2021-05-22 16:00:08 +02:00
2020-12-12 15:16:44 +01:00
if not message or not isinstance(message, Message):
log.debug(f"no valid entry for {file_id} in {chat_id}")
return {
2021-05-22 16:00:08 +02:00
"found": False,
"reason": "Resource you are looking for cannot be retrived!",
"authenticated": req.app["is_authenticated"],
2020-12-12 15:16:44 +01:00
}
2021-05-22 16:00:08 +02:00
return_val = {
"authenticated": req.app["is_authenticated"],
}
2020-12-12 15:16:44 +01:00
reply_btns = []
if message.reply_markup:
if isinstance(message.reply_markup, types.ReplyInlineMarkup):
2021-05-22 16:00:08 +02:00
reply_btns = [
[
{"url": button.url, "text": button.text}
for button in button_row.buttons
if isinstance(button, types.KeyboardButtonUrl)
]
for button_row in message.reply_markup.rows
]
2020-12-12 15:16:44 +01:00
if message.file and not isinstance(message.media, types.MessageMediaWebPage):
file_name = get_file_name(message)
human_file_size = get_human_size(message.file.size)
2021-05-22 16:00:08 +02:00
media = {"type": message.file.mime_type}
if "video/" in message.file.mime_type:
media["video"] = True
elif "audio/" in message.file.mime_type:
media["audio"] = True
elif "image/" in message.file.mime_type:
media["image"] = True
2020-12-12 15:16:44 +01:00
if message.text:
caption = message.raw_text
else:
2021-05-22 16:00:08 +02:00
caption = ""
caption_html = Markup.escape(caption).__str__().replace("\n", "<br>")
return_val.update(
{
"found": True,
"name": file_name,
"file_id": file_id,
"human_size": human_file_size,
"media": media,
"caption_html": caption_html,
"title": f"Download | {file_name} | {human_file_size}",
"reply_btns": reply_btns,
"thumbnail": f"/{alias_id}/{file_id}/thumbnail",
"download_url": "#"
if block_downloads
else f"/{alias_id}/{file_id}/download",
"page_id": alias_id,
"block_downloads": block_downloads,
}
)
2020-12-12 15:16:44 +01:00
elif message.message:
text = message.raw_text
2021-05-22 16:00:08 +02:00
text_html = Markup.escape(text).__str__().replace("\n", "<br>")
return_val.update(
{
"found": True,
"media": False,
"text_html": text_html,
"reply_btns": reply_btns,
"page_id": alias_id,
}
)
2020-12-12 15:16:44 +01:00
else:
2021-05-22 16:00:08 +02:00
return_val.update(
{
"found": False,
"reason": "Some kind of resource that I cannot display",
}
)
2020-12-12 15:16:44 +01:00
log.debug(f"data for {file_id} in {chat_id} returned as {return_val}")
return return_val