Refactor webpage media formatting and import order
This commit is contained in:
+23
-20
@@ -7,9 +7,9 @@ import json
|
||||
from datetime import datetime
|
||||
from typing import Union, Dict, Any, List
|
||||
from pyrogram.types import Message
|
||||
from pyrogram.enums import MessageMediaType
|
||||
from bleach.css_sanitizer import CSSSanitizer
|
||||
from bleach import clean as HTMLSanitizer
|
||||
from pyrogram.enums import MessageMediaType
|
||||
from config import get_settings
|
||||
from url_signer import generate_media_digest
|
||||
|
||||
@@ -402,6 +402,28 @@ class PostParser:
|
||||
html_media = self._sanitize_html(html_media)
|
||||
return html_media
|
||||
|
||||
|
||||
def _format_webpage(self, webpage, message) -> Union[str, None]:
|
||||
base_url = Config['pyrogram_bridge_url']
|
||||
try:
|
||||
if photo := getattr(webpage, "photo", None):
|
||||
if file_unique_id := getattr(photo, "file_unique_id", None):
|
||||
channel_username = self.get_channel_username(message)
|
||||
file = f"{channel_username}/{message.id}/{file_unique_id}"
|
||||
digest = generate_media_digest(file)
|
||||
url = f"{base_url}/media/{file}/{digest}"
|
||||
return (
|
||||
f'<div style="margin:5px;">'
|
||||
f'<a href="{webpage.url}" target="_blank">'
|
||||
f'<img src="{url}" style="max-width:600px; max-height:600px; object-fit:contain;"></a>'
|
||||
f'</div>'
|
||||
)
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"webpage_parsing_error: url {getattr(webpage, 'url', 'unknown')}, error {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
def _generate_html_footer(self, message: Message) -> str:
|
||||
content_footer = []
|
||||
if reactions_views_html := self._reactions_views_links(message): # Add reactions, views, date and links
|
||||
@@ -439,25 +461,6 @@ class PostParser:
|
||||
logger.error(f"url_processing_error: error {str(e)}")
|
||||
return text
|
||||
|
||||
def _format_webpage(self, webpage, message) -> Union[str, None]:
|
||||
base_url = Config['pyrogram_bridge_url']
|
||||
try:
|
||||
if photo := getattr(webpage, "photo", None):
|
||||
if file_unique_id := getattr(photo, "file_unique_id", None):
|
||||
channel_username = self.get_channel_username(message)
|
||||
file = f"{channel_username}/{message.id}/{file_unique_id}"
|
||||
digest = generate_media_digest(file)
|
||||
url = f"{base_url}/media/{file}/{digest}"
|
||||
return (
|
||||
f'<div style="margin:5px;">'
|
||||
f'<a href="{webpage.url}" target="_blank">'
|
||||
f'<img src="{url}" style="max-width:600px; max-height:600px; object-fit:contain;"></a>'
|
||||
f'</div>'
|
||||
)
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"webpage_parsing_error: url {getattr(webpage, 'url', 'unknown')}, error {str(e)}")
|
||||
return None
|
||||
|
||||
def _reactions_views_links(self, message: Message) -> Union[str, None]:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user