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

126 lines
4.1 KiB

  1. namespace :nico do
  2. desc 'ニコニコ DB 同期'
  3. task sync: :environment do
  4. require 'json'
  5. require 'open3'
  6. require 'open-uri'
  7. require 'nokogiri'
  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_ids: nil
  16. current_ids ||= PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set
  17. desired_ids = desired_tag_ids.compact.to_set
  18. to_add = desired_ids - current_ids
  19. to_remove = current_ids - desired_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. abort unless status.success?
  38. data = JSON.parse(stdout)
  39. data.each do |datum|
  40. code = datum['code']
  41. post =
  42. Post
  43. .where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)")
  44. .first
  45. title = datum['title']
  46. original_created_at = datum['uploaded_at'] &&
  47. Time.strptime(datum['uploaded_at'], '%Y-%m-%d %H:%M:%S')
  48. original_created_from = original_created_at&.change(sec: 0)
  49. original_created_before = original_created_from&.+(1.minute)
  50. if post
  51. attrs = { title:, original_created_from:, original_created_before: }
  52. unless post.thumbnail.attached?
  53. thumbnail_base = fetch_thumbnail.(post.url) rescue nil
  54. if thumbnail_base.present?
  55. post.thumbnail.attach(
  56. io: URI.open(thumbnail_base),
  57. filename: File.basename(URI.parse(thumbnail_base).path),
  58. content_type: 'image/jpeg')
  59. attrs[:thumbnail_base] = thumbnail_base
  60. end
  61. end
  62. post.assign_attributes(attrs)
  63. if post.changed?
  64. post.save!
  65. post.resized_thumbnail! if post.thumbnail.attached?
  66. end
  67. else
  68. url = "https://www.nicovideo.jp/watch/#{ code }"
  69. thumbnail_base = fetch_thumbnail.(url) rescue nil
  70. post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil,
  71. original_created_from:, original_created_before:)
  72. if thumbnail_base.present?
  73. post.thumbnail.attach(
  74. io: URI.open(thumbnail_base),
  75. filename: File.basename(URI.parse(thumbnail_base).path),
  76. content_type: 'image/jpeg')
  77. end
  78. post.save!
  79. post.resized_thumbnail!
  80. sync_post_tags!(post, [Tag.tagme.id])
  81. end
  82. kept_ids = PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set
  83. kept_non_nico_ids = post.tags.where.not(category: 'nico').pluck(:id).to_set
  84. desired_nico_ids = []
  85. desired_non_nico_ids = []
  86. datum['tags'].each do |raw|
  87. name = "nico:#{ raw }"
  88. tag = Tag.find_or_create_by_tag_name!(name, category: 'nico')
  89. desired_nico_ids << tag.id
  90. unless tag.id.in?(kept_ids)
  91. linked_ids = tag.linked_tags.pluck(:id)
  92. desired_non_nico_ids.concat(linked_ids)
  93. desired_nico_ids.concat(linked_ids)
  94. end
  95. end
  96. desired_nico_ids.uniq!
  97. desired_all_ids = kept_non_nico_ids.to_a + desired_nico_ids
  98. desired_non_nico_ids.concat(kept_non_nico_ids.to_a)
  99. desired_non_nico_ids.uniq!
  100. if kept_non_nico_ids.to_set != desired_non_nico_ids.to_set
  101. desired_all_ids << Tag.bot.id
  102. end
  103. desired_all_ids.uniq!
  104. sync_post_tags!(post, desired_all_ids, current_ids: kept_ids)
  105. end
  106. end
  107. end