class JarvisbotSongfinder::YoutubeAPI

Constants

ID_REGEX
URL_REGEX

Public Class Methods

new(url, config: JarvisbotSongfinder.configuration) click to toggle source
Calls superclass method JarvisbotSongfinder::Provider::new
# 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

Public Instance Methods

artist() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 38
def artist
  @track.channel_title
end
explicit?() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 50
def explicit?
  # no way to know
  false
end
length() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 34
def length
  @track.duration
end
provider() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 46
def provider
  "youtube"
end
title() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 30
def title
  @track.title
end
url() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 42
def url
  "https://www.youtube.com/watch?v=#{@track_id}"
end

Private Instance Methods

available_in_region?() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 83
def available_in_region?
  return true if !listed? || in_allowed_list? || !in_blocked_list?
  add_error ReplyMessage::Request.region_restricted(@region)
  return false
end
check_id_validity() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 67
def check_id_validity
  @track.empty?
rescue Yt::Errors::NoItems => e
  add_error ReplyMessage::Request.invalid_video_id
end
get_track_id(url) click to toggle source

:nocov:

# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 58
def get_track_id(url)
  if !ID_REGEX.match(url)
    add_error ReplyMessage::Request.invalid_video_id
    nil
  else
    ID_REGEX.match(url)[5]
  end
end
in_allowed_list?() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 93
def in_allowed_list?
  if allowed_list = @track.content_detail.data&.dig('regionRestriction', 'allowed')
    allowed_list.include?(@config.region)
  else
    false
  end
end
in_blocked_list?() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 101
def in_blocked_list?
  if blocked_list = @track.content_detail.data&.dig('regionRestriction', 'blocked')
    blocked_list.include?(@config.region)
  else
    false
  end
end
in_music_category?() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 74
def in_music_category?
  if @track.category_id == "10"
    true
  else
    add_error ReplyMessage::Request.invalid_category
    false
  end
end
listed?() click to toggle source
# File lib/jarvisbot_songfinder/providers/youtube_api.rb, line 89
def listed?
  @track.content_detail.data&.dig('regionRestriction')
end