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

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