Module: Fourchan::Kit::Tools
- Defined in:
- lib/fourchan/kit/tools.rb
Class Method Summary collapse
-
.download_image(link, options = {}) ⇒ Object
Downloads the image from an URL.
-
.download_thread(link, options = {}) ⇒ Object
Downloads every image from a thread.
-
.download_threads(file, options = {}) ⇒ Object
Download all images from each thread in a file.
-
.lurk(link, options = {}) ⇒ Object
Check the thread for new images every x seconds.
Class Method Details
.download_image(link, options = {}) ⇒ Object
Downloads the image from an URL.
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, = {}) [: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 |
.download_thread(link, options = {}) ⇒ Object
Downloads every image from a thread.
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, = {}) [:checked] ||= false if [:checked] || ( valid_thread?(link) && valid_link?(link) ) board, thread_no = get_info(link) thread = Thread.new(board, thread_no) thread.posts.each do |post| [:fsize] = post.fsize download_image(post.image_link, .dup) if post.image_link end else puts "Not a 4chan thread" unless [: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.
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, = {}) [: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 |
.lurk(link, options = {}) ⇒ Object
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, = {}) 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 |