From 782adaf8eb04b617431af3467a74b56ff5b75fe4 Mon Sep 17 00:00:00 2001 From: wood chen <95951386+woodchen-ink@users.noreply.github.com> Date: Tue, 16 Jan 2024 21:49:13 +0800 Subject: [PATCH] # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # On branch main # # Initial commit # # Changes to be committed: # new file: README.md # new file: index.html # new file: index.md # new file: url.json # new file: url/pic/all.csv # new file: url/pic/fjht.csv # new file: url/pic/whitebackground.csv # new file: url/video/all.csv # new file: "worker 302\350\267\263\350\275\254.js" # new file: "worker \344\270\215\350\267\263\350\275\254.js" # --- README.md | 40 ++++++++++++ index.html | 117 ++++++++++++++++++++++++++++++++++++ index.md | 45 ++++++++++++++ url.json | 11 ++++ url/pic/all.csv | 11 ++++ url/pic/fjht.csv | 2 + url/pic/whitebackground.csv | 1 + url/video/all.csv | 2 + worker 302跳转.js | 56 +++++++++++++++++ worker 不跳转.js | 56 +++++++++++++++++ 10 files changed, 341 insertions(+) create mode 100644 README.md create mode 100644 index.html create mode 100644 index.md create mode 100644 url.json create mode 100644 url/pic/all.csv create mode 100644 url/pic/fjht.csv create mode 100644 url/pic/whitebackground.csv create mode 100644 url/video/all.csv create mode 100644 worker 302跳转.js create mode 100644 worker 不跳转.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..be44d49 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# Random-Api 随机文件API + +**请给个star,谢谢** + +## 完整说明请查看: + +[https://random-api.woodchen.ink](https://random-api.woodchen.ink) + +> 我的博客说明:[https://woodchen.ink/archives/1705367469203](https://woodchen.ink/archives/1705367469203) + +使用cloudflare worker配合github网页发布功能,实现获取path与csv_path关系,从 `url.csv` 文件中给出的链接来实现一个随机文件 + +不仅可以放图片,还能放文本文件,视频,等等 + + +## 感谢 + +大部分图片来自网络分享的API + +以下不分先后: + +* https://t.mwm.moe/ + + +## 自行部署 + +1. cloudflare 新建一个worker,把 worker.js文件的代码粘贴进去,保存(可以修改为自己的链接) +2. 访问即可 + +## 赞赏 + +如果对这个项目对你有帮助,欢迎打赏,支持微信和支付宝,请留言和备注项目,我会在我的博客上写出,感谢。 + +### 微信 + +![1705312445316.png](https://cdn-img-r2.czl.net/2024/01/15/65a501199193b.png) + +### 支付宝 + +![1705312348956.png](https://cdn-img-r2.czl.net/2024/01/15/65a50089b4b92.png) diff --git a/index.html b/index.html new file mode 100644 index 0000000..aa79e79 --- /dev/null +++ b/index.html @@ -0,0 +1,117 @@ + + + + + 随机文件api-wood + + + + + + + + + + + +
+
+

Random-Api / 随机文件API

+

wood

+
+ +
+
+
+
+
+
+ + + + + \ No newline at end of file diff --git a/index.md b/index.md new file mode 100644 index 0000000..6b579ff --- /dev/null +++ b/index.md @@ -0,0 +1,45 @@ +# Random-Api 随机文件API + +github地址: [https://github.com/woodchen-ink/Random-Api](https://github.com/woodchen-ink/Random-Api) + + +> 我的博客说明:[https://woodchen.ink/archives/1705367469203](https://woodchen.ink/archives/1705367469203) + +使用cloudflare worker配合github网页发布功能,实现获取path与csv_path关系,从 `url.csv` 文件中给出的链接来实现一个随机文件 + +不仅可以放图片,还能放文本文件,视频,等等 + +## 接口 + +| 类型 | 种类 | 请求地址 | +| ---- | ---------- | ---------------- | +| 图片 | 所有 | [https://random-api.woodchen.ink/pic/all](https://random-api.woodchen.ink/pic/all) | +| 图片 | 白底 | [https://random-api.woodchen.ink/pic/whitebackground](https://random-api.woodchen.ink/pic/whitebackground) | +| 图片 | 风景横图 | [https://random-api.woodchen.ink/pic/fjht](https://random-api.woodchen.ink/pic/fjht) | +| 视频 | 所有 | [https://random-api.woodchen.ink/video/all](https://random-api.woodchen.ink/video/all) | + + +## 感谢 + +大部分图片来自网络分享的API + +以下不分先后: + +* https://t.mwm.moe/ + +## 自行部署 + +1. cloudflare 新建一个worker,把 worker.js文件的代码粘贴进去,保存(可以修改为自己的链接) +2. 访问即可 + +## 赞赏 + +如果对这个项目对你有帮助,欢迎打赏,支持微信和支付宝,请留言和备注项目,我会在我的博客上写出,感谢。 + +### 微信 + +![1705312445316.png](https://cdn-img-r2.czl.net/2024/01/15/65a501199193b.png) + +### 支付宝 + +![1705312348956.png](https://cdn-img-r2.czl.net/2024/01/15/65a50089b4b92.png) diff --git a/url.json b/url.json new file mode 100644 index 0000000..fa54ef3 --- /dev/null +++ b/url.json @@ -0,0 +1,11 @@ +{ + "pic": { + "all": "https://raw.githubusercontent.com/woodchen-ink/Random-Api/master/url/pic/all.csv", + "fjht": "https://raw.githubusercontent.com/woodchen-ink/Random-Api/master/url/pic/fjht.csv", + "whitebackground": "https://raw.githubusercontent.com/woodchen-ink/Random-Api/master/url/pic/whitebackground.csv" + }, + "video": { + "all": "https://raw.githubusercontent.com/woodchen-ink/Random-Api/master/url/video/all.csv" + } +} + \ No newline at end of file diff --git a/url/pic/all.csv b/url/pic/all.csv new file mode 100644 index 0000000..154e5c0 --- /dev/null +++ b/url/pic/all.csv @@ -0,0 +1,11 @@ +https://t.mwm.moe/ycy/ +https://t.mwm.moe/ai +https://t.mwm.moe/moez +https://t.mwm.moe/ysz +https://t.mwm.moe/fj +https://t.mwm.moe/lai +https://t.mwm.moe/xhl +https://cdn.seovx.com/?mom=302 +http://www.dmoe.cc/random.php +http://api.mtyqx.cn/api/random.php +https://api.r10086.com/樱道随机图片api接口.php?图片系列=动漫综合2 \ No newline at end of file diff --git a/url/pic/fjht.csv b/url/pic/fjht.csv new file mode 100644 index 0000000..d888b5f --- /dev/null +++ b/url/pic/fjht.csv @@ -0,0 +1,2 @@ +https://t.mwm.moe/fj +https://api.isoyu.com/aipc_scenery.php \ No newline at end of file diff --git a/url/pic/whitebackground.csv b/url/pic/whitebackground.csv new file mode 100644 index 0000000..e245fec --- /dev/null +++ b/url/pic/whitebackground.csv @@ -0,0 +1 @@ +https://t.mwm.moe/bd \ No newline at end of file diff --git a/url/video/all.csv b/url/video/all.csv new file mode 100644 index 0000000..e85d63f --- /dev/null +++ b/url/video/all.csv @@ -0,0 +1,2 @@ +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://t.mwm.moe/acg/acg \ No newline at end of file diff --git a/worker 302跳转.js b/worker 302跳转.js new file mode 100644 index 0000000..95a49aa --- /dev/null +++ b/worker 302跳转.js @@ -0,0 +1,56 @@ +// 外部 JSON 文件的 URL +const CSV_PATHS_URL = 'https://raw.githubusercontent.com/woodchen-ink/Random-Api/master/url.json'; + +addEventListener('fetch', event => { + event.respondWith(handleRequest(event.request)); +}); + +// 处理请求 +async function handleRequest(request) { + const csvPathsResponse = await fetch(CSV_PATHS_URL); + if (!csvPathsResponse.ok) { + return new Response('CSV paths configuration could not be fetched.', { status: 500 }); + } + const csvPaths = await csvPathsResponse.json(); + + const url = new URL(request.url); + let path = url.pathname.slice(1); // 移除路径前的斜杠 + path = path.split('?')[0]; // 移除问号后的部分 + if (path.endsWith('/')) { + path = path.slice(0, -1); // 移除路径后的斜杠 + } + + // 分割路径得到前缀和后缀 + const pathSegments = path.split('/'); + const prefix = pathSegments[0]; + const suffix = pathSegments.slice(1).join('/'); + + // 如果路径是 CSV 资源路径 + if (prefix in csvPaths && suffix in csvPaths[prefix]) { + const csvUrl = csvPaths[prefix][suffix]; + const fileArrayResponse = await fetch(csvUrl); + if (fileArrayResponse.ok) { + const fileArrayText = await fileArrayResponse.text(); + const fileArray = fileArrayText.split('\n').filter(Boolean); // 过滤掉空行 + + const randomIndex = Math.floor(Math.random() * fileArray.length); + const randomUrl = fileArray[randomIndex]; + + // 用原请求的头部创建新的请求对象 + const resourceRequest = new Request(randomUrl, { + headers: request.headers + }); + + // 直接从随机 URL 获取图片内容并返回 + return fetch(resourceRequest); + } else { + return new Response('CSV file could not be fetched.', { status: 500 }); + } + } else { + // 如果不是 CSV 资源路径,返回 index.html 内容 + const indexHtmlResponse = await fetch('https://raw.githubusercontent.com/woodchen-ink/Random-Api/master/index.html'); + return new Response(indexHtmlResponse.body, { + headers: { 'Content-Type': 'text/html' }, + }); + } +} diff --git a/worker 不跳转.js b/worker 不跳转.js new file mode 100644 index 0000000..95a49aa --- /dev/null +++ b/worker 不跳转.js @@ -0,0 +1,56 @@ +// 外部 JSON 文件的 URL +const CSV_PATHS_URL = 'https://raw.githubusercontent.com/woodchen-ink/Random-Api/master/url.json'; + +addEventListener('fetch', event => { + event.respondWith(handleRequest(event.request)); +}); + +// 处理请求 +async function handleRequest(request) { + const csvPathsResponse = await fetch(CSV_PATHS_URL); + if (!csvPathsResponse.ok) { + return new Response('CSV paths configuration could not be fetched.', { status: 500 }); + } + const csvPaths = await csvPathsResponse.json(); + + const url = new URL(request.url); + let path = url.pathname.slice(1); // 移除路径前的斜杠 + path = path.split('?')[0]; // 移除问号后的部分 + if (path.endsWith('/')) { + path = path.slice(0, -1); // 移除路径后的斜杠 + } + + // 分割路径得到前缀和后缀 + const pathSegments = path.split('/'); + const prefix = pathSegments[0]; + const suffix = pathSegments.slice(1).join('/'); + + // 如果路径是 CSV 资源路径 + if (prefix in csvPaths && suffix in csvPaths[prefix]) { + const csvUrl = csvPaths[prefix][suffix]; + const fileArrayResponse = await fetch(csvUrl); + if (fileArrayResponse.ok) { + const fileArrayText = await fileArrayResponse.text(); + const fileArray = fileArrayText.split('\n').filter(Boolean); // 过滤掉空行 + + const randomIndex = Math.floor(Math.random() * fileArray.length); + const randomUrl = fileArray[randomIndex]; + + // 用原请求的头部创建新的请求对象 + const resourceRequest = new Request(randomUrl, { + headers: request.headers + }); + + // 直接从随机 URL 获取图片内容并返回 + return fetch(resourceRequest); + } else { + return new Response('CSV file could not be fetched.', { status: 500 }); + } + } else { + // 如果不是 CSV 资源路径,返回 index.html 内容 + const indexHtmlResponse = await fetch('https://raw.githubusercontent.com/woodchen-ink/Random-Api/master/index.html'); + return new Response(indexHtmlResponse.body, { + headers: { 'Content-Type': 'text/html' }, + }); + } +}