-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
313 lines (274 loc) · 9.97 KB
/
main.ts
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { Application, Router, helpers } from "https://deno.land/x/[email protected]/mod.ts"
import { marked } from 'npm:marked'
import axios from 'npm:axios'
import { cron } from 'https://deno.land/x/deno_cron/cron.ts';
const { getQuery } = helpers;
const app = new Application();
const router = new Router()
app.use(async (ctx, next) => {
ctx.response.headers.set('Access-Control-Allow-Origin', '*')
ctx.response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild')
ctx.response.headers.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS')
await next()
})
router.get('/', async ctx => {
const md = `
# Howdz Deno
This is a \`Deno\` pratice project by [Leon.D](https://kongfandong.cn)
---
## 🚀Public API
**Movie**
+ **[/movieLines](/movieLines)** - Return random movie lines for json type.
+ **[/movieLinesList](/movieLinesList)** - Return movie lines cache list for json type.
---
**Photo**
+ **[/bing/daily](/bing/daily)** - Return daily photo from Bing.
*Params: (\`w\`: number, \`h\`: number, \`type\`: 'redirect' | 'json')*
+ **[/bing/list](/bing/list)** - Return recent half month Bing daily photo list for json type.
+ **[/unsplash/random](/unsplash/random)** - Return random photo from Unsplash.
*Params: (\`w\`: number, \`h\`: number, \`type\`: 'redirect' | 'json', \`keyword\`: string)*
+ **[/unsplash/list](/unsplash/list)** - Return Unsplash list from daily cache for json type.
+ **[/sina/random](/sina/random)** - Return random photo from sina (transfer).
---
**HotList**
+ **[/hotList/zhihu](/hotList/zhihu)** - Return Zhihu hot list
+ **[/hotList/weibo](/hotList/weibo)** - Return Weibo hot list
+ **[/hotList/juejin](/hotList/juejin)** - Return Juejin hot list
---
`
const html = `<title>Howdz Deno</title><link rel="stylesheet" href="https://unpkg.com/[email protected]/out/water.css" /><body>${marked.parse(md)}</body>`
ctx.response.body = html
ctx.response.type = 'html'
})
// Start: === Movie Lines ===
const movieLinesCache = { time: 0, list: [] as any[] }
const getMovieLines = async () => {
const target = 'https://kongfandong.cn/api/movieLinesList'
const res = await fetch(target)
const { time: _time, list: _list } = await res.json()
movieLinesCache.time = _time
movieLinesCache.list = _list
console.log(`update movieLineCache at ${+new Date()}`)
}
router.get('/movieLines', async ctx => {
if (movieLinesCache.list.length === 0) await getMovieLines()
const randomIndex = ~~(Math.random() * movieLinesCache.list.length)
ctx.response.body = { ...movieLinesCache.list[randomIndex], cacheTime: movieLinesCache.time }
})
router.get('/movieLinesList', async ctx => {
if (movieLinesCache.list.length === 0) await getMovieLines()
ctx.response.body = { time: movieLinesCache.time, list: movieLinesCache.list }
})
cron('0 0,15,30,45 * * * *', () => {
getMovieLines(); // refresh pre 15 minutes
});
// End: === Movie Lines ===
// Start: === Bing Photo ===
router.get('/bing/daily', async ctx => {
const { type = 'redirect', w, h } = getQuery(ctx)
const res = await fetch('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1')
const jsonData = await res.json()
if (type === 'json') {
ctx.response.body = jsonData
ctx.response.type = "json"
ctx.response.status = 200
} else {
const { images } = jsonData
const [todayImg] = images
const { url: todayImgURL } = todayImg
let target = `https://cn.bing.com`
target += todayImgURL
if (w) target += `&w=${w}`
if (h) target += `&h=${h}`
ctx.response.redirect(target)
}
})
const bingListCache = { time: 0, list: [] as any[] }
const getBingList = async () => {
const page1 = `https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=8&mkt=zh-CN`
const res1 = await fetch(page1)
const list1 = await res1.json()
const page2 = `https://cn.bing.com/HPImageArchive.aspx?format=js&idx=8&n=8&mkt=zh-CN`
const res2 = await fetch(page2)
const list2 = await res2.json()
const list = [...list1.images, ...list2.images].map((item: any) => {
return {
url: `https://cn.bing.com/${item.url}`,
thumb: `https://cn.bing.com/${item.urlbase}_320x240.jpg&rf=LaDigue_1920x1080.jpg&pid=hp`,
title: item.title
}
})
bingListCache.time = +new Date()
bingListCache.list = list
console.log(`update bingListCache at ${+new Date()}`)
}
router.get('/bing/list', async ctx => {
if (bingListCache.list.length === 0) await getBingList()
ctx.response.body = { time: bingListCache.time, list: bingListCache.list }
})
cron('0 5 0,12 * * *', () => {
getBingList(); // refresh pre 12 hours
});
// End: === Bing Photo ===
// Start: === Unsplash Photo ===
router.get('/unsplash/random', async ctx => {
const { type = 'redirect', w, h, keyword } = getQuery(ctx)
let target = `https://source.unsplash.com/random/`
if (w && h) target += `${w}x${h}/`
if (keyword) target += `?${keyword}`
if (type === 'redirect') {
ctx.response.redirect(target)
} else {
const res = await fetch(target, { method: 'HEAD' })
const url = res.url
const mirror = url.replace('images.unsplash.com', 'dogefs.s3.ladydaily.com/~/source/unsplash')
if (type === 'json') {
ctx.response.body = { url, mirror }
} else if (type === 'mirror') {
ctx.response.redirect(mirror)
} else {
ctx.response.redirect(target)
}
}
})
const unsplashListCache = { time: 0, list: [] as any[] }
const getUnsplashList = async () => {
const target = `https://kongfandong.cn/api/photos?pageSize=18`
const res = await fetch(target)
const json = await res.json()
const list = json.data.list.map((item: any) => {
return {
url: item.urls.raw,
thumb: item.urls.thumb,
title: item.description
}
})
unsplashListCache.time = +new Date()
unsplashListCache.list = list
console.log(`update unsplashListCache at ${+new Date()}`)
}
router.get('/unsplash/list', async ctx => {
if (unsplashListCache.list.length === 0) await getUnsplashList()
ctx.response.body = { time: unsplashListCache.time, list: unsplashListCache.list }
})
cron('0 5 0,12 * * *', () => {
getUnsplashList(); // refresh pre 12 hours
})
// End: === Unsplash Photo ===
// Start: === Random sina photo ===
router.get('/sina/random', async ctx => {
const { type = 'redirect' } = getQuery(ctx)
const target = 'https://api.ixiaowai.cn/gqapi/gqapi.php?return=json'
const res = await fetch(target)
const data = await res.json()
if (type === 'json') {
ctx.response.body = data
} else {
ctx.response.redirect(data.imgurl)
}
})
// End: === Random sina photo ===
// ### List Api
// Start: === Juejin List ===
const juejinCache = { time: 0, list: [] as any[] }
const getJuejinList = async () => {
const url = `https://api.juejin.cn/recommend_api/v1/article/recommend_all_feed`
const res = await fetch(url, {
method: 'post',
body: JSON.stringify({ limit: 50, client_type: 2680, cursor: '0', id_type: 2, sort_type: 200 }),
headers: { 'Content-Type': 'application/json' }
})
const data = await res.json()
const list = data.data
juejinCache.time = +new Date()
juejinCache.list = list.reduce((prev: any, curr: any) => {
if (curr.item_type === 2) {
const article = curr && curr.item_info && curr.item_info.article_info
if (article) {
const { article_id, title, digg_count, view_count, link_url } = article
const result = {
article_id,
title,
digg_count,
view_count,
link_url: link_url || `https://juejin.cn/post/${article_id}`
}
return [...prev, result]
}
}
return prev
}, [])
}
router.get('/hotList/juejin', async ctx => {
if (juejinCache.list.length === 0) await getJuejinList()
ctx.response.body = { time: juejinCache.time, list: juejinCache.list }
})
// End: === Juejin List ===
// Start: === Zhihu List ===
const zhihuCache = { time: 0, list: [] as any[] }
const getZhihuList = async () => {
const url = 'https://kongfandong.cn/api/zhihuList'
const res = await fetch(url)
const { time, list } = await res.json()
zhihuCache.time = time
zhihuCache.list = list
}
router.get('/hotList/zhihu', async ctx => {
if (zhihuCache.list.length === 0) await getZhihuList()
ctx.response.body = { time: zhihuCache.time, list: zhihuCache.list }
})
// End: === Zhihu List ===
// Start: === Weibo List ===
const weiboCache = { time: 0, list: [] as any[] }
const getWeiboList = async () => {
const url = 'https://m.weibo.cn/api/container/getIndex?containerid=106003%2526filter_type%253Drealtimehot'
const res = await fetch(url)
const data = await res.json()
const list = data.data.cards[0].card_group
weiboCache.time = +new Date()
weiboCache.list = list.map((item: any) => {
const { pic, desc, icon, scheme, desc_extr } = item
return { pic, desc, icon, scheme, desc_extr }
}).slice(0, 50)
}
router.get('/hotList/weibo', async ctx => {
if (weiboCache.list.length === 0) await getWeiboList()
ctx.response.body = { time: weiboCache.time, list: weiboCache.list }
})
// End: === Weibo List ===
// Start: === Random Verse ===
router.get('/randomVerse', async ctx => {
const res = await fetch('https://v1.jinrishici.com/all.json')
const data = await res.json()
ctx.response.body = data
})
router.post('/genRedirect', async ctx => {
const { url } = await ctx.request.body.json();
let resultURL = ''
try {
const res = await axios.get(url, {
maxRedirects: 0,
validateStatus: function (status) {
return status >= 200 && status < 303;
},
timeout: 10000
})
const { status, headers: { location } } = res;
if (status === 302) {
resultURL = location
}
} catch(e) {
console.error('error')
}
ctx.response.body = { url: resultURL }
})
cron('0 0,15,30,45 * * * *', () => {
getJuejinList(); // refresh pre 12 hours
getZhihuList();
getWeiboList();
});
app.use(router.routes()).use(router.allowedMethods());
app.addEventListener("listen", ({ port, secure }) => {
console.log(`Listening on: ${secure ? "https://" : "http://"}localhost:${port}`);
});
await app.listen({ port: 8888 });