This commit is contained in:
@@ -25,7 +25,7 @@ module Youtube
|
||||
def videos ids
|
||||
return { 'items' => [] } if ids.empty?
|
||||
|
||||
get_json('/videos', part: 'snippet,status,contentDetails', id: ids.join (','))
|
||||
get_json('/videos', part: 'snippet,status,contentDetails', id: ids.join(','))
|
||||
end
|
||||
|
||||
def playlist_items playlist_id:, page_token: nil
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
module Youtube
|
||||
class SearchClient
|
||||
API_BASE = 'https://www.googleapis.com/youtube/v3'
|
||||
|
||||
def initialize api_key: ENV.fetch('YOUTUBE_API_KEY')
|
||||
@api_key = api_key
|
||||
end
|
||||
|
||||
def search_videos query:, published_after: nil, published_before: nil, page_token: nil
|
||||
response = connection.get('search', {
|
||||
part: 'snippet',
|
||||
q: query,
|
||||
type: 'video',
|
||||
order: 'date',
|
||||
maxResults: 50,
|
||||
regionCode: 'JP',
|
||||
relevanceLanguage: 'ja',
|
||||
publishedAfter: published_after&.iso8601,
|
||||
publishedBefore: published_before&.iso8601,
|
||||
pageToken: page_token,
|
||||
key: @api_key }.compact)
|
||||
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def connection
|
||||
@connection ||= Faraday.new(url: API_BASE) do |faraday|
|
||||
faraday.response :raise_error
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -15,7 +15,7 @@ module Youtube
|
||||
|
||||
video_ids.each_slice(50) do |ids|
|
||||
@client.videos(ids).fetch('items', []).each do |item|
|
||||
sync_video!(VideoItem.new (item))
|
||||
sync_video!(VideoItem.new(item))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -55,11 +55,10 @@ module Youtube
|
||||
post_changed = false
|
||||
|
||||
if post
|
||||
post.assign_attributes(
|
||||
title: video.title,
|
||||
original_created_from:,
|
||||
original_created_before:,
|
||||
thumbnail_base: video.thumbnail_url)
|
||||
post.assign_attributes(title: video.title,
|
||||
original_created_from:,
|
||||
original_created_before:,
|
||||
thumbnail_base: video.thumbnail_url)
|
||||
|
||||
post_changed = post.changed?
|
||||
post.save! if post_changed
|
||||
@@ -68,10 +67,10 @@ module Youtube
|
||||
else
|
||||
post_created = true
|
||||
post = Post.create!(
|
||||
title: video.title,
|
||||
url: video.url,
|
||||
thumbnail_base: video.thumbnail_url,
|
||||
uploaded_user: nil,
|
||||
title: video.title,
|
||||
url: video.url,
|
||||
thumbnail_base: video.thumbnail_url,
|
||||
uploaded_user: nil,
|
||||
original_created_from:,
|
||||
original_created_before:)
|
||||
|
||||
@@ -127,8 +126,8 @@ module Youtube
|
||||
return if thumbnail_url.blank?
|
||||
|
||||
post.thumbnail.attach(
|
||||
io: URI.open (thumbnail_url),
|
||||
filename: File.basename (URI.parse (thumbnail_url).path),
|
||||
io: URI.open(thumbnail_url),
|
||||
filename: File.basename(URI.parse(thumbnail_url).path),
|
||||
content_type: 'image/jpeg')
|
||||
|
||||
post.resized_thumbnail!
|
||||
|
||||
Reference in New Issue
Block a user