Class: Tumblrer

Inherits:
Object
  • Object
show all
Defined in:
lib/tumblrer.rb

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, number_of_images = nil, start_index = nil) ⇒ Tumblrer

Returns a new instance of Tumblrer.



6
7
8
9
10
11
12
13
14
# File 'lib/tumblrer.rb', line 6

def initialize(url = nil, number_of_images = nil, start_index = nil)
  @url = url
  @number_of_images = number_of_images || 10
  @start_index = start_index || 0 
  @successfulls = 0
  @fails = 0
  @already_exists = 0
  @mechanize = Mechanize.new
end

Instance Method Details

#downloadObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tumblrer.rb', line 16

def download
  unless @url.nil?
    puts "download from: #{@url}"
    puts "going to download #{@number_of_images} of image(s) and starting from (#{@start_index}) index of image"
    puts "---------- downloading ----------"
    url = "#{@url}/api/read?type=photo&num=#{@number_of_images}&start=#{@start_index}"
    xml = Nokogiri::XML.parse(open(url))
    images = (xml/'post photo-url').select{|post| post.content if post['max-width'].to_i == 1280}.map{|image| image.content}
    pwd = FileUtils.pwd
    images.each do |image|
      unless File.exists?("#{pwd}/#{image.to_s.split("/").last}")
        begin
          @mechanize.get(image).save
          puts "file saved to current directory :) - #{image}"
          @successfulls += 1
        rescue
          puts "failed to save file to current directory :( - #{image}"
          @fails += 1
        end
      else
        puts "file already exists :( - #{image}"
        @already_exists += 1
      end
    end
    statistics
  else
    puts "you should give some tumblr url :( \n  usage: tumblrer http://hebe.tumblr.com"
  end
end

#statisticsObject



46
47
48
49
50
51
# File 'lib/tumblrer.rb', line 46

def statistics
  puts "---------- statistics ----------"
  puts "total number of downloaded images: #{@successfulls}"
  puts "total number of failed images: #{@fails}"
  puts "total number of already exist images: #{@already_exists}"
end