更新eo, ecy图片改为获取全部

This commit is contained in:
wood 2024-06-28 11:08:28 +08:00
parent 6818014ee3
commit 1b8e11a1af
6 changed files with 1085 additions and 1064 deletions

32
eo.js
View File

@ -5,14 +5,25 @@ addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
// 处理请求
/**
* 处理客户端请求并根据请求的URL路径获取对应的CSV文件中的随机一行的URL然后重定向到该URL
*
* @param {Request} request 客户端发起的请求对象
* @returns {Response} 根据不同的情况返回不同的响应对象如果能够成功获取CSV路径配置且请求的路径在配置中找到对应的CSV文件
* 则重定向到该CSV文件中随机一行的URL如果CSV路径配置无法获取或请求的路径未在配置中找到
* 则返回相应的错误页面或默认首页
*/
async function handleRequest(request) {
// 从CSV_PATHS_URL获取CSV文件路径配置
const csvPathsResponse = await fetch(CSV_PATHS_URL);
// 配置获取失败时返回500错误响应
if (!csvPathsResponse.ok) {
return new Response('CSV paths configuration could not be fetched.', { status: 500 });
}
// 将配置响应体解析为JSON对象
const csvPaths = await csvPathsResponse.json();
// 解析请求的URL路径
const url = new URL(request.url);
let path = url.pathname.slice(1); // 移除路径前的斜杠
path = path.split('?')[0]; // 移除问号后的部分
@ -20,32 +31,37 @@ async function handleRequest(request) {
path = path.slice(0, -1); // 移除路径后的斜杠
}
// 分割路径得到前缀和后缀
const pathSegments = path.split('/');
// 分割路径前缀和后缀
const pathSegments = path.split('/');
const prefix = pathSegments[0];
const suffix = pathSegments.slice(1).join('/');
// 如果路径是 CSV 资源路径
// 检查请求路径是否在CSV路径配置中
if (prefix in csvPaths && suffix in csvPaths[prefix]) {
// 根据配置获取对应的CSV文件URL
const csvUrl = csvPaths[prefix][suffix];
// 从CSV文件URL获取文件内容
const fileArrayResponse = await fetch(csvUrl);
// 文件内容获取失败时返回500错误响应
if (fileArrayResponse.ok) {
// 处理CSV文件内容过滤空行和注释行
const fileArrayText = await fileArrayResponse.text();
const fileArray = fileArrayText.split('\n').filter(Boolean); // 过滤掉空行
const fileArray = fileArrayText.split('\n').filter(line => Boolean(line) && !line.trim().startsWith('#'));
// 随机选择一行URL进行重定向
const randomIndex = Math.floor(Math.random() * fileArray.length);
const randomUrl = fileArray[randomIndex];
// 返回302重定向到randomUrl,1更快2方便排查
return Response.redirect(randomUrl, 302);
} else {
return new Response('CSV file could not be fetched.', { status: 500 });
}
} else {
// 如果不是 CSV 资源路径,返回 index.html 内容
// 请求路径不在配置中,返回默认首页
const indexHtmlResponse = await fetch('https://random-api-file.czl.net');
return new Response(indexHtmlResponse.body, {
headers: { 'Content-Type': 'text/html' },
});
}
}
}

View File

@ -5,6 +5,7 @@
"czlwb": "https://random-api-file.czl.net/url/pic/czl-website-background.csv",
"truegirl": "https://random-api-file.czl.net/url/pic/truegirl.csv",
"ecy": "https://random-api-file.czl.net/url/pic/ecy.csv",
"ecy1": "https://random-api-file.czl.net/url/pic/ecy1.csv",
"ecy2": "https://random-api-file.czl.net/url/pic/ecy2.csv",
"test": "https://random-api-file.czl.net/url/pic/test.csv"
},

File diff suppressed because it is too large Load Diff

1053
url/pic/ecy1.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1 +1 @@
https://cdn-img.czl.net/woodchenink/2024/1/16/%E6%B5%AA%E5%AE%A2%E5%89%91%E5%BF%83%E8%BF%BD%E5%BF%86%E7%AF%8730%E5%B8%A71080P%E6%97%A0%E5%A3%B0%E9%9F%B3.mp4
https://cdn-r2-cloudflare.czl.net/woodchenink/2024/1/16/%E6%B5%AA%E5%AE%A2%E5%89%91%E5%BF%83%E8%BF%BD%E5%BF%86%E7%AF%8730%E5%B8%A71080P%E6%97%A0%E5%A3%B0%E9%9F%B3.mp4
1 https://cdn-img.czl.net/woodchenink/2024/1/16/%E6%B5%AA%E5%AE%A2%E5%89%91%E5%BF%83%E8%BF%BD%E5%BF%86%E7%AF%8730%E5%B8%A71080P%E6%97%A0%E5%A3%B0%E9%9F%B3.mp4 https://cdn-r2-cloudflare.czl.net/woodchenink/2024/1/16/%E6%B5%AA%E5%AE%A2%E5%89%91%E5%BF%83%E8%BF%BD%E5%BF%86%E7%AF%8730%E5%B8%A71080P%E6%97%A0%E5%A3%B0%E9%9F%B3.mp4

View File

@ -1,5 +1,5 @@
// 外部 JSON 文件的 URL
const CSV_PATHS_URL = 'https://random-api.pages.dev/url.json';
const CSV_PATHS_URL = 'https://random-api-file.czl.net/url.json';
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
@ -58,7 +58,7 @@ async function handleRequest(request) {
}
} else {
// 请求路径不在配置中,返回默认首页
const indexHtmlResponse = await fetch('https://random-api.pages.dev');
const indexHtmlResponse = await fetch('https://random-api-file.czl.net');
return new Response(indexHtmlResponse.body, {
headers: { 'Content-Type': 'text/html' },
});