11const axios = require ( 'axios' ) ;
2- const JSSoup = require ( 'jssoup' ) . default ;
2+ const cheerio = require ( 'cheerio' ) ;
33const { search, Command } = require ( '../utils' ) ;
44
55const STRINGS = {
@@ -12,19 +12,21 @@ const STRINGS = {
1212 } ) ,
1313} ;
1414
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+ }
1822}
1923
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 ( ) ;
2830}
2931
3032class Lyric {
@@ -47,16 +49,15 @@ class Lyric {
4749
4850 const results = await search ( text , 'https://www.letras.mus.br' ) ;
4951 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 } _` ) ;
6061 }
6162}
6263
0 commit comments