Skip to content

Instantly share code, notes, and snippets.

@Coro365
Created March 15, 2020 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coro365/1c34ef5609aa65d7e8f5406728ce64d2 to your computer and use it in GitHub Desktop.
Save Coro365/1c34ef5609aa65d7e8f5406728ce64d2 to your computer and use it in GitHub Desktop.
Download photos from f.hatena.ne.jp
require 'open-uri'
require 'fileutils'
def set_username
return ARGV[0] if ARGV[0]
raise('Specify the user name in the argument.')
end
def get_photo_urls
puts("Get photo urls from #{$user}'s #{$page_limit}fotolife pages.")
photo_urls = 1.upto($page_limit).map do |page|
page_param = "?page=#{page}"
url = ['https://f.hatena.ne.jp', $user, page_param].join('/')
fotolife_html = URI.open(url).read
scrape_photo_url(fotolife_html)
end
photo_urls.flatten
end
def scrape_photo_url(fotolife_html)
url_reg = %r{https://cdn-ak\.f\.st-hatena\.com/images/fotolife}
file_type_reg = /jpg|jpeg|gif|png|bmp/
thumbnail_url_reg = %r[(#{url_reg}/./.*?/\d{8}/.*?\.(?:#{file_type_reg}))]
thumbnail_urls = fotolife_html.scan(thumbnail_url_reg)
thumbnail_urls.map { |e| e.first.sub('_120.', '_original.') }
end
def compare_by(log, photo_urls)
if log
photo_urls.reject { |e| log.include?(file_name(e)) }
else
photo_urls
end
end
def download(photo_urls)
puts('Download photo')
url_size = photo_urls.size
photo_urls.each_with_index do |photo_url, i|
formated_i = format("%0#{url_size.to_s.size}d", i + 1)
file_name = file_name(photo_url)
puts("[#{formated_i}/#{url_size}] #{file_name}")
save_path = File.join(__dir__, 'photo', $user, file_name)
try_count = 0
begin
save_photo(save_path, photo_url)
add_log(file_name)
$download_n += 1
rescue
photo_url = photo_url.sub('_original', '')
try_count += 1
try_count <= 2 ? retry : next
end
end
end
def save_photo(save_path, url)
File.write(save_path, URI.open(url).read)
end
def file_name(url)
File.basename(url).sub('_original', '')
end
def create_user_dir
puts('Scan log')
user_dir = File.join(__dir__, 'photo', $user)
FileUtils.mkdir_p(user_dir) unless File.exist?(user_dir)
end
def add_log(recode)
recode = [Time.now, recode + "\n"].join(",\s")
File.write($log_path, recode, mode: 'a')
end
def load_log(log_path)
if File.exist?(log_path)
File.read(log_path)
else
FileUtils.touch(log_path)
nil
end
end
def macos_niti(title, message)
%x[osascript -e 'display notification "#{message}" with title "#{title}" sound name "purr"']
end
def end_message
if $download_n.zero?
puts('Not found new photo.')
else
puts("#{$download_n}items download complete.")
macos_niti('Fotolife downloader', "#{$download_n}items download complete")
end
end
$user = set_username
$page_limit = ARGV[1] ? ARGV[1].to_i : 20
$download_n = 0
$log_path = File.join(__dir__, 'log', "#{$user}.log")
log = load_log($log_path)
create_user_dir
photo_urls = compare_by(log, get_photo_urls)
download(photo_urls)
end_message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment