Skip to content

Commit

Permalink
feat: added cron job for spotify sync to apple-music
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthikg committed Jan 26, 2025
1 parent c2e2ea1 commit e62330c
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ SMTP_HOST="mailcatcher"
SMTP_PORT="1025"
SMTP_SSL=""

FROM_ADDRESS="[email protected]"
FROM_ADDRESS="[email protected]"

MUSIC_TOKEN=""
2 changes: 2 additions & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface ImportMetaEnv {
readonly SMTP_PORT: string;

readonly FROM_ADDRESS: string;

readonly MUSIC_TOKEN: string;
}

interface ImportMeta {
Expand Down
108 changes: 108 additions & 0 deletions src/pages/api/music/_constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
export interface SpotifyPlaylist {
name: string;
id: string;
updateFrequency: "daily" | "weekly";
}

export const spotifyPlaylists: SpotifyPlaylist[] = [
// Made for You
{
name: "Daily Mix 1",
id: "37i9dQZF1E376QwloseAfr",
updateFrequency: "daily",
},
{
name: "Daily Mix 2",
id: "37i9dQZF1E34T72I0DkmUy",
updateFrequency: "daily",
},
{
name: "Daily Mix 3",
id: "37i9dQZF1E39QHotjSEEJ0",
updateFrequency: "daily",
},
{
name: "Daily Mix 4",
id: "37i9dQZF1E37s9s5M96nRp",
updateFrequency: "daily",
},
{
name: "Daily Mix 5",
id: "37i9dQZF1E35g41kUcLxdn",
updateFrequency: "daily",
},
{
name: "Daily Mix 6",
id: "37i9dQZF1E39gipmPQ7hgQ",
updateFrequency: "daily",
},
{
name: "Discover Weekly",
id: "37i9dQZEVXcLG1H4N5dmtG",
updateFrequency: "weekly",
},
{
name: "Release Radar",
id: "37i9dQZEVXbhEnWhE7TIAb",
updateFrequency: "weekly",
},
// Punjabi
{
name: "Punjabi Pyar",
id: "37i9dQZF1DWSKoG4oVafMt",
updateFrequency: "weekly",
},
{
name: "Punjabi 101",
id: "37i9dQZF1DX5cZuAHLNjGz",
updateFrequency: "weekly",
},
{
name: "Punjabi Pop",
id: "37i9dQZF1DWWwF1YkSKLlU",
updateFrequency: "weekly",
},
{
name: "New Music Friday Punjabi",
id: "37i9dQZF1DX5baCFRgbF3x",
updateFrequency: "weekly",
},
// Hindi
{
name: "Bollywood Acoustic",
id: "37i9dQZF1DWSwxyU5zGZYe",
updateFrequency: "weekly",
},
{
name: "Radar India",
id: "37i9dQZF1DWTAtTdFMiJYK",
updateFrequency: "weekly",
},
{
name: "New Music Hindi",
id: "37i9dQZF1DXd8cOUiye1o2",
updateFrequency: "weekly",
},
// Hindi Indies
{
name: "hIndies",
id: "37i9dQZF1DWXSzFkaLsPkN",
updateFrequency: "weekly",
},
{
name: "Indie India",
id: "37i9dQZF1DX5q67ZpWyRrZ",
updateFrequency: "weekly",
},
// Global
{
name: "Top 50 - Global",
id: "37i9dQZEVXbMDoHDwVN2tF",
updateFrequency: "daily",
},
{
name: "Viral 50 - Global",
id: "37i9dQZEVXbLiRSasKsNU9",
updateFrequency: "daily",
},
];
19 changes: 19 additions & 0 deletions src/pages/api/music/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { spotifyPlaylists } from "./_constants";

export async function GET(): Promise<Response> {
spotifyPlaylists.forEach(async (playlist) => {
const urlSearchParams = new URLSearchParams({
playlistId: playlist.id,
});
console.log("Converting playlist:", playlist.name);
const response = await fetch(
`${import.meta.env.SITE}/api/music/sync?${urlSearchParams.toString()}`,
);
if (response.status === 200) {
console.log("Converted playlist:", playlist.name);
} else {
console.log("Failed to convert playlist:", playlist.name);
}
});
return new Response("Submitted!", { status: 200 });
}
26 changes: 26 additions & 0 deletions src/pages/api/music/sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { APIContext, Props } from "astro";

export async function GET({ request }: APIContext<Props>): Promise<Response> {
try {
const playlistId = new URL(request.url).searchParams.get("playlistId");

if (!playlistId) {
return new Response("Missing playlistId", { status: 400 });
}

await fetch("https://playlistor.io/playlist", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Music-User-Token": import.meta.env.MUSIC_TOKEN,
},
body: JSON.stringify({
platform: "apple-music",
playlist: `https://open.spotify.com/playlist/${playlistId}`,
}),
});
} catch (error) {
return new Response("Something went wrong!", { status: 500 });
}
return new Response("Submitted!", { status: 200 });
}
8 changes: 8 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"crons": [
{
"path": "/api/music",
"schedule": "0 6 * * *"
}
]
}

0 comments on commit e62330c

Please sign in to comment.