This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
class PostImporter
|
||||
def initialize provider:
|
||||
@provider = provider
|
||||
end
|
||||
|
||||
def import_posts source_videos
|
||||
alive_codes = []
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
source_videos.each do |source_video|
|
||||
alive_codes << source_video.fetch(:code)
|
||||
|
||||
deerjikist_tag = deerjikist_tag_of(source_video)
|
||||
video = upsert_post(source_video, deerjikist_tag)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :provider
|
||||
|
||||
def deerjikist_tag_of source_video
|
||||
user_code = source_video[:user_code]
|
||||
return nil if user_code.blank?
|
||||
|
||||
deerjikist&.tag
|
||||
end
|
||||
|
||||
def upsert_post source_video, deerjikist_tag
|
||||
url =
|
||||
case provider
|
||||
when 'youtube'
|
||||
"https://youtube.com/watch/#{ source_video.fetch(:code) }"
|
||||
end
|
||||
raise 'ちんぽ!' if url.blank?
|
||||
|
||||
Post.find_or_initialize_by(url:).tap do |post|
|
||||
post.title = source_viedo.fetch(:title)
|
||||
post.uploaded_user_id = nil
|
||||
post.original_created_from = source_video.fetch(:uploaded_at)
|
||||
post.original_created_before = post.original_created_from + 1.min
|
||||
post.tags << (deerjikist_tag || no_deerjikist)
|
||||
post.tags << Tag.youtube
|
||||
post.tags << Tag.video
|
||||
post.tags << Tag.bot
|
||||
post.tags << Tag.tagme
|
||||
post.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,87 +0,0 @@
|
||||
module VideoSources
|
||||
module Youtube
|
||||
class Client
|
||||
API_BASE = 'https://www.googleapis.com/youtube/v3'
|
||||
|
||||
def initialize api_key: ENV.fetch('YOUTUBE_API_KEY')
|
||||
@api_key = api_key
|
||||
end
|
||||
|
||||
def videos video_ids
|
||||
return [] if video_ids.empty?
|
||||
|
||||
response = connection.get('videos', part: 'snippet,statistics',
|
||||
id: video_ids.join(','),
|
||||
key: @api_key)
|
||||
|
||||
JSON.parse(response.body).fetch('items', []).map do |item|
|
||||
build_video(item)
|
||||
end
|
||||
end
|
||||
|
||||
def comments video_id
|
||||
comments = []
|
||||
page_token = nil
|
||||
|
||||
loop do
|
||||
response = connection.get('commentThreads', {
|
||||
part: 'snippet',
|
||||
videoId: video_id,
|
||||
maxResults: 100,
|
||||
textFormat: 'plainText',
|
||||
pageToken: page_token,
|
||||
key: @api_key }.compact)
|
||||
|
||||
body = JSON.parse(response.body)
|
||||
|
||||
comments.concat(body.fetch('items', []).map { |item| build_comment(item) })
|
||||
|
||||
page_token = body['nextPageToken']
|
||||
break if page_token.blank?
|
||||
end
|
||||
|
||||
comments
|
||||
rescue Faraday::ForbiddenError
|
||||
[]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def connection
|
||||
@connection ||= Faraday.new(url: API_BASE) do |faraday|
|
||||
faraday.response :raise_error
|
||||
end
|
||||
end
|
||||
|
||||
def build_video item
|
||||
snippet = item.fetch('snippet')
|
||||
statistics = item.fetch('statistics', { })
|
||||
|
||||
{ provider: 'youtube',
|
||||
code: item.fetch('id'),
|
||||
user_code: snippet['channelId'],
|
||||
title: snippet['title'].to_s,
|
||||
description: snippet['description'].to_s,
|
||||
tag_names: snippet.fetch('tags', []),
|
||||
views_count: statistics.fetch('viewCount', 0).to_i,
|
||||
uploaded_at: Time.zone.parse(snippet.fetch('publishedAt')) }
|
||||
end
|
||||
|
||||
def build_comment item
|
||||
snippet =
|
||||
item
|
||||
.fetch('snippet')
|
||||
.fetch('topLevelComment')
|
||||
.fetch('snippet')
|
||||
|
||||
{ provider_comment_id: item.fetch('id'),
|
||||
user_code: snippet['authorChannelId']&.fetch('value', nil),
|
||||
content: snippet['textDisplay'].to_s,
|
||||
posted_at: Time.zone.parse(snippet.fetch('publishedAt')),
|
||||
reaction_count: snippet.fetch('likeCount', 0).to_i,
|
||||
comment_no: nil,
|
||||
vpos_ms: nil }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -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