#!/usr/bin/ruby
#
# this script is for downloading files from File Hosting Sites
# Copyright (C) 2010 Jakub Jankiewicz (jcubic@onet.pl)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
require 'net/http'
require 'uri'
require 'getoptlong'
require 'rexml/document'
class DownloadLimitException < Exception
end
class DownloadInProgress < Exception
end
class FileToBigException < Exception
end
class FileDeletedException < Exception
end
class LinkErrorException < Exception
end
class TransferLimitException < Exception
end
class ServerBusyException < Exception
end
class BadPasswordException < Exception
end
def title(string)
return string.split(' ').map {|word| word.capitalize }.join(' ')
end
def host(url)
url =~ /http:\/\/([^\/]*)\//
return $1
end
def file(name)
File.open(name) {|file|
return file.read()
}
end
def trail_slash(url)
if url =~ /http:\/\/[^\/]*$/
url += '/'
end
return url
end
class GetOpt
def initialize(*options)
opts = GetoptLong.new(*options)
@opts = {}
opts.each{|opt,arg|
@opts[opt] = arg
}
end
def [](key)
return @opts[key]
end
end
def response(url, cookies=nil, referer=nil)
url = URI.parse(trail_slash(url))
http = Net::HTTP.new(url.host)
headers = {}
if cookies
headers['Cookie'] = cookies
end
if referer
headers['Referer'] = referer
end
res = http.get(url.path, headers)
return res
end
def post(url, data)
url = URI.parse(trail_slash(url))
res = Net::HTTP.post_form(url, data)
return res.body
end
def wait_indicator(time)
while time >= 0
system('tput sc; echo -n "Wait ' + time.to_s + ' seconds "; tput rc')
sleep(1)
time -= 1
end
puts "done "
end
def livebox_send(data, host, passwd)
url = URI.parse("http://#{host}/SubmitInternetService")
req = Net::HTTP::Post.new(url.path)
req.basic_auth('admin', passwd)
req.set_form_data(data)
res = Net::HTTP.new(url.host, url.port).start {|http|
http.request(req)
}
if res.code == 401
raise BadPasswordException.new("bad password")
else
return res.body
end
end
def connect(host, passwd)
data = {'ACTION_CONNECT' => 'Połącz'}
livebox_send(data, host, passwd)
end
def disconnect(host, passwd)
data = {'ACTION_DISCONNECT'=> 'Rozłącz'}
livebox_send(data, host, passwd)
end
def wget(url, limit=false, filename=nil, referer=nil, cookies=nil)
params = '-c -U Mozilla --keep-session-cookies'
if limit
params += " --limit-rate=#{limit}"
end
if filename
params += " -O \"#{filename}\""
end
if cookies
params += " --load-cookies=\"#{cookies}\""
end
if referer
params += " --referer=\"#{referer}\""
end
`wget #{params} "#{url}"`
end
def four_shared(url, limit=false)
res = response(url)
if res.body =~ /a href="([^"]*)" class="dbtn"/
page = response($1, res.response['set-cookie'], url).body
cookies_filename = 'download_4shared_cookies.txt'
File.open(cookies_filename, 'w') {|file|
file.puts res.response['set-cookie']
}
if page =~ /(Download file now|Pobierz plik teraz)<\/a>/
url = $1
page =~ /([^<]*)
filename = $1
page =~ /var c[= ]+([0-9]*);/
time = $1.to_i
if RUBY_PLATFORM =~ /(:?mswin|mingw)/i
puts "Wait #{time} seconds."
else
#use nice wait indicator on unix (require tput)
wait_indicator(time)
end
wget(url, limit, filename, nil, cookies_filename)
end
end
end
def rapidshare(url, limit=false)
page = response(url).body
if page =~ /The file could not be found/
raise LinkErrorException
end
if page =~ /the file has been removed from the server/
raise FileDeletedException
end
if page =~ /The uploader has removed this/
raise FileDeletedException
end
if page =~ /