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

146 lines
5.0 KiB

  1. namespace :nico do
  2. desc 'ニコニコ DB 同期'
  3. task sync: :environment do
  4. require 'json'
  5. require 'nokogiri'
  6. require 'open-uri'
  7. require 'open3'
  8. require 'set'
  9. require 'time'
  10. fetch_thumbnail = -> url do
  11. html = URI.open(url, read_timeout: 60, 'User-Agent' => 'Mozilla/5.0').read
  12. doc = Nokogiri::HTML(html)
  13. doc.at('meta[name="thumbnail"]')&.[]('content').presence
  14. end
  15. def sync_post_tags! post, desired_tag_ids, current_tag_ids: nil
  16. current_tag_ids ||= PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set
  17. desired_tag_ids = desired_tag_ids.compact.to_set
  18. to_add = desired_tag_ids - current_tag_ids
  19. to_remove = current_tag_ids - desired_tag_ids
  20. Tag.where(id: to_add.to_a).find_each do |tag|
  21. begin
  22. PostTag.create!(post:, tag:)
  23. rescue ActiveRecord::RecordNotUnique
  24. ;
  25. end
  26. end
  27. PostTag.where(post_id: post.id, tag_id: to_remove.to_a).kept.find_each do |pt|
  28. pt.discard_by!(nil)
  29. end
  30. end
  31. mysql_user = ENV['MYSQL_USER']
  32. mysql_pass = ENV['MYSQL_PASS']
  33. nizika_nico_path = ENV['NIZIKA_NICO_PATH']
  34. stdout, stderr, status = Open3.capture3(
  35. { 'MYSQL_USER' => mysql_user, 'MYSQL_PASS' => mysql_pass },
  36. 'python3', "#{ nizika_nico_path }/get_videos.py")
  37. unless status.success?
  38. warn stderr
  39. abort
  40. end
  41. data = JSON.parse(stdout)
  42. data.each do |datum|
  43. code = datum['code']
  44. post =
  45. Post
  46. .where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)")
  47. .first
  48. title = datum['title']
  49. original_created_at = datum['uploaded_at'] &&
  50. Time.strptime(datum['uploaded_at'], '%Y-%m-%d %H:%M:%S')
  51. original_created_from = original_created_at&.change(sec: 0)
  52. original_created_before = original_created_from&.+(1.minute)
  53. if post
  54. attrs = { title:, original_created_from:, original_created_before: }
  55. unless post.thumbnail.attached?
  56. thumbnail_base = fetch_thumbnail.(post.url) rescue nil
  57. if thumbnail_base.present?
  58. post.thumbnail.attach(
  59. io: URI.open(thumbnail_base),
  60. filename: File.basename(URI.parse(thumbnail_base).path),
  61. content_type: 'image/jpeg')
  62. attrs[:thumbnail_base] = thumbnail_base
  63. end
  64. end
  65. post.assign_attributes(attrs)
  66. if post.changed?
  67. post.save!
  68. post.resized_thumbnail! if post.thumbnail.attached?
  69. end
  70. else
  71. url = "https://www.nicovideo.jp/watch/#{ code }"
  72. thumbnail_base = fetch_thumbnail.(url) rescue nil
  73. post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil,
  74. original_created_from:, original_created_before:)
  75. if thumbnail_base.present?
  76. post.thumbnail.attach(
  77. io: URI.open(thumbnail_base),
  78. filename: File.basename(URI.parse(thumbnail_base).path),
  79. content_type: 'image/jpeg')
  80. end
  81. post.save!
  82. post.resized_thumbnail!
  83. sync_post_tags!(post, [Tag.tagme.id, Tag.bot.id, Tag.niconico.id, Tag.video.id])
  84. end
  85. tags = post.tags
  86. # 既存のタグ Id. 集合
  87. kept_tag_ids = tags.pluck(:id).to_set
  88. # うち内部タグ Id. 集合
  89. kept_non_nico_tag_ids = tags.not_nico.pluck(:id).to_set
  90. # 記載すべき外部タグ Id. および連携される内部タグ Id. のリスト
  91. desired_nico_tag_based_ids = []
  92. # 記載すべき内部タグ Id. のリスト
  93. desired_non_nico_tag_ids = []
  94. datum['tags'].each do |raw|
  95. name = "nico:#{ raw }"
  96. tag = Tag.find_or_create_by_tag_name!(name, category: :nico)
  97. desired_nico_tag_based_ids << tag.id
  98. # 新たに記載される外部タグと連携される内部タグを記載
  99. unless tag.id.in?(kept_tag_ids)
  100. linked_ids = tag.linked_tags.pluck(:id)
  101. desired_non_nico_tag_ids.concat(linked_ids)
  102. desired_nico_tag_based_ids.concat(linked_ids)
  103. end
  104. end
  105. deerjikist = Deerjikist.find_by(platform: :nico, code: datum['user'])
  106. if deerjikist
  107. desired_non_nico_tag_ids << deerjikist.tag_id
  108. desired_nico_tag_based_ids << deerjikist.tag_id
  109. elsif !(Tag.where(id: kept_non_nico_tag_ids).where(category: :deerjikist).exists?)
  110. desired_non_nico_tag_ids << Tag.no_deerjikist.id
  111. desired_nico_tag_based_ids << Tag.no_deerjikist.id
  112. end
  113. desired_nico_tag_based_ids.uniq!
  114. desired_all_tag_ids = kept_non_nico_tag_ids.to_a + desired_nico_tag_based_ids
  115. desired_non_nico_tag_ids.concat(kept_non_nico_tag_ids.to_a)
  116. desired_non_nico_tag_ids.uniq!
  117. if kept_non_nico_tag_ids != desired_non_nico_tag_ids.to_set
  118. desired_all_tag_ids << Tag.bot.id
  119. end
  120. desired_all_tag_ids.uniq!
  121. sync_post_tags!(post, desired_all_tag_ids, current_tag_ids: kept_tag_ids)
  122. end
  123. end
  124. end