Advertisement
Clworld

Mastodon アカウント画像再ダウンロード

Apr 16th, 2017
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.62 KB | None | 0 0
  1. # Mastodon アカウント画像再ダウンロードタスク / Re-download deleted account images task.
  2. # lib/tasks/test.rake
  3. # RAILS_ENV=production bundle exec rake test:account_images:recover
  4. #
  5. # なぜかあまり大量だと too many open files が出る(再実行で続きから実行される)
  6. # Sometime occurs "too many open files" error. (You need re-execute task.)
  7. # プログレス表示とかは無い / no progress display.
  8. # S3などを使用している場合かなり多めのリクエストをS3へ飛ばすことになるのでコストに注意。
  9. # If you use cloud strage, this task produces a lot of GET request to storage. (for checking image file exists).
  10.  
  11. namespace :test do
  12.   namespace :account_images do
  13.     desc 'download missing avatars & headers'
  14.     task recover: :environment do
  15.       Account.remote.each do |a|
  16.         begin
  17.           changed = false
  18.           if a.avatar.present? || a.avatar_remote_url.present?
  19.             unless a.avatar.exists?
  20.               a.avatar = a.avatar_remote_url
  21.               changed = true
  22.             end
  23.           end
  24.           if a.header.present? || a.header_remote_url.present?
  25.             unless a.header.exists?
  26.               a.header = a.header_remote_url
  27.               changed = true
  28.             end
  29.           end
  30.           if changed
  31.             a.save
  32.             puts "Recovered account images for @#{a.username}@#{a.domain}"
  33.             sleep 0.3
  34.           end
  35.         rescue OpenURI::HTTPError, SocketError, Timeout::Error, OpenSSL::SSL::SSLError, Errno::ECONNRESET
  36.           ; # ignore errors
  37.         end
  38.       end
  39.     end
  40.   end
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement