HerzaJ commited on
Commit
6a26940
·
verified ·
1 Parent(s): 24275e3

Update plugins/lyrics.js

Browse files
Files changed (1) hide show
  1. plugins/lyrics.js +16 -183
plugins/lyrics.js CHANGED
@@ -1,207 +1,40 @@
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
- await new Promise(resolve => setTimeout(resolve, 500))
9
-
10
- const searchResponse = await axios.get(searchUrl, {
11
- headers: {
12
- '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',
13
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
14
- 'Accept-Language': 'en-US,en;q=0.5',
15
- 'Referer': 'https://www.azlyrics.com/'
16
- },
17
- timeout: 10000
18
- })
19
-
20
- const $ = cheerio.load(searchResponse.data)
21
-
22
- let songLink = null
23
- let title = ''
24
- let artist = ''
25
-
26
- const songResults = $('.table.table-condensed tbody tr')
27
- if (songResults.length > 0) {
28
- const firstResult = songResults.first()
29
- const link = firstResult.find('a').first()
30
- if (link.length && link.attr('href')) {
31
- songLink = link.attr('href')
32
- const linkText = link.text()
33
- const resultText = firstResult.text()
34
-
35
- if (linkText.includes('"')) {
36
- const parts = linkText.split('"')
37
- title = parts[1] || parts[0]
38
- } else {
39
- title = linkText
40
- }
41
-
42
- const artistMatch = resultText.match(/^(.*?)\s*"/)
43
- if (artistMatch) {
44
- artist = artistMatch[1].trim()
45
- }
46
- }
47
- }
48
-
49
- if (!songLink) {
50
- const panelLinks = $('.panel a[href*="/lyrics/"]')
51
- if (panelLinks.length > 0) {
52
- const link = panelLinks.first()
53
- songLink = link.attr('href')
54
- const linkText = link.text()
55
- if (linkText.includes('"')) {
56
- const parts = linkText.split('"')
57
- title = parts[1] || linkText
58
- } else {
59
- title = linkText
60
- }
61
- }
62
- }
63
-
64
- if (!songLink) {
65
- const directLink = $('a[href*="/lyrics/"]').first()
66
- if (directLink.length && directLink.attr('href')) {
67
- songLink = directLink.attr('href')
68
- const linkText = directLink.text()
69
- if (linkText.includes('"')) {
70
- const parts = linkText.split('"')
71
- title = parts[1] || linkText
72
- } else {
73
- title = linkText
74
- }
75
- }
76
- }
77
-
78
- if (!songLink) {
79
- throw new Error(`Lagu "${query}" tidak ditemukan. Coba kata kunci yang lebih spesifik.`)
80
- }
81
-
82
- const fullUrl = songLink.startsWith('http') ? songLink : `https://www.azlyrics.com${songLink}`
83
-
84
- await new Promise(resolve => setTimeout(resolve, 1000))
85
-
86
- const pageResponse = await axios.get(fullUrl, {
87
- headers: {
88
- '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',
89
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
90
- 'Referer': searchUrl
91
- },
92
- timeout: 10000
93
- })
94
-
95
- const $$ = cheerio.load(pageResponse.data)
96
-
97
- if (!title) {
98
- const pageTitle = $$('title').text()
99
- const match = pageTitle.match(/(.+?)\s*-\s*(.+?)\s*Lyrics/i)
100
- if (match) {
101
- artist = match[1].trim()
102
- title = match[2].trim()
103
- }
104
- }
105
-
106
- let lyricsText = ''
107
-
108
- const ringtoneDiv = $$('.ringtone').first()
109
- if (ringtoneDiv.length) {
110
- const lyricsDiv = ringtoneDiv.nextAll('div').filter((i, el) => {
111
- return !$$(el).attr('class') && !$$(el).attr('id')
112
- }).first()
113
-
114
- if (lyricsDiv.length) {
115
- lyricsText = extractLyrics(lyricsDiv.html())
116
- }
117
- }
118
-
119
- if (!lyricsText) {
120
- const allDivs = $$('div')
121
- allDivs.each((i, el) => {
122
- const html = $$(el).html()
123
- if (html && html.includes('Usage of azlyrics.com content') && !lyricsText) {
124
- lyricsText = extractLyrics(html)
125
- }
126
- })
127
- }
128
-
129
- if (!lyricsText) {
130
- throw new Error('Lirik tidak dapat diekstrak dari halaman')
131
- }
132
-
133
- return {
134
- title: title || 'Unknown Title',
135
- artist: artist || 'Unknown Artist',
136
- lyrics: lyricsText,
137
- url: fullUrl
138
- }
139
-
140
- } catch (error) {
141
- if (error.code === 'ECONNABORTED') {
142
- throw new Error('Request timeout - server terlalu lambat merespons')
143
- }
144
- if (error.response?.status === 403) {
145
- throw new Error('Akses diblokir oleh AZLyrics. Coba lagi nanti.')
146
- }
147
- if (error.response?.status === 404) {
148
- throw new Error('Halaman tidak ditemukan')
149
- }
150
- throw error
151
- }
152
- }
153
-
154
- function extractLyrics(html) {
155
- let cleaned = html.replace(/<!--[\s\S]*?-->/g, '')
156
- cleaned = cleaned.replace(/<br\s*\/?>/gi, '\n')
157
- cleaned = cleaned.replace(/<[^>]*>/g, '')
158
- cleaned = cleaned.replace(/&amp;/g, '&')
159
- .replace(/&lt;/g, '<')
160
- .replace(/&gt;/g, '>')
161
- .replace(/&quot;/g, '"')
162
- .replace(/&#039;/g, "'")
163
- cleaned = cleaned.trim()
164
-
165
- return cleaned
166
- }
167
 
168
  const handler = async (req, res) => {
169
  try {
170
- const { query } = req.query
171
 
172
  if (!query) {
173
  return res.status(400).json({
174
  success: false,
175
- error: 'Parameter "query" wajib diisi. Contoh: ?query=savage+garden+i+knew'
176
- })
177
  }
178
 
179
- const result = await getLyrics(query)
180
-
181
  res.json({
182
  author: "Herza",
183
  success: true,
184
- data: result
185
- })
186
 
187
  } catch (error) {
188
- console.error('Error:', error.message)
189
  res.status(500).json({
190
  success: false,
191
- error: error.message,
192
- hint: 'Pastikan nama lagu dan artis benar, atau coba kata kunci yang lebih spesifik'
193
- })
194
  }
195
- }
196
 
197
  module.exports = {
198
- name: 'AZLyrics',
199
- description: 'Search and get song lyrics from AZLyrics',
200
  type: 'GET',
201
  routes: ['api/search/lyrics'],
202
- tags: ['search', 'music', 'lyrics'],
 
203
  parameters: ['query', 'key'],
204
  enabled: true,
205
- main: ['Search'],
206
  handler
207
- }
 
1
+ const axios = require('axios');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  const handler = async (req, res) => {
4
  try {
5
+ const { query } = req.query;
6
 
7
  if (!query) {
8
  return res.status(400).json({
9
  success: false,
10
+ error: 'Missing required parameter: query'
11
+ });
12
  }
13
 
14
+ let result = await axios.get(`https://lyrics.lewdhutao.my.eu.org/v2/youtube/lyrics?title=${encodeURIComponent(query)}`)
 
15
  res.json({
16
  author: "Herza",
17
  success: true,
18
+ msg: result.data.data
19
+ });
20
 
21
  } catch (error) {
 
22
  res.status(500).json({
23
  success: false,
24
+ error: error.message
25
+ });
 
26
  }
27
+ };
28
 
29
  module.exports = {
30
+ name: 'Lyrics Search',
31
+ description: 'Input song nane and system will get the lyrics for you',
32
  type: 'GET',
33
  routes: ['api/search/lyrics'],
34
+ tags: ['Youtube', 'Genius Lyrics', 'Lyrics'],
35
+ main: ['AI'],
36
  parameters: ['query', 'key'],
37
  enabled: true,
38
+ limit: 5,
39
  handler
40
+ };