main
 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3
 4import feedparser
 5from glom import Coalesce, glom
 6from pyrogram.client import Client
 7from pyrogram.types import Message
 8
 9from config import PROXY
10from messages.progress import modify_progress
11from messages.sender import send2tg
12from networking import download_file, hx_req
13
14HEADERS = {
15    "User-Agent": "feedparser/6.0.11 +https://github.com/kurtmckee/feedparser/",
16    "Accept": "application/atom+xml,application/rdf+xml,application/rss+xml,application/x-netcdf,application/xml;q=0.9,text/xml;q=0.2,*/*;q=0.1",
17}
18
19
20async def preview_arxiv(client: Client, message: Message, url: str, arxiv_id: str, **kwargs):
21    """Preview arxiv in the message."""
22    if kwargs.get("show_progress") and "progress" not in kwargs:
23        res = await send2tg(client, message, texts=f"🔗正在解析arXiv链接\n{url}", **kwargs)
24        kwargs["progress"] = res[0]
25    kwargs["send_from_user"] = ""  # disable @send_user
26
27    api = f"https://export.arxiv.org/api/query?id_list={arxiv_id}"
28    resp = await hx_req(api, headers=HEADERS, proxy=PROXY.ARXIV, rformat="text")
29    if "hx_error" in resp:
30        return
31    if not resp.get("text"):
32        await modify_progress(text=f"❌arXiv解析失败: {resp}", force_update=True, **kwargs)
33    arxiv = feedparser.parse(resp["text"])
34
35    entry = glom(arxiv, "entries.0", default={})
36
37    title = glom(entry, "title", default="")
38    updated = glom(entry, Coalesce("updated", "published"), default="")
39    abstract = glom(entry, "summary", default="")
40    comment = glom(entry, "arxiv_comment", default="")
41    authors = ""
42    for author in glom(arxiv, "entries.0.authors", default=[]):
43        if name := author.get("name"):
44            authors += f"{name}, "
45    authors = authors.rstrip(", ")
46    await modify_progress(text="⏬正在下载PDF", force_update=True, **kwargs)
47    pdf = await download_file(f"https://arxiv.org/pdf/{arxiv_id}", suffix=".pdf", proxy=PROXY.ARXIV, stream=True)
48    texts = f"📄**[{title}]({url})**\n👥{authors}\n🕒{updated}\n"
49    if comment:
50        texts += f"📝{comment}\n"
51    texts += f"\n**Abstract**\n{abstract}"
52    await send2tg(client, message, texts=texts, media=[{"document": pdf}], **kwargs)
53    await modify_progress(del_status=True, **kwargs)