rr2fetcher 0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ == 0.0.1 2009-06-08
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
5
+
6
+ == 0.2 2009-07-07
7
+ * new release
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ bin/rr2fetcher
7
+ lib/progressbar.rb
8
+ lib/rr2fetcher.rb
9
+ lib/rrs_parser.rb
10
+ rr2fetcher.gemspec
11
+ script/console
12
+ script/destroy
13
+ script/generate
@@ -0,0 +1,7 @@
1
+
2
+ For more information on rr2fetcher, see http://rr2fetcher.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
@@ -0,0 +1,48 @@
1
+ = rr2fetcher
2
+
3
+ * http://github.com/dcu/rr2fetcher
4
+
5
+ == DESCRIPTION:
6
+
7
+ Download files from a rapidshare free account
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * unknown
12
+
13
+ == SYNOPSIS:
14
+
15
+
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * hpricot
20
+
21
+ == INSTALL:
22
+
23
+ * sudo gem install dcu-rr2fetcher
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/rr2fetcher'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'rr2fetcher' do
14
+ self.developer 'David Cuadrado', 'krawek@gmail.com'
15
+ self.post_install_message = 'PostInstall.txt'
16
+ self.rubyforge_name = self.name
17
+ self.extra_deps = [['hpricot','>= 0.8.1']]
18
+ end
19
+
20
+ require 'newgem/tasks'
21
+ Dir['tasks/**/*.rake'].each { |t| load t }
22
+
23
+ # remove_task :default
24
+ # task :default => [:spec, :features]
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.expand_path(File.dirname(__FILE__)+"/../lib/")
4
+
5
+ require 'rr2fetcher'
6
+ require 'rrs_parser'
7
+
8
+ require 'rubygems'
9
+ require 'hpricot'
10
+ require 'net/http'
11
+ require 'uri'
12
+ require 'progressbar'
13
+ require 'optparse'
14
+
15
+ options = {
16
+ :backend => :wget
17
+ }
18
+
19
+ parser = OptionParser.new do |opts|
20
+ opts.banner = "Usage: #{$0} [options] <url|file>"
21
+
22
+ opts.on("-b", "--backend=BACKEND", %w[wget curl kget4], "Backend", "Default: #{options[:backend]}") do |o|
23
+ options[:backend] = o.to_sym
24
+ end
25
+
26
+ opts.on_tail("-h", "--help", "Show this help message.") do
27
+ puts opts
28
+ exit
29
+ end
30
+ end
31
+
32
+ args = []
33
+ begin
34
+ args = parser.parse!
35
+ raise "Missing url or file" if args.empty?
36
+ rescue => e
37
+ $stderr.puts "Error: #{e}"
38
+ puts parser
39
+ exit 1
40
+ end
41
+
42
+ downloader = RR2Fetcher.new(options)
43
+ downloader.add_download(*args)
44
+
45
+ downloader.start_dowload
@@ -0,0 +1,236 @@
1
+ #
2
+ # Ruby/ProgressBar - a text progress bar library
3
+ #
4
+ # Copyright (C) 2001-2005 Satoru Takabayashi <satoru@namazu.org>
5
+ # All rights reserved.
6
+ # This is free software with ABSOLUTELY NO WARRANTY.
7
+ #
8
+ # You can redistribute it and/or modify it under the terms
9
+ # of Ruby's license.
10
+ #
11
+
12
+ class ProgressBar
13
+ VERSION = "0.9"
14
+
15
+ def initialize (title, total, out = STDERR)
16
+ @title = title
17
+ @total = total
18
+ @out = out
19
+ @terminal_width = 80
20
+ @bar_mark = "o"
21
+ @current = 0
22
+ @previous = 0
23
+ @finished_p = false
24
+ @start_time = Time.now
25
+ @previous_time = @start_time
26
+ @title_width = 14
27
+ @format = "%-#{@title_width}s %3d%% %s %s"
28
+ @format_arguments = [:title, :percentage, :bar, :stat]
29
+ clear
30
+ show
31
+ end
32
+ attr_reader :title
33
+ attr_reader :current
34
+ attr_reader :total
35
+ attr_accessor :start_time
36
+
37
+ private
38
+ def fmt_bar
39
+ bar_width = do_percentage * @terminal_width / 100
40
+ sprintf("|%s%s|",
41
+ @bar_mark * bar_width,
42
+ " " * (@terminal_width - bar_width))
43
+ end
44
+
45
+ def fmt_percentage
46
+ do_percentage
47
+ end
48
+
49
+ def fmt_stat
50
+ if @finished_p then elapsed else eta end
51
+ end
52
+
53
+ def fmt_stat_for_file_transfer
54
+ if @finished_p then
55
+ sprintf("%s %s %s", bytes, transfer_rate, elapsed)
56
+ else
57
+ sprintf("%s %s %s", bytes, transfer_rate, eta)
58
+ end
59
+ end
60
+
61
+ def fmt_title
62
+ @title[0,(@title_width - 1)] + ":"
63
+ end
64
+
65
+ def convert_bytes (bytes)
66
+ if bytes < 1024
67
+ sprintf("%6dB", bytes)
68
+ elsif bytes < 1024 * 1000 # 1000kb
69
+ sprintf("%5.1fKB", bytes.to_f / 1024)
70
+ elsif bytes < 1024 * 1024 * 1000 # 1000mb
71
+ sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
72
+ else
73
+ sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
74
+ end
75
+ end
76
+
77
+ def transfer_rate
78
+ bytes_per_second = @current.to_f / (Time.now - @start_time)
79
+ sprintf("%s/s", convert_bytes(bytes_per_second))
80
+ end
81
+
82
+ def bytes
83
+ convert_bytes(@current)
84
+ end
85
+
86
+ def format_time (t)
87
+ t = t.to_i
88
+ sec = t % 60
89
+ min = (t / 60) % 60
90
+ hour = t / 3600
91
+ sprintf("%02d:%02d:%02d", hour, min, sec);
92
+ end
93
+
94
+ # ETA stands for Estimated Time of Arrival.
95
+ def eta
96
+ if @current == 0
97
+ "ETA: --:--:--"
98
+ else
99
+ elapsed = Time.now - @start_time
100
+ eta = elapsed * @total / @current - elapsed;
101
+ sprintf("ETA: %s", format_time(eta))
102
+ end
103
+ end
104
+
105
+ def elapsed
106
+ elapsed = Time.now - @start_time
107
+ sprintf("Time: %s", format_time(elapsed))
108
+ end
109
+
110
+ def eol
111
+ if @finished_p then "\n" else "\r" end
112
+ end
113
+
114
+ def do_percentage
115
+ if @total.zero?
116
+ 100
117
+ else
118
+ @current * 100 / @total
119
+ end
120
+ end
121
+
122
+ def get_width
123
+ # FIXME: I don't know how portable it is.
124
+ default_width = 80
125
+ begin
126
+ tiocgwinsz = 0x5413
127
+ data = [0, 0, 0, 0].pack("SSSS")
128
+ if @out.ioctl(tiocgwinsz, data) >= 0 then
129
+ rows, cols, xpixels, ypixels = data.unpack("SSSS")
130
+ if cols >= 0 then cols else default_width end
131
+ else
132
+ default_width
133
+ end
134
+ rescue Exception
135
+ default_width
136
+ end
137
+ end
138
+
139
+ def show
140
+ arguments = @format_arguments.map {|method|
141
+ method = sprintf("fmt_%s", method)
142
+ send(method)
143
+ }
144
+ line = sprintf(@format, *arguments)
145
+
146
+ width = get_width
147
+ if line.length == width - 1
148
+ @out.print(line + eol)
149
+ @out.flush
150
+ elsif line.length >= width
151
+ @terminal_width = [@terminal_width - (line.length - width + 1), 0].max
152
+ if @terminal_width == 0 then @out.print(line + eol) else show end
153
+ else # line.length < width - 1
154
+ @terminal_width += width - line.length + 1
155
+ show
156
+ end
157
+ @previous_time = Time.now
158
+ end
159
+
160
+ def show_if_needed
161
+ if @total.zero?
162
+ cur_percentage = 100
163
+ prev_percentage = 0
164
+ else
165
+ cur_percentage = (@current * 100 / @total).to_i
166
+ prev_percentage = (@previous * 100 / @total).to_i
167
+ end
168
+
169
+ # Use "!=" instead of ">" to support negative changes
170
+ if cur_percentage != prev_percentage ||
171
+ Time.now - @previous_time >= 1 || @finished_p
172
+ show
173
+ end
174
+ end
175
+
176
+ public
177
+ def clear
178
+ @out.print "\r"
179
+ @out.print(" " * (get_width - 1))
180
+ @out.print "\r"
181
+ end
182
+
183
+ def finish
184
+ @current = @total
185
+ @finished_p = true
186
+ show
187
+ end
188
+
189
+ def finished?
190
+ @finished_p
191
+ end
192
+
193
+ def file_transfer_mode
194
+ @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
195
+ end
196
+
197
+ def format= (format)
198
+ @format = format
199
+ end
200
+
201
+ def format_arguments= (arguments)
202
+ @format_arguments = arguments
203
+ end
204
+
205
+ def halt
206
+ @finished_p = true
207
+ show
208
+ end
209
+
210
+ def inc (step = 1)
211
+ @current += step
212
+ @current = @total if @current > @total
213
+ show_if_needed
214
+ @previous = @current
215
+ end
216
+
217
+ def set (count)
218
+ if count < 0 || count > @total
219
+ raise "invalid count: #{count} (total: #{@total})"
220
+ end
221
+ @current = count
222
+ show_if_needed
223
+ @previous = @current
224
+ end
225
+
226
+ def inspect
227
+ "#<ProgressBar:#{@current}/#{@total}>"
228
+ end
229
+ end
230
+
231
+ class ReversedProgressBar < ProgressBar
232
+ def do_percentage
233
+ 100 - super
234
+ end
235
+ end
236
+
@@ -0,0 +1,83 @@
1
+ class RR2Fetcher
2
+ VERSION = "0.2"
3
+
4
+ attr_accessor :download_list
5
+ attr_accessor :app
6
+
7
+ def initialize(options)
8
+ @download_list = []
9
+ @app = options[:backend]
10
+ @options = options
11
+ end
12
+
13
+ def add_download(*links)
14
+ links.each do |link|
15
+ if File.exist?(link)
16
+ File.open(link) do |f|
17
+ @download_list += f.readlines
18
+ end
19
+ else
20
+ @download_list << link
21
+ end
22
+ end
23
+ end
24
+
25
+ def start_dowload
26
+ while !@download_list.empty?
27
+ url = @download_list.pop.strip
28
+ next if url.empty? || downloaded?(url)
29
+ download url
30
+ end
31
+ end
32
+
33
+ def download(to_download)
34
+ parser = RSSParser.new(to_download)
35
+
36
+ wait_until_ready(parser)
37
+ agent = "Mozilla Firefox 1.5"
38
+
39
+ cmd = "wget -U '#{agent}' '#{parser.download_link}' --post-data='mirror=on&x=67&y=50'"
40
+ file = parser.filename # FIXME: check if download_link is valid
41
+
42
+ case @app
43
+ when :curl
44
+ cmd = "curl -A '#{agent}' --url '#{parser.download_link}' --data 'mirror=on&x=67&y=50' > #{file}"
45
+ when :kget4
46
+ url = %@'#{parser.download_link}?mirror=on&x=67&y=50'@
47
+ cmd = "qdbus org.kde.kget /kget/MainWindow_1 org.kde.kget.addTransfer '#{parser.download_link}' ./#{file} true"
48
+ end
49
+
50
+ system(cmd)
51
+ end
52
+
53
+ private
54
+ def downloaded?(url)
55
+ filename = url.to_s.split("/").last
56
+
57
+ if File.exist?(filename)
58
+ $stderr.puts "Already downloaded: #{filename}"
59
+ return true
60
+ end
61
+ false
62
+ end
63
+
64
+ def wait_until_ready(parser)
65
+ while parser.metadata.include?(:minutes)
66
+ show_delay(parser.metadata[:minutes] * 60)
67
+ parser.parse_next_page
68
+ end
69
+
70
+ show_delay(parser.metadata[:seconds])
71
+ end
72
+
73
+ def show_delay(seconds)
74
+ seconds += 5
75
+
76
+ pbar = ProgressBar.new("Waiting", seconds)
77
+ seconds.times do |i|
78
+ pbar.inc
79
+ sleep(1)
80
+ end
81
+ pbar.finish
82
+ end
83
+ end
@@ -0,0 +1,47 @@
1
+ class RSSParser
2
+ attr_reader :page, :metadata, :url, :download_link, :filename, :form_action
3
+ def initialize(url)
4
+ @page = Hpricot(Net::HTTP.get(URI.parse(url)))
5
+ @url = url
6
+ @download_link = ""
7
+ @filename = ""
8
+
9
+ find_form_action
10
+ end
11
+
12
+ def parse_next_page
13
+ @metadata = {}
14
+
15
+ resp = Net::HTTP.post_form(URI.parse(@form_action), {"dl.start"=>"free"})
16
+ body = resp.body
17
+
18
+ body.each_line do |line|
19
+ if line =~ /.*name="dlf"\saction="(\S*)"/
20
+ $stderr.puts "-> Downloading #{$1}..."
21
+ @download_link = $1
22
+ @filename = $1.to_s.split("/").last
23
+ end
24
+
25
+ if line =~ /var\s*c\s*=\s*(\d+)/
26
+ @metadata[:seconds] = $1.to_i
27
+ elsif line =~ /try\s*again\s*in\s*about\s*(\d+)\s*minutes/
28
+ @metadata[:minutes] = $1.to_i
29
+ end
30
+ end
31
+
32
+ @metadata
33
+ end
34
+
35
+ protected
36
+ def find_form_action
37
+ ff = @page.search("#ff")
38
+ unless ff
39
+ $stderr.puts "there was an error"
40
+ return false
41
+ end
42
+
43
+ @form_action = ff.attr("action") # FIXME: Check the error/exception
44
+ parse_next_page
45
+ true
46
+ end
47
+ end
@@ -0,0 +1,39 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rr2fetcher}
5
+ s.version = "0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jorge Cuadrado", "David Cuadrado"]
9
+ s.date = %q{2009-06-08}
10
+ s.default_executable = %q{rr2fetcher}
11
+ s.description = %q{Download files from a rapidshare free account}
12
+ s.email = ["krawek@gmail.com"]
13
+ s.executables = ["rr2fetcher"]
14
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc"]
15
+ s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "bin/rr2fetcher", "lib/progressbar.rb", "lib/rr2fetcher.rb", "lib/rrs_parser.rb", "rr2fetcher.gemspec", "script/console", "script/destroy", "script/generate"]
16
+ s.homepage = %q{http://github.com/dcu/rr2fetcher}
17
+ s.post_install_message = %q{PostInstall.txt}
18
+ s.rdoc_options = ["--main", "README.rdoc"]
19
+ s.require_paths = ["lib"]
20
+ s.rubyforge_project = %q{rr2fetcher}
21
+ s.rubygems_version = %q{1.3.4}
22
+ s.summary = %q{Download files from a rapidshare free account}
23
+
24
+ if s.respond_to? :specification_version then
25
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
+ s.specification_version = 3
27
+
28
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
29
+ s.add_development_dependency(%q<newgem>, [">= 1.4.1"])
30
+ s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
31
+ else
32
+ s.add_dependency(%q<newgem>, [">= 1.4.1"])
33
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
34
+ end
35
+ else
36
+ s.add_dependency(%q<newgem>, [">= 1.4.1"])
37
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
38
+ end
39
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/rr2fetcher.rb'}"
9
+ puts "Loading rr2fetcher gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rr2fetcher
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.9"
5
+ platform: ruby
6
+ authors:
7
+ - David Cuadrado
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-05 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.8.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.3
34
+ version:
35
+ description: Download files from a rapidshare free account
36
+ email:
37
+ - krawek@gmail.com
38
+ executables:
39
+ - rr2fetcher
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ - PostInstall.txt
46
+ files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - PostInstall.txt
50
+ - README.rdoc
51
+ - Rakefile
52
+ - bin/rr2fetcher
53
+ - lib/progressbar.rb
54
+ - lib/rr2fetcher.rb
55
+ - lib/rrs_parser.rb
56
+ - rr2fetcher.gemspec
57
+ - script/console
58
+ - script/destroy
59
+ - script/generate
60
+ has_rdoc: true
61
+ homepage: http://github.com/dcu/rr2fetcher
62
+ licenses: []
63
+
64
+ post_install_message: PostInstall.txt
65
+ rdoc_options:
66
+ - --main
67
+ - README.rdoc
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ requirements: []
83
+
84
+ rubyforge_project: rr2fetcher
85
+ rubygems_version: 1.3.5
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Download files from a rapidshare free account
89
+ test_files: []
90
+