Last active
December 12, 2015 08:58
-
-
Save jhbruhn/4747635 to your computer and use it in GitHub Desktop.
Download "All" Puush.me-files in a crappy way (but it works, yay!) requires 'trollop' gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'fileutils' | |
require 'logger' | |
require 'trollop' | |
CHARS = ("0".."9").to_a + ("a".."z").to_a + ("A".."Z").to_a | |
opts = Trollop::options do | |
opt :output_dir, "Output-Directory", :short => 'o', :default => "files", :type => String | |
opt :log_file, "Logfile-Path", :short => 'l', :default => "STDOUT", :type => String | |
version "PuuushLoad 1.0 (c) 2013 Jan-Henrik Bruhn" | |
end | |
if(opts[:log_file] == "STDOUT") then | |
$logger = Logger.new(STDOUT) | |
else | |
$logger = Logger.new(opts[:log_file], 10, 1024000) | |
end | |
$logger.level = Logger::INFO | |
def download_file_from_puush(id, subdir) | |
begin | |
Net::HTTP.start("puu.sh") do |http| | |
file = nil | |
begin | |
http.request_get('/' + URI.encode(id)) do |response| | |
filename = get_file_name_from_response(response) | |
return false if !filename | |
filename = id + " - " + filename | |
path = subdir + "/" + filename | |
return filename if File.exists?(path) | |
FileUtils.touch(path) | |
file = open(path, 'wb') | |
response.read_body do |segment| | |
file.write(segment) | |
end | |
return filename | |
end | |
end | |
end | |
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Errno::EHOSTUNREACH, SocketError, | |
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e | |
$logger.error "Connection failed: " + e.to_s | |
return false | |
end | |
end | |
def get_file_name_from_response(response) | |
contentDisposition = response['content-disposition'] | |
if contentDisposition.nil? then | |
return false | |
end | |
contentDisposition.split(";")[1].split("=")[1].gsub("\"", "") | |
end | |
def doDownloading(subdir) | |
$logger.info "PUUSHLoader v.1.1 started!" | |
dir = File.absolute_path(subdir) | |
$logger.info "Downloading into #{dir}" | |
totalValidFiles = 0 | |
totalInvalidFiles = 0 | |
loop do | |
FileUtils.makedirs(subdir) | |
batches = 0 | |
$logger.info "Starting a runthrough!" | |
("1".."9").each do |num| | |
CHARS.each do |char1| | |
CHARS.each do |char2| | |
CHARS.each do |char3| | |
spawnedThreads = [] | |
validFiles = 0 | |
invalidFiles = 0 | |
batches += 1 | |
$logger.info "Starting Batch #{batches}" | |
CHARS.each do |char4| | |
str = num + char1 + char2 + char3 + char4 | |
spawnedThreads << Thread.new { | |
result = download_file_from_puush str, dir | |
if result != false then | |
validFiles += 1 | |
else | |
invalidFiles += 1 | |
end | |
} | |
sleep 0.05 #erstmal chillen | |
end | |
spawnedThreads.each { |spawnedThread| spawnedThread.join } | |
$logger.info "Downloaded batch #{batches}; Total files: #{validFiles + invalidFiles}. Valid Files: #{validFiles}. Invalid Files: #{invalidFiles}" | |
totalValidFiles += validFiles | |
totalInvalidFiles += invalidFiles | |
end | |
end | |
end | |
end | |
$logger.info "Finished with a runthrough, starting again soon!" | |
$logger.info "Total files: #{$totalValidFiles + $totalInvalidFiles}. Valid Files: #{$totalValidFiles}. Invalid Files: #{$totalInvalidFiles}" | |
sleep 60 | |
end | |
end | |
subdir = opts[:output_dir] | |
doDownloading(subdir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment