Created
December 16, 2025 11:33
-
-
Save ONLym22/3221ad2b9b38d086ef3f9bdedb70d316 to your computer and use it in GitHub Desktop.
ONLym BOT Gist Uploaded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const axios = require("axios"); | |
| const { wrapper } = require("axios-cookiejar-support"); | |
| const { CookieJar } = require("tough-cookie"); | |
| async function Spotify(input) { | |
| try { | |
| let metadata; | |
| let trackId; | |
| if (input.includes('open.spotify.com/track/')) { | |
| trackId = extractTrackId(input); | |
| if (!trackId) throw new Error("URL Spotify tidak valid"); | |
| const searchResults = await spotdownSearch(`https://open.spotify.com/track/${trackId}`); | |
| if (searchResults.error || !searchResults.songs || searchResults.songs.length === 0) { | |
| throw new Error("Tidak dapat menemukan informasi lagu"); | |
| } | |
| const trackInfo = searchResults.songs[0]; | |
| metadata = { | |
| title: trackInfo.title, | |
| artist: trackInfo.artist, | |
| album: trackInfo.album, | |
| duration: trackInfo.duration, | |
| thumbnail: trackInfo.thumbnail, | |
| trackId: trackId | |
| }; | |
| } else { | |
| const searchResults = await spotdownSearch(input); | |
| if (searchResults.error || !searchResults.songs || searchResults.songs.length === 0) { | |
| throw new Error("Tidak ditemukan hasil search"); | |
| } | |
| const firstResult = searchResults.songs[0]; | |
| trackId = extractTrackId(firstResult.url); | |
| metadata = { | |
| title: firstResult.title, | |
| artist: firstResult.artist, | |
| album: firstResult.album, | |
| duration: firstResult.duration, | |
| thumbnail: firstResult.thumbnail, | |
| trackId: trackId | |
| }; | |
| } | |
| if (!trackId) throw new Error("Gagal mendapatkan track ID"); | |
| const downloadUrl = await generateDownloadUrl(trackId, metadata.title, metadata.artist); | |
| if (!downloadUrl) { | |
| throw new Error("Gagal generate URL download"); | |
| } | |
| return { | |
| metadata: metadata, | |
| downloadUrl: downloadUrl, | |
| download: [ | |
| { | |
| quality: "HQ", | |
| url: downloadUrl, | |
| format: "mp3", | |
| duration: metadata.duration | |
| } | |
| ] | |
| }; | |
| } catch (err) { | |
| throw new Error(err.message); | |
| } | |
| } | |
| function extractTrackId(url) { | |
| const match = url.match(/track\/([a-zA-Z0-9]{22})/); | |
| return match ? match[1] : null; | |
| } | |
| async function generateDownloadUrl(trackId, title, artist) { | |
| try { | |
| const encodedTitle = encodeURIComponent(title); | |
| const encodedArtist = encodeURIComponent(artist); | |
| const endpoints = [ | |
| `https://spotify-downloader-api-4v3t.onrender.com/download/${trackId}?filename=${encodedTitle}`, | |
| `https://api.spotifydown.com/download/${trackId}`, | |
| `https://spotify-downloader-ten.vercel.app/api/download?url=https://open.spotify.com/track/${trackId}` | |
| ]; | |
| for (const endpoint of endpoints) { | |
| try { | |
| const testResponse = await axios.head(endpoint, { timeout: 5000 }); | |
| if (testResponse.status === 200) { | |
| return endpoint; | |
| } | |
| } catch (e) { | |
| continue; | |
| } | |
| } | |
| return null; | |
| } catch (error) { | |
| throw new Error(`Gagal generate URL download: ${error.message}`); | |
| } | |
| } | |
| async function spotdownSearch(query) { | |
| try { | |
| const jar = new CookieJar(); | |
| const client = wrapper(axios.create({ | |
| jar: jar, | |
| withCredentials: true | |
| })); | |
| await client.get('https://spotdown.org/', { | |
| headers: { | |
| 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36' | |
| } | |
| }); | |
| const response = await client.get('https://spotdown.org/api/song-details', { | |
| params: { | |
| url: query | |
| }, | |
| headers: { | |
| 'authority': 'spotdown.org', | |
| 'accept': 'application/json, text/plain, */*', | |
| 'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7', | |
| 'referer': 'https://spotdown.org/', | |
| 'sec-ch-ua': '"Chromium";v="137", "Not/A)Brand";v="24"', | |
| 'sec-ch-ua-mobile': '?1', | |
| 'sec-ch-ua-platform': '"Android"', | |
| 'sec-fetch-dest': 'empty', | |
| 'sec-fetch-mode': 'cors', | |
| 'sec-fetch-site': 'same-origin', | |
| 'user-agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36' | |
| } | |
| }); | |
| return response.data; | |
| } catch (error) { | |
| return { error: true, message: error.message }; | |
| } | |
| } | |
| function formatDuration(seconds) { | |
| if (!seconds) return 'Unknown'; | |
| if (typeof seconds === 'string') return seconds; | |
| const mins = Math.floor(seconds / 60); | |
| const secs = seconds % 60; | |
| return `${mins}:${secs.toString().padStart(2, '0')}`; | |
| } | |
| module.exports = { | |
| Spotify, | |
| formatDuration, | |
| extractTrackId | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment