#!/usr/bin/ruby
# -*- coding: utf-8 -*-
#script for download files from przekle.pl
# 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"
class FileToBigException < Exception
end
class FileDeleted < Exception
end
class LinkError < Exception
end
def file(name)
File.open(name) {|file|
return file.read()
}
end
def host(url)
url =~ /http:\/\/([^\/]*)\//
return $1
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 get(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.body
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 przeklej_login_cookies(user, passwd)
url = URI.parse('http://www.przeklej.pl/loguj')
data = {
'login[login]'=> user,
'login[pass]' => passwd}
res = Net::HTTP.post_form(url, data)
return res.response['set-cookie']
end
def przeklej_logout()
url = 'http://przeklej.pl/wyloguj'
return get(url)
end
def fix_filename(filename)
filename = filename.gsub(/ +/, ' ')
filename = filename.gsub(' .', '.')
filename = title(filename)
return filename
end
def download(url, limit=false, user=nil, passwd=nil)
referer = url
if user and passwd
cookies = przeklej_login_cookies(user, passwd)
cookies_filename = 'download_przeklej_cookies.txt'
File.open(cookies_filename, 'w') {|file|
file.puts cookies
}
page = get(url, cookies)
else
cookies_filename = nil
page = get(url)
end
if page =~ /Plik zosta/
raise FileDeletedException
end
#if not loged
if not page =~ /Wyloguj/
loged = false
if page =~ /pny abonament<\/strong>/
raise FileToBigException
end
else
loged = true
end
if page =~ /