Class: SL::LyricsSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/searchlink/searches/lyrics.rb

Overview

Give it a unique class name

Class Method Summary collapse

Class Method Details

.get_lyrics(url) ⇒ Object

Any additional helper methods can be defined after #search



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/searchlink/searches/lyrics.rb', line 93

def get_lyrics(url)
  if SL::URL.valid_link?(url)
    # You can use Ruby's net/http methods for retrieving pages, but
    # `curl -SsL` is faster and easier. Curl::Html.new(url) returns a
    # new object containing :body
    body = Curl::Html.new(url).body
    title = body.match(/_sf_async_config.title = '(.*?) \| Genius Lyrics'/)[1].gsub(/\\/, '').sub(/ Lyrics$/, '')
    matches = body.scan(%r{class="Lyrics__Container-.*?>(.*?)</div><div class="LyricsFooter})

    lyrics = matches.join("\n")

    if lyrics
      lyrics = CGI.unescape(lyrics).gsub(%r{<br/?>}, "  \n").gsub(%r{</?.*?>}, '').gsub(/&#x27;/, "'")
      "#{title}\n\n#{lyrics.code_indent}\n"
    else
      false
    end
  else
    false
  end
end

.js_embed(url) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/searchlink/searches/lyrics.rb', line 74

def js_embed(url)
  if SL::URL.valid_link?(url)
    body = Curl::Html.new(url).body
    api_path = body.match(%r{\\"apiPath\\":\\"(/songs/.*?)\\"})[1]
    id = api_path.sub(/.*?(\d+)$/, '\1')
    title = body.match(/_sf_async_config.title = '(.*?) \| Genius Lyrics'/)[1]

    <<~EOEMBED
      <div id='rg_embed_link_#{id}' class='rg_embed_link' data-song-id='#{id}'>
      Read <a href='#{url}'>#{title}</a> on Genius
      </div>
      <script crossorigin src='//genius.com#{api_path}/embed.js'></script>
    EOEMBED
  else
    false
  end
end

.search(search_type, search_terms, link_text) ⇒ Object

Every plugin must contain a #search method that takes 3 arguments:

  • ‘search_type` will contain the !search trigger that was used (minus the !)

  • ‘search_terms` will include everything that came after the !search

  • ‘link_text` will contain the text that will be used for the linked

text portion of the link. This can usually remain untouched but must be passed back at the end of the function.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/searchlink/searches/lyrics.rb', line 34

def search(search_type, search_terms, link_text)
  # You can branch to multiple searches by testing the search_type
  case search_type
  when /e$/
    url, title = SL.ddg("site:genius.com #{search_terms}", link_text)
    if url
      title = get_lyrics(url)
      # To return an embed, set url (first parameter in the return
      # array) to 'embed', and put the embed contents in the second
      # parameter.
      title ? ['embed', title, link_text] : false
    else
      # Use `SL#add_error(title, text)` to add errors to the HTML
      # report. The report will only be shown if errors have been added.
      SL.add_error('No lyrics found', "Song lyrics for #{search_terms} not found")
      false
    end
  when /js$/
    url, title = SL.ddg("site:genius.com #{search_terms}", link_text)
    if url
      title = js_embed(url)
      title ? ['embed', title, link_text] : false
    else
      SL.add_error('No lyrics found', "Song lyrics for #{search_terms} not found")
      false
    end
  else
    # You can perform a DuckDuckGo search using SL#ddg, passing the
    # search terms and link_text. It will return url, title, and
    # link_text. SL#ddg will add its own errors, and if it returns false
    # that will automatically be tested for, no additional error
    # handling is required.
    url, title, link_text = SL.ddg("site:genius.com #{search_terms}", link_text)
    # Always return an array containing the resulting URL, the title,
    # and the link_text variable that was passed in, even if it's
    # unmodified.
    [url, title, link_text]
  end
end

.settingsObject

Settings block is required with ‘trigger` and `searches`



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/searchlink/searches/lyrics.rb', line 9

def settings
  {
    # `trigger` is A regular expression that will trigger this plugin
    # when used with a bang. The one below will trigger on !lyrics or
    # !lyricse.
    trigger: 'lyrics?(e|e?js)?',
    # Every search that the plugin should execute should be individually
    # listed and described in the searches array. This is used for
    # completion and help generation. Do not include the bang (!) in the
    # search keyword.
    searches: [
      ['lyric', 'Song Lyrics Search'],
      ['lyrice', 'Song Lyrics Embed'],
      ['lyricjs', 'Song Lyrics JS Embed']
    ]
  }
end