Class: SL::LyricsSearch
- Inherits:
-
Object
- Object
- SL::LyricsSearch
- Defined in:
- lib/searchlink/searches/lyrics.rb
Overview
Give it a unique class name
Class Method Summary collapse
-
.get_lyrics(url) ⇒ Object
Any additional helper methods can be defined after #search.
-
.search(search_type, search_terms, link_text) ⇒ Object
Every plugin must contain a #search method that takes 3 arguments:.
-
.settings ⇒ Object
Settings block is required with ‘trigger` and `searches`.
Class Method Details
.get_lyrics(url) ⇒ Object
Any additional helper methods can be defined after #search
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/searchlink/searches/lyrics.rb', line 63 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 matches = body.scan(%r{class="Lyrics__Container-.*?>(.*?)</div><div class="LyricsFooter}) lyrics = matches.join("\n") if lyrics "```\n#{CGI.unescape(lyrics).gsub(%r{<br/?>}, " \n").gsub(%r{</?.*?>}, '').gsub(/'/, "'")}\n```" else false end 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.
31 32 33 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 |
# File 'lib/searchlink/searches/lyrics.rb', line 31 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 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 |
.settings ⇒ Object
Settings block is required with ‘trigger` and `searches`
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/searchlink/searches/lyrics.rb', line 7 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?', # 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'] ] } end |