Module: VideoInfo::Providers::YoutubeScraper

Defined in:
lib/video_info/providers/youtube_scraper.rb

Constant Summary collapse

BASE_URL =
"https://www.youtube.com"
CHANNEL_URL =
"#{BASE_URL}/channel/"
THUMB_DEFAULT_SIZE =
88

Instance Method Summary collapse

Instance Method Details

#authorObject



21
22
23
# File 'lib/video_info/providers/youtube_scraper.rb', line 21

def author
  meta_node_value(channel_meta_nodes, "og:title")
end

#author_thumbnailObject



25
26
27
28
29
# File 'lib/video_info/providers/youtube_scraper.rb', line 25

def author_thumbnail
  image_hq_url = meta_node_value(channel_meta_nodes, "og:image")

  resize_thumb(image_hq_url, THUMB_DEFAULT_SIZE)
end

#author_urlObject



39
40
41
# File 'lib/video_info/providers/youtube_scraper.rb', line 39

def author_url
  URI.join(CHANNEL_URL, channel_id).to_s
end

#available?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/video_info/providers/youtube_scraper.rb', line 11

def available?
  !!title
end

#channel_idObject



31
32
33
34
35
36
37
# File 'lib/video_info/providers/youtube_scraper.rb', line 31

def channel_id
  # NOTE the previous implementation, based on itemprop_node_value("channelId"),
  # does not work well, probably due to some YouTube change
  page_content = data.to_s
  matches = page_content.match(/"channelId":"([a-zA-Z0-9_-]+)"/)
  matches[1] if matches
end

#dateObject



15
16
17
18
19
# File 'lib/video_info/providers/youtube_scraper.rb', line 15

def date
  date = itemprop_node_value("datePublished")

  Time.parse(date) if date
end

#descriptionObject



43
44
45
# File 'lib/video_info/providers/youtube_scraper.rb', line 43

def description
  meta_node_value(video_meta_nodes, "og:description")
end

#durationObject



47
48
49
50
51
52
53
54
55
# File 'lib/video_info/providers/youtube_scraper.rb', line 47

def duration
  duration = itemprop_node_value("duration")

  if duration
    ISO8601::Duration.new(duration).to_seconds.to_i
  else
    0
  end
end

#keywordsObject



57
58
59
60
61
62
63
# File 'lib/video_info/providers/youtube_scraper.rb', line 57

def keywords
  return unless available?

  value = meta_node_value(video_meta_nodes, "keywords")

  value&.split(", ")
end

#statsObject



74
75
76
77
78
# File 'lib/video_info/providers/youtube_scraper.rb', line 74

def stats
  {
    "viewCount" => view_count
  }
end

#titleObject



65
66
67
68
# File 'lib/video_info/providers/youtube_scraper.rb', line 65

def title
  return if meta_node_value(video_meta_nodes, "title").empty?
  meta_node_value(video_meta_nodes, "title")
end

#view_countObject



70
71
72
# File 'lib/video_info/providers/youtube_scraper.rb', line 70

def view_count
  itemprop_node_value("interactionCount").to_i
end