Class: JarvisbotSongfinder::YoutubeAPI

Inherits:
Provider
  • Object
show all
Defined in:
lib/jarvisbot_songfinder/providers/youtube_api.rb

Constant Summary collapse

ID_REGEX =
/\A((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu\.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?/.freeze
URL_REGEX =
/youtube\.com|youtu\.be/.freeze

Instance Attribute Summary

Attributes inherited from Provider

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Provider

#valid?

Constructor Details

#initialize(url, config: JarvisbotSongfinder.configuration) ⇒ YoutubeAPI

Returns a new instance of YoutubeAPI.



9
10
11
12
13
14
15
16
# File 'lib/jarvisbot_songfinder/providers/youtube_api.rb', line 9

def initialize(url, config: JarvisbotSongfinder.configuration)
  super()
  @config = config
  @track_id = get_track_id(url)
  @track = Yt::Video.new(id: @track_id) # unless @track_id.nil?
  check_id_validity
  valid? unless @errors.any?
end

Class Method Details

.from_search(query, config: JarvisbotSongfinder.configuration) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jarvisbot_songfinder/providers/youtube_api.rb', line 18

def self.from_search(query, config: JarvisbotSongfinder.configuration)
  @config = config
  videos = Yt::Collections::Videos.new
  params = { q: query, order: "relevance", video_category_id: 10 }
  if track_id = videos.where(params)&.first&.id
    link = "https://www.youtube.com/watch?v=#{track_id}"
    return new(link)
  else
    return nil
  end
end

Instance Method Details

#artistObject



38
39
40
# File 'lib/jarvisbot_songfinder/providers/youtube_api.rb', line 38

def artist
  @track.channel_title
end

#explicit?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/jarvisbot_songfinder/providers/youtube_api.rb', line 50

def explicit?
  # no way to know
  false
end

#lengthObject



34
35
36
# File 'lib/jarvisbot_songfinder/providers/youtube_api.rb', line 34

def length
  @track.duration
end

#providerObject



46
47
48
# File 'lib/jarvisbot_songfinder/providers/youtube_api.rb', line 46

def provider
  "youtube"
end

#titleObject



30
31
32
# File 'lib/jarvisbot_songfinder/providers/youtube_api.rb', line 30

def title
  @track.title
end

#urlObject



42
43
44
# File 'lib/jarvisbot_songfinder/providers/youtube_api.rb', line 42

def url
  "https://www.youtube.com/watch?v=#{@track_id}"
end