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

101 lines
3.1 KiB

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