Created
April 25, 2017 15:04
-
-
Save modeverv/b415ac44f80294bdf199008b2f2b974f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AvatarHeader | |
require 'fileutils' | |
def get_loop(field, method) | |
Account.remote.limit(19).each do |account| | |
# Account.remote.find_each do |account| | |
send(method, field, account) | |
end | |
end | |
def re_get(field, account) | |
return unless account.send(field + '_file_name').present? && account.send(field + '_remote_url').present? | |
return if File.exist?(account.send(field).path) | |
kind = 'refetch' | |
puts "#{kind} #{field} of #{account.username}" | |
mkdir_p(File.dirname(account.send(field).path)) | |
open(account.send(field).path, 'wb') do |out| | |
open(account.send(field + '_remote_url')) do |data| | |
out.write(data.read) | |
end | |
end | |
rescue => ex | |
_raise(field, account, ex, kind) | |
end | |
def re_save(field, account) | |
return unless account.send(field).exists? && account.send(field + '_remote_url').present? | |
kind = 'refresh' | |
puts "#{kind} #{field} of #{account.username}" | |
account.send(field + '=', URI.parse(account.send(field + '_remote_url'))) | |
account.save | |
rescue => ex | |
_raise(field, account, ex, kind) | |
end | |
def _raise(field, account, ex, kind) | |
# need to think about "404" | |
puts "can't #{kind} #{field} of #{account.username} due to " + ex.inspect | |
end | |
end | |
desc 'Refetch missing avatar and header' | |
task refetch_avatar_header: :environment do | |
include AvatarHeader | |
%w(avatar header).each { |e| get_loop e, :re_get } | |
end | |
desc 'Refresh All avatar and header' | |
task refresh_avatar_header: :environment do | |
include AvatarHeader | |
%w(avatar header).each { |e| get_loop e, :re_save } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment