ilhamdev commited on
Commit
b408541
·
verified ·
1 Parent(s): b479895

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +159 -0
index.js CHANGED
@@ -1493,6 +1493,165 @@ result : {name: file?.name,
1493
  code: 500
1494
  });
1495
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1496
  })
1497
  .use((req, res, next) => {
1498
  res.redirect('/');
 
1493
  code: 500
1494
  });
1495
  }
1496
+ })
1497
+ .get('/spotifySearch', async (req, res) => {
1498
+ try {
1499
+ async function spotifySearch(query) {
1500
+ let acc = [
1501
+ {
1502
+ id: "e22d2dda8830401b88e4aebaf1327d21",
1503
+ secret: "37d4c71ee45a47c1baccec2ae6f789d6",
1504
+ },
1505
+ {
1506
+ id: "2a16c7e8b59a459c91e0f6e5f734dbad",
1507
+ secret: "b000f142441545ceb2a831c341145c3d",
1508
+ },
1509
+ {
1510
+ id: "189a1643acfa4fd3b9a665a84d2f9b8f",
1511
+ secret: "bf82850195df4066b652801e262238cd",
1512
+ },
1513
+ {
1514
+ id: "6430383e20eb4bccac72961146c55240",
1515
+ secret: "d46acaa4104f4016b3a228206497fc32",
1516
+ },
1517
+ {
1518
+ id: "7e15fc4078014b45954582148be5bc85",
1519
+ secret: "55a465fa161f4f24bc9f0ef7384d4b96",
1520
+ },
1521
+ ];
1522
+ acc = acc[Math.floor(Math.random() * acc.length)];
1523
+
1524
+ async function getToken() {
1525
+ try {
1526
+ const response = await axios.post(
1527
+ "https://accounts.spotify.com/api/token",
1528
+ new URLSearchParams({
1529
+ grant_type: "client_credentials",
1530
+ client_id: acc?.id,
1531
+ client_secret: acc?.secret,
1532
+ }),
1533
+ {
1534
+ headers: {
1535
+ "Content-Type": "application/x-www-form-urlencoded",
1536
+ },
1537
+ }
1538
+ );
1539
+ return response.data.access_token;
1540
+ } catch (error) {
1541
+ return null;
1542
+ }
1543
+ }
1544
+
1545
+ const token = await getToken();
1546
+ if (!token) {
1547
+ return [];
1548
+ }
1549
+
1550
+ async function searchSpotifyTrack(query, token) {
1551
+ const url = "https://api.spotify.com/v1/search";
1552
+ const config = {
1553
+ headers: {
1554
+ Authorization: `Bearer ${token}`,
1555
+ },
1556
+ params: {
1557
+ q: query,
1558
+ type: "track",
1559
+ market: "ID",
1560
+ include_external: "audio",
1561
+ },
1562
+ };
1563
+
1564
+ try {
1565
+ const response = await axios.get(url, config);
1566
+ const tracks = response.data.tracks.items;
1567
+ const trackInfo = tracks.map((track) => ({
1568
+ name: track.name,
1569
+ artist: track.artists.map((artist) => artist.name).join(", "),
1570
+ album: track.album.name,
1571
+ id: track.album.id,
1572
+ release_date: track.album.release_date,
1573
+ url: track.external_urls.spotify,
1574
+ popularity: track.popularity,
1575
+ preview_url: track.preview_url,
1576
+ duration_ms: track.duration_ms,
1577
+ explicit: track.explicit,
1578
+ thumbnail: track.album.images[0] ? track.album.images[0].url : null,
1579
+ track_number: track.track_number,
1580
+ total_tracks: track.album.total_tracks,
1581
+ is_local: track.is_local,
1582
+ }));
1583
+
1584
+ return trackInfo;
1585
+ } catch (error) {
1586
+ console.error(
1587
+ "Error searching Spotify track:",
1588
+ error.response ? error.response.data : error.message
1589
+ );
1590
+ return [];
1591
+ }
1592
+ }
1593
+
1594
+ async function getSpotifyInfoFromURL(url, token) {
1595
+ try {
1596
+ const spotifyId = url.split("/").pop().split("?")[0];
1597
+ if (!spotifyId) return [];
1598
+ const response = await axios.get(
1599
+ `https://api.spotify.com/v1/tracks/${spotifyId}`,
1600
+ {
1601
+ headers: {
1602
+ Authorization: `Bearer ${token}`,
1603
+ },
1604
+ }
1605
+ );
1606
+
1607
+ const track = response.data;
1608
+ return [
1609
+ {
1610
+ name: track.name,
1611
+ artist: track.artists.map((artist) => artist.name).join(", "),
1612
+ album: track.album.name,
1613
+ id: track.album.id,
1614
+ release_date: track.album.release_date,
1615
+ url: track.external_urls.spotify,
1616
+ popularity: track.popularity,
1617
+ preview_url: track.preview_url,
1618
+ duration_ms: track.duration_ms,
1619
+ explicit: track.explicit,
1620
+ thumbnail: track.album.images[0] ? track.album.images[0].url : null,
1621
+ track_number: track.track_number,
1622
+ total_tracks: track.album.total_tracks,
1623
+ is_local: track.is_local,
1624
+ },
1625
+ ];
1626
+ } catch (error) {
1627
+ console.error(
1628
+ "Error fetching Spotify info from URL:",
1629
+ error.response ? error.response.data : error.message
1630
+ );
1631
+ return [];
1632
+ }
1633
+ }
1634
+
1635
+ let results = [];
1636
+
1637
+ if (query.startsWith("https://open.spotify.com/track/")) {
1638
+ results = await getSpotifyInfoFromURL(query, token);
1639
+ } else {
1640
+ results = await searchSpotifyTrack(query, token);
1641
+ }
1642
+
1643
+ return results;
1644
+ }
1645
+ let q = req.query.q || req.query.query;
1646
+ if (!q) return res.json({ message: 'Input parameter q' });
1647
+ let get = await spotifySearch(q)
1648
+ return res.json({message: "Success", code: 200, data: get})
1649
+ } catch (e) {
1650
+ console.log(e);
1651
+ return res.json({ message: "Internal Server Error",
1652
+ code: 500
1653
+ });
1654
+ }
1655
  })
1656
  .use((req, res, next) => {
1657
  res.redirect('/');