Module: Fourchan::Kit::Tools

Defined in:
lib/fourchan/kit/tools.rb

Class Method Summary collapse

Class Method Details

.download_image(link, options = {}) ⇒ Object

Downloads the image from an URL.

Parameters:

  • link (URL)

    the URL where the image is.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fourchan/kit/tools.rb', line 15

def self.download_image(link, options = {})
  options[:fsize] ||= 0
  options[:name]  ||= link.split('/').last
  options[:out]   ||= "#{Dir.pwd}/images"
  options[:quiet] ||= false

  image = "#{create_dir(options[:out])}/#{options[:name]}"
  unless File.exists?(image)
    if valid_link?(link)
      output = "Downloading: #{link}" unless options[:quiet]
      output << (options[:fsize].zero? ? "" : " @ " << "#{(options[:fsize] / 1024.0).round(2)}kB".rjust(9))
      puts output
      $agent.get(link).save(image)
    end
  else
    puts "Already got image, skipping" unless options[:quiet]
  end
end

.download_thread(link, options = {}) ⇒ Object

Downloads every image from a thread.

Parameters:

  • link (URL)

    the URL for the thread to download.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fourchan/kit/tools.rb', line 38

def self.download_thread(link, options = {})
  options[:checked] ||= false

  if options[:checked] || ( valid_thread?(link) && valid_link?(link) )
    board, thread_no = get_info(link)
    thread = Thread.new(board, thread_no)

    thread.posts.each do |post|
      options[:fsize] = post.fsize
      download_image(post.image_link, options.dup) if post.image_link
    end
  else
    puts "Not a 4chan thread" unless options[:quiet]
  end
end

.download_threads(file, options = {}) ⇒ Object

Download all images from each thread in a file.

Each thread must be on its own line and only be the URL, nothing else.

For example:

# threads.txt
http://boards.4chan.org/wg/thread/5777567
http://boards.4chan.org/wg/thread/5776602

It takes care of dead threads or wrong URLs.

Parameters:

  • file (File)

    the location of the file.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fourchan/kit/tools.rb', line 66

def self.download_threads(file, options = {})
  options[:quiet] ||= false

  if File.exists?(file)
    File.open(file, 'r').each_line do |link|
      puts "Getting images from thread: #{link}" unless options[:quiet]
      if valid_thread?(link) && valid_link?(link)
        options[:out]     = "images/#{link.scan(/(\d+)$/).first.first}"
        options[:checked] = true
        download_thread(link, options)
        puts
      else
        puts "Not a 4chan thread" unless options[:quiet]
        puts
      end
    end
  else
    puts "Not able to find the input file"
  end
end

.lurk(link, options = {}) ⇒ Object

Check the thread for new images every x seconds.

  • The refresh rate is determined by options and is an integer.

  • The time to lurk is determined by options and is an integer.

Parameters:

  • link (URL)

    the thread to lurk



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fourchan/kit/tools.rb', line 94

def self.lurk(link, options = {})
  puts "Started lurking #{link}"

  downloaded = []
  board, thread_no = get_info(link)
  thread = Thread.new(board, thread_no)

  download_image(thread.op.image_link, options.dup)

  begin
    timeout(options[:timeout]) do
      loop do
        puts "Checking for images" unless options[:quiet]
        new = thread.fetch_replies

        (new - downloaded).each do |post|
          options[:fsize] = post.fsize
          download_image(post.image_link, options.dup) if post.image_link

          downloaded << post
        end

        sleep(options[:refresh])
      end
    end
  rescue Timeout::Error
    puts "Timeout after #{options[:timeout]} second(s)"
    exit 0
  end
end