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

67 lines
2.3 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 {
  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. }
  12. mysql_user = ENV['MYSQL_USER']
  13. mysql_pass = ENV['MYSQL_PASS']
  14. nizika_nico_path = ENV['NIZIKA_NICO_PATH']
  15. stdout, stderr, status = Open3.capture3(
  16. { 'MYSQL_USER' => mysql_user, 'MYSQL_PASS' => mysql_pass },
  17. 'python3', "#{ nizika_nico_path }/get_videos.py")
  18. if status.success?
  19. data = JSON.parse(stdout)
  20. data.each do |datum|
  21. post = Post.where('url LIKE ?', '%nicovideo.jp%').find { |post|
  22. post.url =~ %r{#{ Regexp.escape(datum['code']) }(?!\d)}
  23. }
  24. unless post
  25. title = datum['title']
  26. url = "https://www.nicovideo.jp/watch/#{ datum['code'] }"
  27. thumbnail_base = fetch_thumbnail.(url) || '' rescue ''
  28. post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil)
  29. if thumbnail_base.present?
  30. post.thumbnail.attach(
  31. io: URI.open(thumbnail_base),
  32. filename: File.basename(URI.parse(thumbnail_base).path),
  33. content_type: 'image/jpeg')
  34. end
  35. post.save!
  36. post.resized_thumbnail!
  37. end
  38. current_tags = post.tags.where(category: 'nico').pluck(:name).sort
  39. new_tags = datum['tags'].map { |tag| "nico:#{ tag }" }.sort
  40. if current_tags != new_tags
  41. post.tags.destroy(post.tags.where(name: current_tags))
  42. new_tags.each do |name|
  43. post.tags << Tag.find_or_initialize_by(name:, category: 'nico')
  44. end
  45. if post.tags.size < 20
  46. name = 'タグ希望'
  47. post.tags.destroy(post.tags.find_by(name:))
  48. post.tags << Tag.find_or_initialize_by(name:) { |tag|
  49. tag.category = 'meta'
  50. }
  51. end
  52. name = 'bot操作'
  53. post.tags.destroy(post.tags.find_by(name:))
  54. post.tags << Tag.find_or_initialize_by(name:) { |tag|
  55. tag.category = 'meta'
  56. }
  57. end
  58. end
  59. end
  60. end
  61. end