generated from jigintern/template-deno-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.deno.js
242 lines (214 loc) · 6.57 KB
/
server.deno.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { serveDir } from "https://deno.land/std@0.151.0/http/file_server.ts";
//スクレイピング用のライブラリ
import { DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
//表示するページを変更するための変数
const maxPage=8;
let page = 0;
const zennPageCount = 20;
const issouPageCount = 20;
let flag = true;
Deno.serve(async (req) => {
const pathname = new URL(req.url).pathname;
console.log(pathname);
//データベースにアクセス
const kv = await Deno.openKv(Deno.env.get("DENOKV_URL"));
//クエリパラメータを取得
const param = new URL(req.url).searchParams;
const qiita = param.get("qiita") === "0" ? false : true;
const zenn = param.get("zenn") === "0" ? false : true;
const issou = param.get("issou") === "0" ? false : true;
const keyWord = param.get("q") || "";
const riverId = param.get("riverId");
//レスポンス用のJSON変数
let obj = { "Qiita": [], "Zenn": [], "Issou": [] };
let zennObj = [];
let qiitaObj = [];
let issouObj = [];
if (req.method === "POST" && pathname === "/river") {
console.log("POST");
// リクエストのペイロードを取得
const requestJson = await req.json();
const riverName = requestJson.riverName;
const articles = requestJson.articles;
//ハッシュ値生成
const uuid = self.crypto.randomUUID();
const key = ["river", uuid];
const value = {
"riverName": riverName,
"articles": articles,
};
const result = await kv.set(key, value);
console.log(result);
return Response.json({
status: 200,
riverId: uuid,
});
}
if (req.method === "GET" && pathname === "/river") {
console.log("GET");
if (!riverId) {
return Response.redirect("https://gijudon.deno.dev/", 302);
}
const getResult = await kv.get(["river", riverId]);
//console.log(getResult);
const requestValue = getResult.value;
return Response.json({
requestValue,
});
}
if (req.method === "GET" && pathname === "/article") {
page = 1 + (page % maxPage);
//Qiitaから記事をとってくる
if (qiita) {
console.log("Qiita API");
//トークン情報をヘッダーに登録
const reqQiita = new Request(
//
`https://qiita.com/api/v2/items?page=${page}&per_page=20&sort=stock&query=title:${keyWord}`,
{
headers: {
"Authorization": `Bearer ${(Deno.env.get("QIITA_API_TOKEN"))}`,
},
},
);
const resQiita = await fetch(reqQiita);
const resQiitaData = await resQiita.json();
console.log(
resQiitaData.map((item) => {
return `title: ${item.title}`;
}),
);
qiitaObj.push(...resQiitaData.map((item) => {
return {
title: item.title,
updated_at: item.updated_at,
url: item.url,
description: item.body,
likes_count: item.likes_count,
comments_count: item.comments_count,
username: item.user.id,
};
}));
}
//Zennから記事をとってくる
if (zenn) {
let zennPage;
if (page % 2) {
zennPage = page / 2 - 0.5;
} else {
zennPage = page / 2;
}
flag = !flag;
console.log("ZennAPI");
keyWord.replace(/\s+/g, "");
let zennUrl;
if (keyWord !== "") {
zennUrl =
`https://zenn.dev/api/search?q=${keyWord}&order=alltime&source=articles&page=${page}`;
} else {zennUrl =
`https://zenn.dev/api/articles?order=latest&page=${zennPage}`;}
console.log(zennUrl);
const resZenn = await fetch(
zennUrl,
);
const resZennData = await resZenn.json();
const zennDataSliced = [];
for (
let i = flag * zennPageCount;
i < (flag + 1) * zennPageCount;
i++
) {
zennDataSliced.push(resZennData.articles[i]);
}
zennObj.push(...zennDataSliced.map((item) => {
return {
title: item.title,
updated_at: item.body_updated_at,
url: `https://zenn.dev${item.path}`,
description: "",
likes_count: item.liked_count,
comments_count: item.comments_count,
username: item.user.id, //usernameだと設定していない人がいるため
};
}));
}
//一日一創から記事を取得
if (issou) {
console.log("issou");
//結果を格納する配列
const Results = [];
//RSS
//XMLソースを DOMオブジェクトに変換した物が入っている
const respIssou = await fetch("https://fukuno.jig.jp/rss.xml");
const rssContent = await respIssou.text();
const source = `${
rssContent.slice(0, rssContent.lastIndexOf("<it"))
}</channel></rss>`;
//console.log(source);
const DOM = new DOMParser().parseFromString(
source,
"text/html",
);
const titleTarget = DOM.querySelectorAll(
"rss > channel > item > title",
);
const dateTarget = DOM.querySelectorAll(
"rss > channel > item> pubDate",
);
const urlTarget = DOM.querySelectorAll(
"rss > channel > item> guid",
);
const descriptionTarget = DOM.querySelectorAll(
"rss > channel > item > description",
);
for (
let i = (issouPageCount * (page - 1)) % titleTarget.length;
i < issouPageCount * page ;
i=(i+1)% titleTarget.length
) {
const title = titleTarget[i].innerText;
const updated_at = dateTarget[i].innerText;
const url = urlTarget[i].innerText;
const description = descriptionTarget[i].innerText;
let likes_count;
let comments_count;
let username;
Results.push({
title,
updated_at,
url,
description,
likes_count,
comments_count,
username,
});
}
issouObj.push(...Results.map((item) => {
return {
title: item.title,
updated_at: item.updated_at,
url: item.url,
description: item.description,
likes_count: item.likes_count,
comments_count: item.comments_count,
username: item.username,
};
}));
}
obj.Qiita = qiitaObj;
obj.Zenn = zennObj;
obj.Issou = issouObj;
console.log("show obj");
return new Response(JSON.stringify(obj), {
headers: {
"content-type": "application/json",
},
});
}
return serveDir(req, {
fsRoot: "public",
urlRoot: "",
showDirListing: true,
enableCors: true,
});
});