ぼざクリタグ広場 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.
 
 
 
 
 
 

96 lines
3.0 KiB

  1. namespace :nico do
  2. desc 'ニコニコ DB 同期'
  3. task sync: :environment do
  4. require 'open3'
  5. require 'open-uri'
  6. require 'nokogiri'
  7. fetch_thumbnail = -> url do
  8. html = URI.open(url, read_timeout: 60, 'User-Agent' => 'Mozilla/5.0').read
  9. doc = Nokogiri::HTML(html)
  10. doc.at('meta[name="thumbnail"]')&.[]('content').presence
  11. end
  12. def sync_post_tags! post, desired_tag_ids
  13. desired_ids = desired_tag_ids.compact.to_set
  14. current_ids = post.tags.pluck(:id).to_set
  15. to_add = desired_ids - current_ids
  16. to_remove = current_ids - desired_ids
  17. Tag.where(id: to_add.to_a).find_each do |tag|
  18. begin
  19. PostTag.create!(post:, tag:)
  20. rescue ActiveRecord::RecordNotUnique
  21. ;
  22. end
  23. end
  24. PostTag.where(post_id: post.id, tag_id: to_remove.to_a).kept.find_each do |pt|
  25. pt.discard_by!(nil)
  26. end
  27. end
  28. mysql_user = ENV['MYSQL_USER']
  29. mysql_pass = ENV['MYSQL_PASS']
  30. nizika_nico_path = ENV['NIZIKA_NICO_PATH']
  31. stdout, stderr, status = Open3.capture3(
  32. { 'MYSQL_USER' => mysql_user, 'MYSQL_PASS' => mysql_pass },
  33. 'python3', "#{ nizika_nico_path }/get_videos.py")
  34. abort unless status.success?
  35. data = JSON.parse(stdout)
  36. data.each do |datum|
  37. post = Post.where('url LIKE ?', '%nicovideo.jp%').find { |post|
  38. post.url =~ %r{#{ Regexp.escape(datum['code']) }(?!\d)}
  39. }
  40. unless post
  41. title = datum['title']
  42. url = "https://www.nicovideo.jp/watch/#{ datum['code'] }"
  43. thumbnail_base = fetch_thumbnail.(url) || '' rescue ''
  44. post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil)
  45. if thumbnail_base.present?
  46. post.thumbnail.attach(
  47. io: URI.open(thumbnail_base),
  48. filename: File.basename(URI.parse(thumbnail_base).path),
  49. content_type: 'image/jpeg')
  50. end
  51. post.save!
  52. post.resized_thumbnail!
  53. sync_post_tags!(post, [Tag.tagme.id])
  54. end
  55. kept_tags = post.tags.reload
  56. kept_non_nico_ids = kept_tags.where.not(category: 'nico').pluck(:id).to_set
  57. desired_nico_ids = []
  58. desired_non_nico_ids = []
  59. datum['tags'].each do |raw|
  60. name = "nico:#{ raw }"
  61. tag = Tag.find_or_initialize_by(name:) do |t|
  62. t.category = 'nico'
  63. end
  64. tag.save! if tag.new_record?
  65. desired_nico_ids << tag.id
  66. unless tag.in?(kept_tags)
  67. desired_non_nico_ids.concat(tag.linked_tags.pluck(:id))
  68. desired_nico_ids.concat(tag.linked_tags.pluck(:id))
  69. end
  70. end
  71. desired_nico_ids.uniq!
  72. desired_all_ids = kept_non_nico_ids.to_a + desired_nico_ids
  73. desired_non_nico_ids.concat(kept_non_nico_ids.to_a)
  74. desired_non_nico_ids.uniq!
  75. if kept_non_nico_ids.to_set != desired_non_nico_ids.to_set
  76. desired_all_ids << Tag.bot.id
  77. end
  78. desired_all_ids.uniq!
  79. sync_post_tags!(post, desired_all_ids)
  80. end
  81. end
  82. end