Last active
March 23, 2016 09:05
-
-
Save song940/6534104 to your computer and use it in GitHub Desktop.
Image downloader for loudatui.com
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
require 'net/http' | |
require 'nokogiri' | |
### | |
# Lsong | |
# i@lsong.org | |
# http://lsong.org | |
# | |
# MIT LICENSE | |
# http://lsong.mit-license.org/ | |
# | |
# RUNTIME | |
# ============== | |
# $ ruby -v | |
# ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0] | |
# | |
def fetch_page(num) | |
puts "Now index from page #{num}" | |
base_url = "http://loudatui.com" | |
#images in page | |
images_url = [] | |
#make a uri | |
uri = URI.parse(base_url) | |
Net::HTTP.start(uri.host,uri.port) do |http| | |
resp = http.get("/?page=#{num}") | |
#parse xml | |
doc = Nokogiri::XML(resp.body) | |
#find all <img class="img" /> | |
images = doc.css("img.img") | |
images.each do |img| | |
#get image src attribute | |
images_url << img.attr('src') | |
end | |
end | |
images_url | |
end | |
def fetch_site | |
# | |
puts "Downloading index , will take a minute\n" | |
puts "#{'-' * 50}\n" | |
n = 1 #page begin from 1 | |
#temp var | |
images_url = [] | |
#fetch all page | |
while (images_url = fetch_page(n)).count > 0 do | |
#merge arrays | |
images_url.each do |img_url| | |
download_image img_url | |
end | |
n = n + 1 | |
end | |
end | |
def download_image(url) | |
#get a big pic , not small . | |
url = url.gsub('/small','/large') | |
#file name | |
fname = /[0-9a-z]{22,32}/.match(url)[0] || Time.now.to_i.to_s | |
# .ext | |
fname = "#{fname}.jpg" | |
#break , if file already exist . | |
return if File.exist? fname | |
File.open(fname,'wb') do |f| | |
uri = URI.parse(url) | |
Net::HTTP.start(uri.host,uri.port) do |http| | |
resp = http.get(uri.path) | |
f.write resp.body | |
puts "download image file : #{fname}" | |
end | |
end | |
end | |
def main | |
puts "Image downloader for loudatui.com\n" | |
puts "#{'=' * 50}\n" | |
fetch_site | |
#have a fun . | |
puts "finished" | |
end | |
main | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment