class YoutubeParser::Channels::AboutSection

Public Instance Methods

info() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 8
def info
  {
    title: title,
    email: email,
    description: description,
    country: country,
    tags: tags,
    views_count: views_count,
    followers_count: followers_count,
    videos_count: videos_count,
    avatar_url: avatar_url
  }
end

Private Instance Methods

avatar_url() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 47
def avatar_url
  results = json.dig(*keys.meta_avatar) || section.dig(*keys.avatar)
  return if results.nil? || results.empty?

  results.detect { |t| t&.dig('url') }&.dig('url')
end
country() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 39
def country
  section.dig(*keys.country)
end
description() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 33
def description
  descriptions = [section.dig(*keys.description_first),
                  section.dig(*keys.description_second)]
  descriptions.compact.join(' ')
end
email() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 28
def email
  regex = /#{URI::MailTo::EMAIL_REGEXP.source.gsub(/\\A|\\z/, '')}/
  description[regex]
end
followers_count() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 61
def followers_count
  count = scrape_subscribers_count
  count.present? ? count : statistics(:followers_count)
end
json() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 109
def json
  @json ||= client.get("#{options.channel_url}/about")
end
scrape_subscribers_count() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 77
def scrape_subscribers_count
  return unless subscribers_match

  number = subscribers_match[:number].gsub(',', '.').to_f
  return number.to_i unless subscribers_match[:units].present?

  subscribers_with_units number
end
section() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 100
def section
  return @section if @section

  @section = json.dig(*keys.section_tabs)&.detect do |tab|
    tab.dig(*keys.about_section_tab)
  end
  @section = @section&.dig(*keys.about_section_tab) || {}
end
statistics(title) click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 70
def statistics(title)
  indexes = { videos_count: 1, views_count: 2, followers_count: 3 }
  stats = section.dig(*keys.statistics) || []

  stats[indexes[title.to_sym].to_i].to_s.gsub(/\D+/, '')
end
subscribers_match() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 90
def subscribers_match
  regex = /(?<number>[\d.,]+)(?<units>\w?)/
  subscribers_text = json.dig(*keys.subscribers_count_text).to_s
  subscribers_text.match(regex)
end
subscribers_with_units(number) click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 86
def subscribers_with_units(number)
  (number * units[subscribers_match[:units].downcase.to_sym]).to_i
end
tags() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 43
def tags
  json.dig(*keys.keywords).to_s.split('"').select(&:present?)
end
title() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 24
def title
  section.dig(*keys.title)
end
units() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 96
def units
  { m: 1_000_000, k: 1_000 }
end
videos_count() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 66
def videos_count
  statistics :videos_count
end
views_count() click to toggle source
# File lib/youtube_parser/channels/about_section.rb, line 54
def views_count
  views_str = section.dig(*keys.views)&.map(&:values)&.join
  return unless views_str

  (views_str.gsub(/\D+/, '') || statistics(:views_count))
end