Module: Fourchan::Kit::Tools

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

Class Method Summary (collapse)

Class Method Details

+ (Object) download_image(link, options = {})

Downloads the image from an URL.

Parameters:

  • link (URL)

    the URL where the image is.



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

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

+ (Object) download_thread(link, options = {})

Downloads every image from a thread.

Makes use of parallel processing for faster downloading. Currently set to 8 threads.

Parameters:

  • link (URL)

    the URL for the thread to download.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fourchan/kit/tools.rb', line 41

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)
    images  = []
    sizes   = []

    thread.posts.each do |post|
      sizes  << post.fsize
      images << post.image_link if post.tim
    end

    Parallel.each_with_index(images, in_threads: 8) do |image, index|
      options[:fsize] = sizes[index]
      download_image(image, options.dup)
    end

  else
    puts "Not a 4chan thread" unless options[:quiet]
  end
end

+ (Object) download_threads(file, options = {})

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fourchan/kit/tools.rb', line 77

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

+ (Object) lurk(link, options = {})

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/fourchan/kit/tools.rb', line 105

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