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

90 lines
2.8 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(&:discard!)
  25. end
  26. mysql_user = ENV['MYSQL_USER']
  27. mysql_pass = ENV['MYSQL_PASS']
  28. nizika_nico_path = ENV['NIZIKA_NICO_PATH']
  29. stdout, stderr, status = Open3.capture3(
  30. { 'MYSQL_USER' => mysql_user, 'MYSQL_PASS' => mysql_pass },
  31. 'python3', "#{ nizika_nico_path }/get_videos.py")
  32. abort unless status.success?
  33. data = JSON.parse(stdout)
  34. data.each do |datum|
  35. post = Post.where('url LIKE ?', '%nicovideo.jp%').find { |post|
  36. post.url =~ %r{#{ Regexp.escape(datum['code']) }(?!\d)}
  37. }
  38. unless post
  39. title = datum['title']
  40. url = "https://www.nicovideo.jp/watch/#{ datum['code'] }"
  41. thumbnail_base = fetch_thumbnail.(url) || '' rescue ''
  42. post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil)
  43. if thumbnail_base.present?
  44. post.thumbnail.attach(
  45. io: URI.open(thumbnail_base),
  46. filename: File.basename(URI.parse(thumbnail_base).path),
  47. content_type: 'image/jpeg')
  48. end
  49. post.save!
  50. post.resized_thumbnail!
  51. end
  52. kept_tags = post.tags.reload
  53. kept_non_nico_ids = kept_tags.where.not(category: 'nico').pluck(:id).to_set
  54. desired_nico_ids = []
  55. datum['tags'].each do |raw|
  56. name = "nico:#{ raw }"
  57. tag = Tag.find_or_initialize_by(name:) do |t|
  58. t.category = 'nico'
  59. end
  60. tag.save! if tag.new_record?
  61. desired_nico_ids << tag.id
  62. desired_nico_ids.concat(tag.linked_tags.pluck(:id))
  63. end
  64. desired_nico_ids.uniq!
  65. desired_extra_ids = []
  66. desired_extra_ids << Tag.tagme.id if kept_tags.size < 10
  67. desired_extra_ids << Tag.bot.id
  68. desired_extra_ids.compact!
  69. desired_extra_ids.uniq!
  70. desired_all_ids = kept_non_nico_ids.to_a + desired_nico_ids + desired_extra_ids
  71. desired_all_ids.uniq!
  72. sync_post_tags!(post, desired_all_ids)
  73. end
  74. end
  75. end