This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
193 lines (192 loc) · 5.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Router } from "itty-router";
import {
getAssetFromKV,
mapRequestToAsset,
} from "@cloudflare/kv-asset-handler";
import { handleScheduled } from "./schedule";
import { config, mode } from "./config";
import { identify } from "./utils/identify";
import { setTgBot } from "./bot";
const secret_path = config.SECRET_PATH;
const router = Router();
if (mode === "telegram") {
setTgBot(router);
}
const errorHandler = (error) =>
new Response(error.message || "Server Error", {
status: error.status || 500,
});
router.get("/", async () => {
return new Response("Only the wise can see this page", { status: 200 });
});
router.get(`/${secret_path}`, async (req, e) => {
const data = await KV.get("sub");
if (!data) {
await KV.put("sub", "[]");
}
return await getAssetFromKV(e, {
mapRequestToAsset: (req) => {
let defaultAssetKey = mapRequestToAsset(req);
let url = new URL(defaultAssetKey.url);
url.pathname = url.pathname.replace(secret_path, "/");
return new Request(url.toString(), defaultAssetKey);
},
});
});
router.get(`/${secret_path}/feeds`, async () => {
const raw = await KV.get("sub");
return new Response(raw, { status: 200 });
});
router.post(`/${secret_path}/subitem`, async (req) => {
const subraw = (await KV.get("sub")) || "[]";
let sub = JSON.parse(subraw);
const body = await req.json();
if (body.url === undefined) {
return new Response(
JSON.stringify({
status: 400,
message: "Url not found",
})
);
}
const msg = body.url;
const resp = await fetch(`${config.PARSE_URL}/api/xml2json`, {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
body: JSON.stringify({
url: msg,
}),
});
if (resp.status === 200) {
let feed = {};
const data = await resp.json();
feed.title = data.title.replaceAll("\n", "");
feed.url = msg;
feed.telegraph = false;
feed.active = true;
feed.errorTimes = 0;
if (
sub.findIndex((e) => e.url === feed.url) != -1 ||
sub.findIndex((e) => e.title === feed.title) != -1
) {
return new Response(
JSON.stringify({
status: 400,
message: "Already subscribed",
})
);
} else {
const now = new Date();
feed.lastUpdateTime = now;
const textres = await fetch(msg);
feed.id = identify(await textres.text());
sub.push(feed);
await KV.put("sub", JSON.stringify(sub));
return new Response(
JSON.stringify({
status: 0,
message: `${feed.title} add succeed`,
})
);
}
} else {
return new Response(
JSON.stringify({
status: 400,
message: "can't parse this url",
})
);
}
});
router.post(`/${secret_path}/deleteitem`, async (req) => {
const subraw = await KV.get("sub");
let sub = JSON.parse(subraw);
const body = await req.json();
const url = body.url;
const index = sub.findIndex((e) => e.url === url);
if (index === -1) {
return new Response(
JSON.stringify({
status: 400,
message: "Url not found",
})
);
} else {
sub.splice(index, 1);
await KV.put("sub", JSON.stringify(sub));
return new Response(
JSON.stringify({
status: 0,
message: "Delete succeed!",
})
);
}
});
router.post(`/${secret_path}/active`, async (req) => {
const subraw = await KV.get("sub");
let sub = JSON.parse(subraw);
const body = await req.json();
const url = body.url || "";
const state = body.state;
const index = sub.findIndex((e) => e.url === url);
if (index === -1 || state === undefined) {
return new Response(
JSON.stringify({
status: 400,
message: "Please verify your input!",
})
);
}
sub[index].active = state;
sub[index].lastUpdateTime = new Date();
sub[index].errorTimes = 0;
await KV.put("sub", JSON.stringify(sub));
return new Response(
JSON.stringify({
status: 0,
message: `修改成功,当前状态为 ${state ? "on" : "off"}`,
})
);
});
router.post(`/${secret_path}/telegraph`, async (req) => {
const subraw = await KV.get("sub");
let sub = JSON.parse(subraw);
const body = await req.json();
const url = body.url || "";
const state = body.state;
const index = sub.findIndex((e) => e.url === url);
if (index === -1 || state === undefined) {
return new Response(
JSON.stringify({
status: 400,
message: "Please verify your input!",
})
);
}
sub[index].telegraph = state;
await KV.put("sub", JSON.stringify(sub));
return new Response(
JSON.stringify({
status: 0,
message: `修改成功,当前状态为 ${state ? "on" : "off"}`,
})
);
});
router.get("/test", async (req, e) => {
e.waitUntil(handleScheduled(e));
});
router.get("*", async (req, e) => {
try {
return await getAssetFromKV(e);
} catch (err) {
return new Response("An unexpected error occurred", { status: 500 });
}
});
addEventListener("fetch", (e) => {
e.respondWith(router.handle(e.request, e).catch(errorHandler));
});
addEventListener("scheduled", async (event) => {
event.waitUntil(handleScheduled(event));
});