Class: Youtube::VideoFetcher

Inherits:
Object
  • Object
show all
Defined in:
app/my_lib/youtube/video_fetcher.rb

Constant Summary collapse

BASE_URL =
'https://www.googleapis.com/youtube/v3'.freeze
CACHE_EXPIRATION =
24.hours
REDIS_EXPIRATION_IN_SECONDS =

1 hour

3600

Instance Method Summary collapse

Constructor Details

#initializeVideoFetcher

Returns a new instance of VideoFetcher.



9
10
11
# File 'app/my_lib/youtube/video_fetcher.rb', line 9

def initialize
  @api_key = Figaro.env.YOUTUBE_API_KEY!
end

Instance Method Details

#fetch_json_ld_schema(video_url) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/my_lib/youtube/video_fetcher.rb', line 58

def fetch_json_ld_schema(video_url)
  return {} if video_url.blank?

  video_object = fetch_video_details(video_url)
  return {} if video_object.blank?

  {
    "@context": 'https://schema.org',
    "@type": 'VideoObject',
    name: video_object[:title],
    description: video_object[:description],
    uploadDate: video_object[:published_at],
    thumbnailUrl: video_object[:thumbnail_url],
    contentUrl: video_object[:contentUrl],
    embedUrl: video_object[:embedUrl],
    duration: video_object[:duration],
  }
rescue StandardError => e
  HH_LOGGER.error("Error fetching JSON-LD schema for #{video_url}: #{e.message}")
  {}
end

#fetch_video_details(video_url) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/my_lib/youtube/video_fetcher.rb', line 13

def fetch_video_details(video_url)
  video_id = extract_video_id(video_url)
  return {} if video_id.blank?

  cached_video = Rails.cache.read(cache_key(video_id))
  cached_timestamp = Rails.cache.read(timestamp_cache_key(video_id))

  if cached_video && cached_timestamp && !stale?(cached_timestamp)
    return cached_video
  end

  redis_data = redis_get(video_id)
  if redis_data.present?
    # Update Rails cache
    Rails.cache.write(cache_key(video_id), redis_data, expires_in: CACHE_EXPIRATION)
    Rails.cache.write(timestamp_cache_key(video_id), Time.current_time, expires_in: CACHE_EXPIRATION)
    return redis_data
  end

  # Ref: https://developers.google.com/youtube/v3/docs/videos/list
  response = DefaultFaradayClient.create_faraday_connection.get(
    "#{BASE_URL}/videos", {
      id: video_id,
      part: 'snippet,contentDetails',
      key: @api_key,
    }
  )

  video_details = parse_response(video_id, response)
  if video_details.present?
    # Update both caches
    Rails.cache.write(cache_key(video_id), video_details, expires_in: CACHE_EXPIRATION)
    Rails.cache.write(timestamp_cache_key(video_id), Time.current_time, expires_in: CACHE_EXPIRATION)
    redis_set(video_id, video_details)
  end

  video_details
rescue Faraday::TimeoutError, Faraday::ConnectionFailed => e
  APMErrorHandler.report("Youtube API: Timeout error occurred while fetching #{video_url}: #{e.message}")
  {}
rescue StandardError => e
  HH_LOGGER.error("Error fetching video details for #{video_url}: #{e.message}")
  {}
end