|
| 1 | +import axios from "axios"; |
| 2 | + |
| 3 | +const GITHUB_REPOS_API_URL = |
| 4 | + "https://api.github.com/repos/tangjan/Anime-Girls-Holding-Programming-Books-710px-width/contents"; |
| 5 | +const GITHUB_GIST_API_URL = |
| 6 | + "https://api.github.com/gists/ceb852425be20b772ee2625d9b5ee606"; |
| 7 | + |
| 8 | +const githubApiUrl = axios.create({ |
| 9 | + headers: { |
| 10 | + Authorization: `token ${process.env.TOKEN_OF_GITHUB_GIST}`, |
| 11 | + }, |
| 12 | +}); |
| 13 | + |
| 14 | +async function getRepoContents(path = "", images = []) { |
| 15 | + try { |
| 16 | + const encodedPath = path ? encodeURIComponent(path) : ""; |
| 17 | + const url = encodedPath |
| 18 | + ? `${GITHUB_REPOS_API_URL}/${encodedPath}` |
| 19 | + : GITHUB_REPOS_API_URL; |
| 20 | + |
| 21 | + console.log(`正在请求: ${url}`); |
| 22 | + const { data } = await githubApiUrl.get(url); |
| 23 | + |
| 24 | + if (Array.isArray(data)) { |
| 25 | + for (const item of data) { |
| 26 | + if (item.type === "dir") { |
| 27 | + await getRepoContents(item.path, images); // 递归 |
| 28 | + } else if ( |
| 29 | + item.type === "file" && |
| 30 | + /\.(jpg|jpeg|png|gif)$/i.test(item.name) |
| 31 | + ) { |
| 32 | + images.push(item.download_url); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } catch (error) { |
| 37 | + console.error(`获取路径 ${path} 的内容时出错:`, error.message); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +async function updateGist(content) { |
| 42 | + try { |
| 43 | + await githubApiUrl.patch(GITHUB_GIST_API_URL, { |
| 44 | + files: { |
| 45 | + "Anime-Girls-Holding-Programming-Books-710px-width.json": { |
| 46 | + content: JSON.stringify(content, null, 2), |
| 47 | + }, |
| 48 | + }, |
| 49 | + }); |
| 50 | + console.log("🎉 处理完成,数据已更新到 GitHub Gist:", GITHUB_GIST_API_URL); |
| 51 | + } catch (error) { |
| 52 | + console.error("更新 Gist 时出错:", error.message); |
| 53 | + throw error; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +async function main() { |
| 58 | + try { |
| 59 | + const images = []; |
| 60 | + await getRepoContents("", images); |
| 61 | + |
| 62 | + const result = { |
| 63 | + last_updated: new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), // UTC+8 |
| 64 | + source: |
| 65 | + "https://github.com/tangjan/Anime-Girls-Holding-Programming-Books-710px-width", |
| 66 | + count: images.length, |
| 67 | + images: images, |
| 68 | + }; |
| 69 | + |
| 70 | + await updateGist(result); |
| 71 | + } catch (error) { |
| 72 | + console.error(error.message); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +main(); |
0 commit comments