HerzaJ commited on
Commit
dfcebae
·
verified ·
1 Parent(s): cd0eb4f

Create lyrics.js

Browse files
Files changed (1) hide show
  1. plugins/lyrics.js +102 -0
plugins/lyrics.js ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require("axios")
2
+ const cheerio = require("cheerio")
3
+
4
+ async function getLyrics(query) {
5
+ try {
6
+ const searchUrl = `https://www.azlyrics.com/search/?q=${encodeURIComponent(query)}&x=6c789b703562209942b3c6f9a5b5a50bbcc3196a2727629f11784e520faeec36`
7
+
8
+ const searchResponse = await axios.get(searchUrl, {
9
+ headers: {
10
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
11
+ }
12
+ })
13
+
14
+ const $ = cheerio.load(searchResponse.data)
15
+ const link = $('a[href*="/lyrics/"]').first()
16
+
17
+ if (!link.length || !link.attr("href")) {
18
+ throw new Error("Lagu tidak ditemukan")
19
+ }
20
+
21
+ const songUrl = link.attr("href")
22
+ const title = link.text().split('"')[1]
23
+ const artist = link.text().split(" - ")[1].split("...")[0].trim()
24
+
25
+ await new Promise(resolve => setTimeout(resolve, 1000))
26
+
27
+ const pageResponse = await axios.get(songUrl, {
28
+ headers: {
29
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
30
+ }
31
+ })
32
+
33
+ const $$ = cheerio.load(pageResponse.data)
34
+
35
+ const ringtoneDiv = $$('.ringtone').first()
36
+ if (!ringtoneDiv.length) {
37
+ throw new Error("Struktur halaman tidak sesuai")
38
+ }
39
+
40
+ const lyricsDiv = ringtoneDiv.nextAll('div').filter((i, el) => {
41
+ const html = $$(el).html()
42
+ return html && html.includes('Usage of azlyrics.com content') && !$$(el).attr('class')
43
+ }).first()
44
+
45
+ if (!lyricsDiv.length) {
46
+ throw new Error("Lirik tidak ditemukan")
47
+ }
48
+
49
+ const rawHtml = lyricsDiv.html()
50
+ const cleanedHtml = rawHtml.replace(/<!--[\s\S]*?-->/g, '')
51
+ const textWithBreaks = cleanedHtml.replace(/<br\s*\/?>/gi, '\n')
52
+ const cleanText = textWithBreaks.replace(/<[^>]*>/g, '').trim()
53
+
54
+ return {
55
+ title,
56
+ artist,
57
+ lyrics: cleanText,
58
+ url: songUrl
59
+ }
60
+
61
+ } catch (error) {
62
+ throw error
63
+ }
64
+ }
65
+
66
+ const handler = async (req, res) => {
67
+ try {
68
+ const { query } = req.query
69
+
70
+ if (!query) {
71
+ return res.status(400).json({
72
+ success: false,
73
+ error: 'Missing required parameter: query'
74
+ })
75
+ }
76
+
77
+ const result = await getLyrics(query)
78
+ res.json({
79
+ author: "Herza",
80
+ success: true,
81
+ data: result
82
+ })
83
+
84
+ } catch (error) {
85
+ res.status(500).json({
86
+ success: false,
87
+ error: error.message
88
+ })
89
+ }
90
+ }
91
+
92
+ module.exports = {
93
+ name: 'Search Lyrics',
94
+ description: 'Search and get song lyrics from AZLyrics',
95
+ type: 'GET',
96
+ routes: ['api/search/lyrics'],
97
+ tags: ['search', 'music', 'lyrics'],
98
+ parameters: ['query', 'key'],
99
+ enabled: true,
100
+ main: ['Search'],
101
+ handler
102
+ }