Skip to content

Commit

Permalink
♻️ refactor: apply fixes from codefactor
Browse files Browse the repository at this point in the history
Apply fixes from CodeFactor
  • Loading branch information
kms0219kms authored Nov 11, 2023
2 parents 669c045 + eccd2cc commit 027dfc5
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 128 deletions.
2 changes: 1 addition & 1 deletion src/functions/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function errorLog(
content += `오류 페이지: ${page}\n`

await axios.post(webhook, {
content: content,
content,
})
}

Expand Down
8 changes: 4 additions & 4 deletions src/functions/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ function readDeepDir(distPath: string) {
...fs
.readdirSync(distPath)
.filter(file =>
fs.statSync(distPath + '/' + file).isDirectory(),
fs.statSync(`${distPath}/${file}`).isDirectory(),
)
.reduce((all: string[], subDir: string) => {
return [
...all,
...fs
.readdirSync(distPath + '/' + subDir)
.map(e => subDir + '/' + e),
.readdirSync(`${distPath}/${subDir}`)
.map(e => `${subDir}/${e}`),
]
}, []),
...fs
.readdirSync(distPath)
.filter(
file => !fs.statSync(distPath + '/' + file).isDirectory(),
file => !fs.statSync(`${distPath}/${file}`).isDirectory(),
),
]
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const app = new Koa()
const router = new Router()

const accessLogStream = fs.createWriteStream(
__dirname + '/logs/access.log',
`${__dirname}/logs/access.log`,
{ flags: 'a' },
)

Expand Down Expand Up @@ -66,7 +66,7 @@ app.use(async (ctx, next) => {
console.log('------------------------------------')

for (const i in routers) {
let pathName = '/' + routers[i]
const pathName = `/${routers[i]}`

try {
router.use(
Expand Down
4 changes: 2 additions & 2 deletions src/routers/charts/daily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ async function getDetailData(): Promise<any> {
const previousRanking: IChartCalculation[] = []

statistics.forEach(x => {
let find = thisWeek.find(
y => y.videos.video == 'https://youtu.be/' + x.id,
const find = thisWeek.find(
y => y.videos.video == `https://youtu.be/${x.id}`,
)

if (find) {
Expand Down
4 changes: 2 additions & 2 deletions src/routers/charts/realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ async function getDetailData(): Promise<any> {
const previousRanking: IChartCalculation[] = []

statistics.forEach(x => {
let find = thisWeek.find(
y => y.videos.video == 'https://youtu.be/' + x.id,
const find = thisWeek.find(
y => y.videos.video == `https://youtu.be/${x.id}`,
)

if (find) {
Expand Down
4 changes: 2 additions & 2 deletions src/routers/charts/total.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ async function getDetailData(): Promise<any> {
const previousRanking: IChartCalculation[] = []

statistics.forEach(x => {
let find = thisWeek.find(
y => y.videos.video == 'https://youtu.be/' + x.id,
const find = thisWeek.find(
y => y.videos.video == `https://youtu.be/${x.id}`,
)

if (find) {
Expand Down
4 changes: 2 additions & 2 deletions src/routers/charts/weekly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ async function getDetailData(): Promise<any> {
const previousRanking: IChartCalculation[] = []

statistics.forEach(x => {
let find = thisWeek.find(
y => y.videos.video == 'https://youtu.be/' + x.id,
const find = thisWeek.find(
y => y.videos.video == `https://youtu.be/${x.id}`,
)

if (find) {
Expand Down
99 changes: 52 additions & 47 deletions src/routers/music/artist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ async function getDetailData(): Promise<any[]> {
await ThisWeek.find(),
)
return searchResult.sort(
(a, b) => new Date(b.uploadDate).getTime() - new Date(a.uploadDate).getTime(),
(a, b) =>
new Date(b.uploadDate).getTime() -
new Date(a.uploadDate).getTime(),
)
} catch (error: any) {
await errorLog(
Expand All @@ -46,7 +48,7 @@ router.all('/', async (ctx, next) => {

const artist: any = await Artist.find()

let musicOriginData: musicSearchResult[] = await getDetailData()
const musicOriginData: musicSearchResult[] = await getDetailData()

const artistData: artistSearchResult[] = artist.filter(
(x: any) =>
Expand All @@ -59,57 +61,58 @@ router.all('/', async (ctx, next) => {
if (ctx.query.artist == 'isedol') {
// 이세계아이돌: 예외처리 (개인별로 등록된 음악 추가)
return (
x.artist['ine'] &&
x.artist['jingburger'] &&
x.artist['lilpa'] &&
x.artist['jururu'] &&
x.artist['gosegu'] &&
x.artist['viichan']
x.artist.ine &&
x.artist.jingburger &&
x.artist.lilpa &&
x.artist.jururu &&
x.artist.gosegu &&
x.artist.viichan
)
} else if (ctx.query.artist == 'gomem') {
}
if (ctx.query.artist == 'gomem') {
// 고멤: 예외처리 (개인별로 등록된 음악 추가)
return (
x.artist['chunyang'] &&
x.artist['chunshik'] &&
x.artist['kwonmin'] &&
x.artist['kimchimandu'] &&
x.artist['nosferatuhodd'] &&
x.artist['dandapbug'] &&
x.artist['dopamine'] &&
x.artist['dokkhye'] &&
x.artist['roentgenium'] &&
x.artist['haku'] &&
x.artist['bujungingan'] &&
x.artist['secretto'] &&
x.artist['businesskim'] &&
x.artist['friedshrimp'] &&
x.artist['sophia'] &&
x.artist['wakphago'] &&
x.artist['leedeoksoo'] &&
x.artist['carnarjungtur'] &&
x.artist['callycarly'] &&
x.artist['pungsin'] &&
x.artist['freeter'] &&
x.artist['rusuk'] &&
x.artist['hikiking']
x.artist.chunyang &&
x.artist.chunshik &&
x.artist.kwonmin &&
x.artist.kimchimandu &&
x.artist.nosferatuhodd &&
x.artist.dandapbug &&
x.artist.dopamine &&
x.artist.dokkhye &&
x.artist.roentgenium &&
x.artist.haku &&
x.artist.bujungingan &&
x.artist.secretto &&
x.artist.businesskim &&
x.artist.friedshrimp &&
x.artist.sophia &&
x.artist.wakphago &&
x.artist.leedeoksoo &&
x.artist.carnarjungtur &&
x.artist.callycarly &&
x.artist.pungsin &&
x.artist.freeter &&
x.artist.rusuk &&
x.artist.hikiking
)
} else if (ctx.query.artist == 'gomem-academy') {
}
if (ctx.query.artist == 'gomem-academy') {
// 아카데미: 예외처리 (개인별로 등록된 음악 추가)
return (
x.artist['ninnin'] &&
x.artist['ballantine'] &&
x.artist['bulgom'] &&
x.artist['jentu'] &&
x.artist['soosemi'] &&
x.artist['sirianrain'] &&
x.artist['amadeuschoi'] &&
x.artist['jinhe'] &&
x.artist['gilbert'] &&
x.artist['sullivan']
x.artist.ninnin &&
x.artist.ballantine &&
x.artist.bulgom &&
x.artist.jentu &&
x.artist.soosemi &&
x.artist.sirianrain &&
x.artist.amadeuschoi &&
x.artist.jinhe &&
x.artist.gilbert &&
x.artist.sullivan
)
} else {
return x.artist[String(ctx.query.artist)]
}
return x.artist[String(ctx.query.artist)]
})

searchResult.push(
Expand All @@ -120,13 +123,15 @@ router.all('/', async (ctx, next) => {
findGroupAliases(x.title.simple.split(' - ')[0]) ==
'isedol'
)
} else if (ctx.query.artist == 'gomem') {
}
if (ctx.query.artist == 'gomem') {
// 고멤: 예외처리 (단체로 등록된 음악 추가)
return (
findGroupAliases(x.title.simple.split(' - ')[0]) ==
'gomem'
)
} else if (ctx.query.artist == 'gomem-academy') {
}
if (ctx.query.artist == 'gomem-academy') {
// 아카데미: 예외처리 (단체로 등록된 음악 추가)
return (
findGroupAliases(x.title.simple.split(' - ')[0]) ==
Expand Down
104 changes: 50 additions & 54 deletions src/routers/music/hearts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ router.get('/', async (ctx, next) => {
status: 200,
data: userHearts.length > 0,
})
} else {
return (ctx.body = {
status: 200,
data: heartsByArtist.length,
})
}
} else if (ctx.query.video) {
return (ctx.body = {
status: 200,
data: heartsByArtist.length,
})
}
if (ctx.query.video) {
const videoId = ctx.query.video
const heartsByMusic = await Hearts.find({ music: videoId })

Expand All @@ -77,19 +77,17 @@ router.get('/', async (ctx, next) => {
status: 200,
data: userHearts.length > 0,
})
} else {
return (ctx.body = {
status: 200,
data: heartsByMusic.length,
})
}
} else {
ctx.status = 404
return (ctx.body = {
status: 404,
data: 'Not Found',
status: 200,
data: heartsByMusic.length,
})
}
ctx.status = 404
return (ctx.body = {
status: 404,
data: 'Not Found',
})
}
} catch (error) {
await errorLog(
Expand Down Expand Up @@ -133,18 +131,18 @@ router.post('/', async (ctx, next) => {
}

if (body.artist) {
const artist = body.artist
const { artist } = body
const hearts = await Hearts.findOne({
user: userData.id,
artist: artist,
artist,
})

if (hearts) {
await Hearts.deleteOne({ user: userData.id, artist })
} else {
await Hearts.create({
user: userData.id,
artist: artist,
artist,
})
}

Expand All @@ -160,51 +158,49 @@ router.post('/', async (ctx, next) => {

return (ctx.body = {
status: 200,
data: data,
data,
})
}
const videoId = body.video

if (musicIds.includes(body.video as string)) {
const hearts = await Hearts.findOne({
user: userData.id,
music: videoId,
})
} else {
const videoId = body.video

if (musicIds.includes(body.video as string)) {
const hearts = await Hearts.findOne({
if (hearts) {
await Hearts.deleteOne({
user: userData.id,
music: videoId,
})

if (hearts) {
await Hearts.deleteOne({
user: userData.id,
music: videoId,
})
} else {
await Hearts.create({
user: userData.id,
music: videoId,
})
}

const heartData: IHearts[] = loadJSON(
await Hearts.find({ music: videoId }),
)
const data = heartData.map(x => {
if (x.user !== userData.id) {
x.user = undefined
}
return x
})

return (ctx.body = {
status: 200,
data: data,
})
} else {
ctx.status = 404
return (ctx.body = {
status: 404,
data: 'Not Found',
await Hearts.create({
user: userData.id,
music: videoId,
})
}

const heartData: IHearts[] = loadJSON(
await Hearts.find({ music: videoId }),
)
const data = heartData.map(x => {
if (x.user !== userData.id) {
x.user = undefined
}
return x
})

return (ctx.body = {
status: 200,
data,
})
}
ctx.status = 404
return (ctx.body = {
status: 404,
data: 'Not Found',
})
} catch (error) {
await errorLog(
error,
Expand Down
2 changes: 1 addition & 1 deletion src/routers/music/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function getDetailData(id: string): Promise<any> {
.includes(String(data?.title).split(' - ')[1]) &&
x.id != data?.id,
)
return { data: data, covers: coversData }
return { data, covers: coversData }
} catch (error: any) {
await errorLog(error, `/api/music/${id}`, process.env.WEBHOOK_URL)
throw error
Expand Down
Loading

0 comments on commit 027dfc5

Please sign in to comment.