mirror of
https://github.com/afoim/fuwari.git
synced 2026-01-31 09:03:18 +08:00
posts: 更新随机图API架构说明并添加重命名插件文章
添加关于批量重命名插件的实现文章,并更新随机图API架构说明 移除旧版基于Cloudflare R2的实现,新增EdgeOne Pages Functions方案
This commit is contained in:
BIN
src/content/assets/images/2025-09-06-02-01-14-image.png
Normal file
BIN
src/content/assets/images/2025-09-06-02-01-14-image.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
BIN
src/content/assets/images/2025-09-06-02-06-32-image.png
Normal file
BIN
src/content/assets/images/2025-09-06-02-06-32-image.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 614 KiB |
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: 公开架构,我的二次元随机图API是怎么做的
|
title: 公开架构,我的二次元随机图API是怎么做的
|
||||||
published: 2025-08-31T04:07:40
|
published: 2025-09-06T02:22:00
|
||||||
description: '发现很多小伙伴也想搭建一个自己的随机图API,这里我就公开一下我的架构,打磨2年了,供大家参考~'
|
description: '发现很多小伙伴也想搭建一个自己的随机图API,这里我就公开一下我的架构,打磨2年了,供大家参考~'
|
||||||
image: '../assets/images/2025-08-31-04-09-37-image.png'
|
image: '../assets/images/2025-08-31-04-09-37-image.png'
|
||||||
tags: [随机图API]
|
tags: [随机图API]
|
||||||
@@ -11,15 +11,21 @@ lang: ''
|
|||||||
|
|
||||||
# API端点
|
# API端点
|
||||||
|
|
||||||
门户: https://pic.072103.xyz
|
~~门户: https://pic.072103.xyz~~
|
||||||
|
|
||||||
门户里面的API端点: https://hpic.072103.xyz https://vpic.072103.xyz (CF Worker)
|
~~门户里面的API端点: https://hpic.072103.xyz https://vpic.072103.xyz (CF Worker)~~
|
||||||
|
|
||||||
博客用的API端点: https://eopageapi.2x.nz/pic?img=ua (EdgeOne Pages Functions)
|
博客用的API端点: https://eopageapi.2x.nz/pic?img=ua (EdgeOne Pages Functions)
|
||||||
|
|
||||||
*什么?你问为什么这么乱?你先别急*
|
# 新版实现
|
||||||
|
|
||||||
# 详细实现
|
图源存放在 cnb.cool ,如 https://cnb.cool/2x.nz/r3/-/git/raw/main/ri/h/1.webp 。分为横屏和竖屏随机图,统一重命名为 `数字.webp` ,如 `2333.webp` 。关于重命名插件的编写,可以参阅:[这里](/posts/rename/)
|
||||||
|
|
||||||
|
EdgeOne Pages Functions作为入口,当收到请求后首先区分 横屏、竖屏、自适应,即 `?img=h` `?img=v` `?img=ua` ,然后内部随机出一个数字,最大值在代码中硬编码并且代理请求 https://cnb.cool/2x.nz/r3/-/git/raw/main/ri/h|v/*.webp ,其中,代理请求设置一个独特的 `Accept` 请求头来绕过防盗链。从而使客户端能得到正确的图片响应,关于更多详情,请参考源码: [EdgeOne_Function_PicAPI/functions/pic.js at main · afoim/EdgeOne_Function_PicAPI](https://github.com/afoim/EdgeOne_Function_PicAPI/blob/main/functions/pic.js)
|
||||||
|
|
||||||
|
# 旧版实现
|
||||||
|
|
||||||
|
> 在Cloudflare R2被刷了 **7千万次(GET)请求** 并且扣款 **28.08 USD(折合人民币 207.93 CNY)** 后废弃
|
||||||
|
|
||||||
图源全部存在 **Cloudflare R2**,全部采用 **Webp** 格式,仅分类为 **横屏、竖屏** ,如图
|
图源全部存在 **Cloudflare R2**,全部采用 **Webp** 格式,仅分类为 **横屏、竖屏** ,如图
|
||||||
|
|
||||||
|
|||||||
64
src/content/posts/rename.md
Normal file
64
src/content/posts/rename.md
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
---
|
||||||
|
title: 记录一下批量重命名插件咋写的
|
||||||
|
published: 2025-09-06T01:59:26
|
||||||
|
description: '因为重装系统了,一些小插件丢失了,要重写,然后又把之前的坑踩了一遍'
|
||||||
|
image: '../assets/images/2025-09-06-02-01-14-image.png'
|
||||||
|
tags: [Python]
|
||||||
|
|
||||||
|
draft: false
|
||||||
|
lang: ''
|
||||||
|
---
|
||||||
|
|
||||||
|
# 正式开始
|
||||||
|
|
||||||
|
首先我让AI写一个批量重命名插件
|
||||||
|
|
||||||
|
```bash
|
||||||
|
编写一个重命名的脚本(Python)
|
||||||
|
让我通过 python xxx.py (目录)
|
||||||
|
传入一个目录,然后将内部的所有文件按照 1 2 3 4 5这样重命名,保留原后缀
|
||||||
|
```
|
||||||
|
|
||||||
|
然后AI写出来了,但是有一个交互
|
||||||
|
|
||||||
|
```bash
|
||||||
|
是否要对 xxx 进行批量重命名(Y/N):
|
||||||
|
```
|
||||||
|
|
||||||
|
我就让他删掉
|
||||||
|
|
||||||
|
```bash
|
||||||
|
不要交互,直接运行
|
||||||
|
```
|
||||||
|
|
||||||
|
然后又出现了新问题,如果有一个文件,比如 `100.webp` 已经存在,然后在Python的默认排序中,`100.webp` 可能并不是第 **100** 个,也就是说会有别的文件将要被重命名为 `100.webp` 然后代码对于这样的文件会直接跳过,这就会导致重命名不完全
|
||||||
|
|
||||||
|
那么我就说
|
||||||
|
|
||||||
|
```bash
|
||||||
|
不要跳过 强制重命名
|
||||||
|
```
|
||||||
|
|
||||||
|
然后新的问题出现了:如果已经有一个文件为 `100.webp` 是无法将另一个文件也重命名为 `100.webp` 的,会报错
|
||||||
|
|
||||||
|
```bash
|
||||||
|
重命名文件 '716.webp' 失败: [WinError 183] 当文件已存在时,无法创建该文件。: 'C:\\Users\\acofork\\Pictures\\r1\\ri\\h\\716.webp' -> 'C:\\Users\\acofork\\Pictures\\r1\\ri\\h\\744.webp'
|
||||||
|
```
|
||||||
|
|
||||||
|
那么最后,我用了一个绝妙的办法
|
||||||
|
|
||||||
|
1. 将所有文件随机命名
|
||||||
|
|
||||||
|
2. 重新排序
|
||||||
|
|
||||||
|
3. 依次重命名
|
||||||
|
|
||||||
|
于是我继续说
|
||||||
|
|
||||||
|
```bash
|
||||||
|
先全部命名为 随机数,然后再重新命名
|
||||||
|
```
|
||||||
|
|
||||||
|
这下就对了
|
||||||
|
|
||||||
|

|
||||||
Reference in New Issue
Block a user