-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreview.py
53 lines (45 loc) · 1.96 KB
/
preview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import asyncio
import httpx
from abc import ABC, abstractmethod
from tenacity import retry, AsyncRetrying, stop_after_attempt
from re import Pattern
from telegram import Update
from telegram.ext import ContextTypes
from utils.create_page import create_page
class Preview(ABC):
def __init__(self, pattern:Pattern, channel:str = '', channel_url:str = '', headers = {}, show_origin = False) -> None:
self.pattern = pattern
self.channel = channel
self.channel_url = channel_url
self.headers = headers
self.show_origin = show_origin
async def clean_url(self, URL:str) -> str:
return URL
@retry(stop = stop_after_attempt(5))
async def get_res(self, URL:str):
URL = await self.clean_url(URL)
async with httpx.AsyncClient() as client:
res = await client.get(URL, headers = self.headers)
return res
@abstractmethod
def viewer(self, res):
pass # soup to (title, author, content)
async def to_telegraph(self, URL:str) -> str:
# (title, author, content) to a list of telegraph URL.
soup = await self.get_res(URL)
title, author, content = self.viewer(soup)
res = await create_page(title, content, author = author, author_url=self.channel_url)
if len(res) == 1:
responses = [f'[{title}]({url})' for url in res]
else:
responses = [f'[{title}({i+1})]({url})' for i, url in enumerate(res)]
if self.show_origin:
responses = [r + f' | [原文]({URL})' for r in responses]
return responses
async def preview(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
URL = context.match.group(0)
responses = await self.to_telegraph(URL)
for r in responses:
async for attempt in AsyncRetrying(stop=stop_after_attempt(5)):
with attempt:
await update.message.reply_text(r, parse_mode = "Markdown")