Module: Fourchan::Kit::Tools
- Defined in:
- lib/fourchan/kit/tools.rb
Class Method Summary (collapse)
-
+ (Object) download_image(link, options = {})
Downloads the image from an URL.
-
+ (Object) download_thread(link, options = {})
Downloads every image from a thread.
-
+ (Object) download_threads(file, options = {})
Download all images from each thread in a file.
-
+ (Object) lurk(link, options = {})
Check the thread for new images every x seconds.
Class Method Details
+ (Object) download_image(link, options = {})
Downloads the image from an URL.
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, = {}) [:fsize] ||= 0 [:name] ||= link.split('/').last [:out] ||= "#{Dir.pwd}/images" [:quiet] ||= false image = "#{create_dir([:out])}/#{[:name]}" unless File.exists?(image) if valid_link?(link) output = "Downloading: #{link}" unless [:quiet] output << ([:fsize].zero? ? "" : " @ " << "#{([:fsize] / 1024.0).round(2)}kB".rjust(9)) puts output $agent.get(link).save(image) end else puts "Already got image, skipping" unless [: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.
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, = {}) [:checked] ||= false if [: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| [:fsize] = sizes[index] download_image(image, .dup) end else puts "Not a 4chan thread" unless [: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.
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, = {}) [:quiet] ||= false if File.exists?(file) File.open(file, 'r').each_line do |link| puts "Getting images from thread: #{link}" unless [:quiet] if valid_thread?(link) && valid_link?(link) [:out] = "images/#{link.scan(/(\d+)$/).first.first}" [:checked] = true download_thread(link, ) puts else puts "Not a 4chan thread" unless [:quiet] puts end end else puts "Not able to find the input file" end end |
+ (Object) lurk(link, options = {})
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, = {}) puts "Started lurking #{link}" downloaded = [] board, thread_no = get_info(link) thread = Thread.new(board, thread_no) download_image(thread.op.image_link, .dup) begin timeout([:timeout]) do loop do puts "Checking for images" unless [:quiet] new = thread.fetch_replies (new - downloaded).each do |post| [:fsize] = post.fsize download_image(post.image_link, .dup) if post.image_link downloaded << post end sleep([:refresh]) end end rescue Timeout::Error puts "Timeout after #{[:timeout]} second(s)" exit 0 end end |