[哲也] 增加一些功能

This commit is contained in:
blackmatrix7
2023-02-11 09:05:40 +08:00
parent c1019ccb1c
commit 3bc776aa28
10 changed files with 187 additions and 1319 deletions

View File

@ -0,0 +1,36 @@
# -*-coding:utf-8-*-
import asyncio
import aiohttp
from fastapi import FastAPI
app = FastAPI()
async def fetch(session, url):
if url and url.startswith('https://www.zhihu.com/appview/v2/answer'):
async with session.get(url) as resp:
return await resp.text()
else:
return ""
@app.post("/api/v1/answer/links")
async def request_zhihu_answers(links: list[str]):
loop = asyncio.get_event_loop()
tasks = []
async with aiohttp.ClientSession() as session:
for link in links:
tasks.append(loop.create_task(fetch(session, link)))
responses = await asyncio.gather(*tasks)
result = []
for link, response in zip(links, responses):
if ('查看完整内容' in response or '查看全部章节' in response) and 'paid' in response:
result.append("付费内容")
elif 'ad-link-card' in response or 'xg.zhihu.com' in response or '营销平台' in response:
result.append("营销推广")
elif 'mcn-link-card' in response:
result.append("购物推广")
else:
result.append("")
return result

Binary file not shown.

View File

@ -0,0 +1,50 @@
addEventListener("fetch", (event) => {
if (event.request.method === "POST" && event.request.url.endsWith("api/v1/answer/links")) {
event.respondWith(handlePostRequest(event.request));
} else {
event.respondWith(new Response('Forbidden', {status: 403}));
}
});
async function handlePostRequest(request) {
let requestBody = await request.json();
let promise = [];
let checkResult = [];
for (let link of requestBody) {
promise.push(_request(link));
}
await Promise.all(promise).then(result => {
checkResult = result;
})
let bodyString = JSON.stringify(checkResult);
return new Response(bodyString, {status: 200});
}
async function _request(url) {
return new Promise(resolve => {
if (!url || url === "" || !url.startsWith("https://www.zhihu.com/appview/v2/answer")) {
resolve("");
} else {
fetch(url).then(async resp => {
let responseText = await resp.text();
// 付费内容
if ((responseText.indexOf("查看完整内容") >= 0 || responseText.indexOf("查看全部章节") >= 0) && responseText.indexOf("paid") >= 0) {
resolve("付费内容");
}
// 营销推广提醒
else if (responseText.indexOf("ad-link-card") >= 0 || responseText.indexOf("xg.zhihu.com") >= 0 || responseText.indexOf("营销平台") >= 0) {
resolve("营销推广");
}
// 购物推广提醒
else if (responseText.indexOf("mcn-link-card") >= 0) {
resolve("购物推广");
} else {
resolve("");
}
}).catch(err => {
console.log(err);
resolve("");
});
}
});
}