55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
import fs from 'fs'
|
|
import express from 'express'
|
|
import { main, get_search, get_views } from './main.js'
|
|
|
|
const app = express()
|
|
|
|
app.use(express.json())
|
|
|
|
app.get('/api/get_views/:name', (req, res) => {
|
|
res.json(get_views(req.params.name, req.query.ids))
|
|
})
|
|
|
|
app.get('/api/get_search/hot', (req, res) => {
|
|
res.json(get_search(req.query.pagesize || 10))
|
|
})
|
|
|
|
app.get('/api', (req, res) => {
|
|
const { start, end } = req.query
|
|
console.log(req.query)
|
|
if (!start || !end) return res.status(400).json({
|
|
success: false,
|
|
message: '请求无效,参数错误',
|
|
})
|
|
|
|
const 作品 = new Map()
|
|
const 游戏 = new Map()
|
|
const 截图 = new Map()
|
|
|
|
// 读取目录中的所有文件
|
|
fs.readdirSync('./data/').filter(name => {
|
|
const type = name.endsWith('.json')
|
|
const match = name.match(/\d{4}-\d{2}-\d{2}/)
|
|
if (!match) return false
|
|
const date = new Date(match[0])
|
|
return type && new Date(start) <= date && new Date(end) >= date
|
|
}).forEach(name => {
|
|
const file = fs.readFileSync(`./data/${name}`, 'utf8')
|
|
if (file) {
|
|
const data = JSON.parse(file)
|
|
Object.entries(data.作品).forEach(([k, v]) => 作品.set(k, v))
|
|
Object.entries(data.游戏).forEach(([k, v]) => 游戏.set(k, v))
|
|
Object.entries(data.截图).forEach(([k, v]) => 截图.set(k, v))
|
|
}
|
|
})
|
|
|
|
res.json({
|
|
作品: [...作品.entries()].sort((a, b) => b[1] - a[1]).map(([k]) => k),
|
|
游戏: [...游戏.entries()].sort((a, b) => b[1] - a[1]).map(([k]) => k),
|
|
截图: [...截图.entries()].sort((a, b) => b[1] - a[1]).map(([k]) => k),
|
|
})
|
|
})
|
|
|
|
app.listen(6005, () => console.log('http://localhost:6005'))
|
|
main()
|