ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

47 lines
1.4 KiB

  1. class Discovery::YoutubeVideoDiscoverer
  2. def initialize(client: Youtube::SearchClient.new)
  3. @client = client
  4. end
  5. def call discovery_query:, published_after:, published_before:
  6. body = @client.search_videos(
  7. query: discovery_query.query,
  8. published_after: published_after,
  9. published_before: published_before)
  10. body.fetch('items', []).each do |item|
  11. next unless item.dig('id', 'kind') == 'youtube#video'
  12. upsert_candidate!(discovery_query, item)
  13. end
  14. end
  15. private
  16. def upsert_candidate! discovery_query, item
  17. snippet = item.fetch('snippet')
  18. code = item.fetch('id').fetch('videoId')
  19. candidate = VideoCandidate.find_or_initialize_by(provider: 'youtube', code:)
  20. candidate.title = snippet['title'].to_s
  21. candidate.description = snippet['description'].to_s
  22. candidate.channel_code = snippet['channelId']
  23. candidate.channel_title = snippet['channelTitle']
  24. candidate.published_at = Time.zone.parse(snippet['publishedAt'])
  25. candidate.thumbnail_url = snippet.dig('thumbnails', 'high', 'url')
  26. candidate.raw_data = item
  27. candidate.last_discovered_at = Time.current
  28. candidate.save!
  29. VideoCandidateHit.find_or_create_by!(
  30. video_candidate: candidate,
  31. discovery_query: discovery_query
  32. ) do |hit|
  33. hit.matched_field = 'youtube_search'
  34. hit.score = 0
  35. hit.searched_at = Time.current
  36. end
  37. end
  38. end