Class: SL::YouTubeSearch

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

Overview

YouTube Search/Linking

Constant Summary collapse

YOUTUBE_RX =
%r{(?:youtu\.be/|youtube\.com/watch\?v=)?(?<id>[a-z0-9_\-]+)$}i.freeze

Class Method Summary collapse

Class Method Details

.embed_for_url(url) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/searchlink/searches/youtube.rb', line 33

def embed_for_url(url)
  return unless url =~ YOUTUBE_RX

  id = Regexp.last_match("id")
  title = [
    %(<iframe width="560" height="315" src="https://www.youtube.com/embed/#{id}"),
    %(title="YouTube video player" frameborder="0"),
    %(allow="accelerometer; autoplay; clipboard-write; encrypted-media;),
    %(gyroscope; picture-in-picture; web-share"),
    %(allowfullscreen></iframe>)
  ].join(" ")
  ["embed", title]
end

.search(search_type, search_terms, link_text) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/searchlink/searches/youtube.rb', line 19

def search(search_type, search_terms, link_text)
  if SL::URL.url?(search_terms) && search_terms =~ YOUTUBE_RX
    url = search_terms
  elsif search_terms =~ /^[a-z0-9_\-]+$/i
    url = "https://youtube.com/watch?v=#{search_terms}"
  else
    url, title = SL.ddg("site:youtube.com #{search_terms}", link_text)
  end

  url, title = embed_for_url(url) if search_type =~ /e$/

  [url, title]
end

.settingsObject



9
10
11
12
13
14
15
16
17
# File 'lib/searchlink/searches/youtube.rb', line 9

def settings
  {
    trigger: "yte?",
    searches: [
      ["yt", "YouTube Search"],
      ["yte", "YouTube Embed"]
    ]
  }
end