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

36 lines
1.1 KiB

  1. namespace :nico do
  2. desc 'ニコニコ DB 同期'
  3. task sync: :environment do
  4. require 'open3'
  5. mysql_user = ENV['MYSQL_USER']
  6. mysql_pass = ENV['MYSQL_PASS']
  7. nizika_nico_path = ENV['NIZIKA_NICO_PATH']
  8. stdout, stderr, status = Open3.capture3(
  9. { 'MYSQL_USER' => mysql_user, 'MYSQL_PASS' => mysql_pass },
  10. 'python3', "#{ nizika_nico_path }/get_videos.py")
  11. if status.success?
  12. data = JSON.parse(stdout)
  13. data.each do |datum|
  14. post = Post.where('url LIKE ?', '%nicovideo.jp%').find { |post|
  15. post.url =~ %r{#{ Regexp.escape(datum['code']) }(?!\d)}
  16. }
  17. unless post
  18. title = datum['title']
  19. url = "https://www.nicovideo.jp/watch/#{ datum['code'] }"
  20. post = Post.new(title:, url:, thumbnail_base: '', uploaded_user: nil)
  21. post.save!
  22. end
  23. post.tags.destroy(post.tags.where(category: 'nico'))
  24. datum['tags'].each do |name|
  25. name = "nico:#{ name }"
  26. tag = Tag.find_or_initialize_by(name:, category: 'nico')
  27. post.tags << tag
  28. end
  29. end
  30. end
  31. end
  32. end