feat: updates

This commit is contained in:
qier222 2023-03-03 03:12:27 +08:00
parent 9a52681687
commit 840a5b8e9b
No known key found for this signature in database
104 changed files with 1645 additions and 13494 deletions

View file

@ -4,6 +4,7 @@
"description": "This project was bootstrapped with Fastify-CLI.",
"main": "app.ts",
"scripts": {
"postinstall": "prisma generate",
"start": "fastify start --port 35530 --address 0.0.0.0 -l info dist/app.js",
"build": "tsc",
"watch": "tsc -w",

View file

@ -23,9 +23,10 @@ const album: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
Querystring: {
neteaseId: string
lang?: 'zh-CN' | 'en-US'
noCache?: boolean
}
}>('/album', opts, async function (request, reply): Promise<ResponseSchema | undefined> {
const { neteaseId: neteaseIdString, lang = 'en-US' } = request.query
const { neteaseId: neteaseIdString, lang = 'en-US', noCache = false } = request.query
// validate neteaseAlbumID
const neteaseId = Number(neteaseIdString)
@ -35,12 +36,14 @@ const album: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
}
// get from database
const fromDB = await fastify.prisma.album.findFirst({
where: { neteaseId: neteaseId },
include: { editorialNote: { select: { en_US: true, zh_CN: true } } },
})
if (fromDB) {
return fromDB as ResponseSchema
if (!noCache) {
const fromDB = await fastify.prisma.album.findFirst({
where: { neteaseId: neteaseId },
include: { editorialNote: { select: { en_US: true, zh_CN: true } } },
})
if (fromDB) {
return fromDB as ResponseSchema
}
}
// get from netease
@ -106,11 +109,10 @@ const album: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
neteaseName: albumName,
neteaseArtistName: artist,
}
reply.send(data)
// save to database
await fastify.prisma.album
.create({
if (!noCache) {
await fastify.prisma.album.create({
data: {
...data,
editorialNote: {
@ -121,9 +123,9 @@ const album: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
},
},
})
.catch(e => console.error(e))
}
return
return data
})
}

View file

@ -19,9 +19,10 @@ const artist: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
Querystring: {
neteaseId: string
lang?: 'zh-CN' | 'en-US'
noCache?: boolean
}
}>('/artist', async function (request, reply): Promise<ResponseSchema | undefined> {
const { neteaseId: neteaseIdString, lang = 'en-US' } = request.query
const { neteaseId: neteaseIdString, lang = 'en-US', noCache = false } = request.query
// validate neteaseId
const neteaseId = Number(neteaseIdString)
@ -31,12 +32,14 @@ const artist: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
}
// get from database
const fromDB = await fastify.prisma.artist.findFirst({
where: { neteaseId: neteaseId },
include: { artistBio: { select: { en_US: true, zh_CN: true } } },
})
if (fromDB) {
return fromDB as ResponseSchema
if (!noCache) {
const fromDB = await fastify.prisma.artist.findFirst({
where: { neteaseId: neteaseId },
include: { artistBio: { select: { en_US: true, zh_CN: true } } },
})
if (fromDB) {
return fromDB as ResponseSchema
}
}
// get from netease
@ -95,11 +98,9 @@ const artist: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
artwork: artist?.attributes?.artwork?.url,
}
reply.send(data)
// save to database
await fastify.prisma.artist
.create({
if (!noCache) {
await fastify.prisma.artist.create({
data: {
...data,
artistBio: {
@ -110,7 +111,9 @@ const artist: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
},
},
})
.catch(e => console.error(e))
}
return data
})
}

View file

@ -0,0 +1,30 @@
import { FastifyPluginAsync } from 'fastify'
import appleMusicRequest from '../../utils/appleMusicRequest'
type ResponseSchema = {
status: 'OK' | 'Expired'
}
const album: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify.get('/check-token', opts, async function (request, reply): Promise<
ResponseSchema | undefined
> {
const result = await appleMusicRequest({
method: 'GET',
url: '/search',
params: {
term: `Taylor Swift evermore`,
types: 'albums',
'fields[albums]': 'artistName,artwork,name,copyright,editorialVideo,editorialNotes',
limit: '1',
l: 'en-us',
},
})
return {
status: result?.results?.album ? 'OK' : 'Expired',
}
})
}
export default album