Skip to content

Commit

Permalink
fetch songs
Browse files Browse the repository at this point in the history
  • Loading branch information
idoubi committed Apr 7, 2024
1 parent b39d4b9 commit b7ff7f9
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 57 deletions.
16 changes: 14 additions & 2 deletions app/[locale]/(default)/song/[uuid]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { findByUuid, getRandomSongs } from "@/models/song";
import { findByUuid, getRandomSongs, insertRow } from "@/models/song";

import Header from "../../_components/song/header";
import List from "../../_components/song/list";
import Lyrics from "../../_components/song/lyrics";
import { formatSong } from "@/services/song";
import { getSongInfo } from "@/services/suno";
import { getTranslations } from "next-intl/server";

export default async function ({ params }: { params: { uuid: string } }) {
const t = await getTranslations("nav");
const song = await findByUuid(params.uuid);
let song = await findByUuid(params.uuid);
if (!song) {
const data = await getSongInfo([params.uuid]);
if (data && data.length > 0) {
song = formatSong(data[0], false);
if (song) {
await insertRow(song);
}
}
}

const randomSongs = await getRandomSongs(1, 10);

return (
Expand Down
32 changes: 32 additions & 0 deletions app/api/process/fetch-latest-songs/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { formatSong, updateSongs } from "@/services/song";
import { respData, respErr } from "@/utils/resp";

import { Song } from "@/types/song";
import { getLatestSongs } from "@/services/suno";

export async function POST(req: Request) {
try {
const { page } = await req.json();

let songs: Song[] = [];

const data = await getLatestSongs(page);
if (data && data["playlist_clips"]) {
data["playlist_clips"].forEach((item: any) => {
const song = formatSong(item["clip"], false);
songs.push(song);
});
}

if (songs.length === 0) {
return respErr("no data");
}

const result = await updateSongs(songs);

return respData(result);
} catch (e) {
console.log("fetch latest songs failed:", e);
return respErr("fetch latest songs failed");
}
}
33 changes: 33 additions & 0 deletions app/api/process/fetch-song/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { findByUuid, insertRow } from "@/models/song";
import { respData, respErr } from "@/utils/resp";

import { formatSong } from "@/services/song";
import { getSongInfo } from "@/services/suno";

export async function POST(req: Request) {
try {
const { uuid } = await req.json();
if (!uuid) {
return respErr("invalid params");
}

let song = await findByUuid(uuid);
if (song) {
return respData(song);
}

const data = await getSongInfo([uuid]);
if (!data || data.length === 0) {
return respErr("fetch song failed");
}

song = formatSong(data[0], false);

await insertRow(song);

return respData(song);
} catch (e) {
console.log("fetch song failed:", e);
return respErr("fetch song failed");
}
}
9 changes: 0 additions & 9 deletions app/api/process/fetch-songs/route.ts

This file was deleted.

32 changes: 32 additions & 0 deletions app/api/process/fetch-trending-songs/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { formatSong, updateSongs } from "@/services/song";
import { respData, respErr } from "@/utils/resp";

import { Song } from "@/types/song";
import { getTrendingSongs } from "@/services/suno";

export async function POST(req: Request) {
try {
const { page } = await req.json();

let songs: Song[] = [];

const data = await getTrendingSongs(page);
if (data && data["playlist_clips"]) {
data["playlist_clips"].forEach((item: any) => {
const song = formatSong(item["clip"], true);
songs.push(song);
});
}

if (songs.length === 0) {
return respErr("no data");
}

const result = await updateSongs(songs);

return respData(result);
} catch (e) {
console.log("fetch trending songs failed:", e);
return respErr("fetch trending songs failed");
}
}
51 changes: 6 additions & 45 deletions app/api/process/update-songs/route.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,16 @@
import { getUuids, insertRow } from "@/models/song";
import { getSongsFromFile, updateSongs } from "@/services/song";
import { respData, respErr } from "@/utils/resp";

import { getSongsFromFile } from "@/services/song";

export async function POST() {
try {
const uuids = await getUuids();
const allSongs = await getSongsFromFile();
const allSongsCount = allSongs.length;

console.log(
`all songs count: ${allSongsCount}, exist songs count: ${uuids.length}`
);

let existCount = 0;
let newCount = 0;
let failedCount = 0;
for (let i = 0; i < allSongsCount; i++) {
const song = allSongs[i];
if (!song.uuid) {
continue;
}

if (uuids && uuids.includes(song.uuid)) {
console.log("song exist: ", song.uuid, song.audio_url);
existCount += 1;
continue;
}

try {
await insertRow(song);
newCount += 1;
console.log(
"insert new songs: ",
song.uuid,
song.audio_url,
i,
allSongsCount - i
);
} catch (e) {
failedCount += 1;
console.log("insert song failed: ", song.uuid, song.audio_url, i, e);
}
if (!allSongs || allSongs.length === 0) {
return respErr("no data");
}

return respData({
all_count: allSongsCount,
exist_count: existCount,
new_count: newCount,
failed_count: failedCount,
});
const result = await updateSongs(allSongs);

return respData(result);
} catch (e) {
console.log("update songs failed: ", e);
return respErr("update songs failed");
Expand Down
26 changes: 25 additions & 1 deletion debug/apitest.http
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@baseUri=http://127.0.0.1:8023/api
@baseUri=http://localhost:8023/api

### update songs
POST {{baseUri}}/process/update-songs
Expand All @@ -8,6 +8,30 @@ Content-Type: application/json

}

### fetch song
POST {{baseUri}}/process/fetch-song
Content-Type: application/json

{
"uuid": "f81be5c0-3de9-4940-95e1-cfb780d3aa5e"
}

### fetch trending songs
POST {{baseUri}}/process/fetch-trending-songs
Content-Type: application/json

{
"page": 1
}

### fetch latest songs
POST {{baseUri}}/process/fetch-latest-songs
Content-Type: application/json

{
"page": 1
}

### get random songs
POST {{baseUri}}/get-random-songs
Content-Type: application/json
Expand Down
48 changes: 48 additions & 0 deletions services/song.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getLatestSongs, getTrendingSongs } from "./suno";
import { getUuids, insertRow } from "@/models/song";

import { Song } from "@/types/song";
import fs from "fs";
Expand Down Expand Up @@ -64,6 +65,53 @@ export async function getSunoLatestSongs(page: number): Promise<Song[]> {
}
}

export async function updateSongs(allSongs: Song[]) {
const uuids = await getUuids();
const allSongsCount = allSongs.length;

console.log(
`update songs count: ${allSongsCount}, exist songs count: ${uuids.length}`
);

let existCount = 0;
let newCount = 0;
let failedCount = 0;
for (let i = 0; i < allSongsCount; i++) {
const song = allSongs[i];
if (!song.uuid) {
continue;
}

if (uuids && uuids.includes(song.uuid)) {
console.log("song exist: ", song.uuid, song.audio_url);
existCount += 1;
continue;
}

try {
await insertRow(song);
newCount += 1;
console.log(
"insert new songs: ",
song.uuid,
song.audio_url,
i,
allSongsCount - i
);
} catch (e) {
failedCount += 1;
console.log("insert song failed: ", song.uuid, song.audio_url, i, e);
}
}

return {
all_count: allSongsCount,
exist_count: existCount,
new_count: newCount,
failed_count: failedCount,
};
}

export function formatSong(v: any, is_trending: boolean): Song {
const song: Song = {
uuid: v["id"],
Expand Down

0 comments on commit b7ff7f9

Please sign in to comment.