1
1
const axios = require ( 'axios' ) ;
2
- const JSSoup = require ( 'jssoup' ) . default ;
2
+ const cheerio = require ( 'cheerio' ) ;
3
3
const { search, Command } = require ( '../utils' ) ;
4
4
5
5
const STRINGS = {
@@ -12,19 +12,21 @@ const STRINGS = {
12
12
} ) ,
13
13
} ;
14
14
15
- async function makeSoup ( url ) {
16
- const { data } = await axios . get ( url ) ;
17
- return new JSSoup ( data ) ;
15
+ async function loadCheerio ( url ) {
16
+ try {
17
+ const { data } = await axios . get ( url ) ;
18
+ return cheerio . load ( data ) ;
19
+ } catch {
20
+ return null ;
21
+ }
18
22
}
19
23
20
- function removeBr ( raw ) {
21
- const html = raw
22
- . prettify ( )
23
- . split ( '\n' )
24
- . filter ( ( e ) => ! e . match ( / < b r [ \s ] * [ \/ ] ? > / ) )
25
- . map ( ( e ) => e . trim ( ) )
26
- . join ( '\n' ) ;
27
- return new JSSoup ( html ) ;
24
+ function removeTags ( raw ) {
25
+ const html = cheerio
26
+ . load ( raw )
27
+ . html ( )
28
+ . replace ( / < b r [ \s ] * [ / ] ? > / g, '\n' ) ;
29
+ return cheerio . load ( html ) . text ( ) ;
28
30
}
29
31
30
32
class Lyric {
@@ -47,16 +49,15 @@ class Lyric {
47
49
48
50
const results = await search ( text , 'https://www.letras.mus.br' ) ;
49
51
const { link } = results [ 0 ] ;
50
- const soup = await makeSoup ( link ) ;
51
- const title = soup . find ( 'div' , { class : 'cnt-head_title' } ) . find ( 'h1' ) ;
52
- const lyrics = soup
53
- . find ( 'div' , { class : 'cnt-letra' } )
54
- . findAll ( 'p' )
55
- . map ( ( p ) => removeBr ( p ) . text )
56
- . join ( '' ) ;
57
- const output = `*${ title . text } *\n\n${ lyrics } \n\n_${ link } _` ;
58
-
59
- message . reply ( output ) ;
52
+ const $ = await loadCheerio ( link ) ;
53
+ const title = $ ( 'div.cnt-head_title h1' ) . text ( ) ;
54
+ let lyrics = [ ] ;
55
+ $ ( 'div.cnt-letra p' ) . each ( ( _ , p ) => {
56
+ lyrics . push ( removeTags ( p ) ) ;
57
+ } ) ;
58
+ lyrics = lyrics . join ( '\n\n' ) ;
59
+
60
+ message . reply ( `*${ title } *\n\n${ lyrics } \n\n_${ link } _` ) ;
60
61
}
61
62
}
62
63
0 commit comments