Browse Source

#314

feature/314
みてるぞ 4 days ago
parent
commit
6ee621e565
3 changed files with 31 additions and 6 deletions
  1. +8
    -3
      backend/app/services/youtube/api_client.rb
  2. +20
    -3
      backend/app/services/youtube/sync.rb
  3. +3
    -0
      backend/app/services/youtube/video_item.rb

+ 8
- 3
backend/app/services/youtube/api_client.rb View File

@@ -11,14 +11,17 @@ module Youtube
@api_key = api_key @api_key = api_key
end end


def search_videos q:, published_after:, page_token: nil
def search_videos q:, published_after: nil, published_before: nil, page_token: nil
get_json('/search', { get_json('/search', {
part: 'snippet', part: 'snippet',
type: 'video', type: 'video',
q: q,
q:,
order: 'date', order: 'date',
maxResults: 50, maxResults: 50,
publishedAfter: published_after.iso8601,
regionCode: 'JP',
relevanceLanguage: 'ja',
publishedAfter: published_after&.iso8601,
publishedBefore: published_before&.iso8601,
pageToken: page_token }.compact) pageToken: page_token }.compact)
end end


@@ -37,6 +40,8 @@ module Youtube
end end


def channel id: nil, handle: nil def channel id: nil, handle: nil
raise ArgumentError, 'id or handle is required' if id.present? == handle.present?

params = { part: 'snippet,contentDetails' } params = { part: 'snippet,contentDetails' }
params[:id] = id if id params[:id] = id if id
params[:forHandle] = handle if handle params[:forHandle] = handle if handle


+ 20
- 3
backend/app/services/youtube/sync.rb View File

@@ -35,9 +35,10 @@ module Youtube
end end


playlist_ids.each do |playlist_id| playlist_ids.each do |playlist_id|
response = @client.playlist_items(playlist_id:)
response.fetch('items', []).each do |item|
each_playlist_item(playlist_id) do |item|
video_id = item.dig('contentDetails', 'videoId') video_id = item.dig('contentDetails', 'videoId')
video_id ||= item.dig('snippet', 'resourceId', 'videoId')

ids << video_id if video_id.present? ids << video_id if video_id.present?
end end
end end
@@ -70,7 +71,7 @@ module Youtube
title: video.title, title: video.title,
url: video.url, url: video.url,
thumbnail_base: video.thumbnail_url, thumbnail_base: video.thumbnail_url,
uploaded_user: nil,
uploaded_user_id: nil,
original_created_from:, original_created_from:,
original_created_before:) original_created_before:)


@@ -84,6 +85,7 @@ module Youtube


deerjikist = Deerjikist.find_by(platform: :youtube, code: video.channel_id) deerjikist = Deerjikist.find_by(platform: :youtube, code: video.channel_id)
if deerjikist if deerjikist
desired_tag_ids.delete(Tag.no_deerjikist.id)
desired_tag_ids << deerjikist.tag_id desired_tag_ids << deerjikist.tag_id
elsif post.tags.where(category: :deerjikist).none? elsif post.tags.where(category: :deerjikist).none?
desired_tag_ids << Tag.no_deerjikist.id desired_tag_ids << Tag.no_deerjikist.id
@@ -147,5 +149,20 @@ module Youtube
end end


def sync_since = 14.days.ago def sync_since = 14.days.ago

def each_playlist_item playlist_id
page_token = nil

loop do
response = @client.playlist_items(playlist_id:, page_token:)

response.fetch('items', []).each do |item|
yield item
end

page_token = response['nextPageToken']
break if page_token.blank?
end
end
end end
end end

+ 3
- 0
backend/app/services/youtube/video_item.rb View File

@@ -1,3 +1,6 @@
require 'time'


module Youtube module Youtube
class VideoItem class VideoItem
attr_reader :id, :title, :channel_id, :published_at, :thumbnail_url, :raw_tags attr_reader :id, :title, :channel_id, :published_at, :thumbnail_url, :raw_tags


Loading…
Cancel
Save