/
ruby-nzb
executable file
·78 lines (63 loc) · 2.17 KB
/
ruby-nzb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env ruby
$: << File.expand_path('../../lib/', __FILE__)
require 'rubygems'
require 'nzb'
require 'optparse'
options = { :blocking => true }
parser = OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} --server news.example.com --output-directory /path/to/output [options] file1.nzb file2.nzb ..."
opts.on('-s', '--server [HOST]', 'The news server to connect to') do |host|
options[:host] = host
end
opts.on('-p', '--port [PORT]', Numeric, 'The port on which to connect to the news server') do |port|
options[:port] = port
end
opts.on('-t', '--threads [SIZE]', Numeric, 'The number of simultaneous connections to make to the news server') do |pool_size|
options[:pool_size] = pool_size
end
opts.on('-o', '--output-directory [PATH]', 'The directory where the downloads should be stored') do |output_directory|
options[:output_directory] = output_directory
end
opts.on('-c', '--disable-curses', 'Disable the usage of Curses and simply prints a line for each status update') do |disable_curses|
options[:disable_curses] = disable_curses
end
end
parser.parse!
def log_message(nzb, message)
"[#{File.basename(nzb.path)} (#{Time.now.strftime('%H:%M:%S')})]: #{message}"
end
begin
raise LoadError if options.delete(:disable_curses)
require 'curses'
require 'set'
# A log method which uses Curses
def log(nzb, message)
@screen ||= Curses.init_screen
(@nzbs_on_screen ||= SortedSet.new) << nzb
@nzbs_on_screen.each_with_index do |nzb_on_screen, index|
if nzb_on_screen == nzb
@screen.setpos(index, 0)
@screen.addstr(log_message(nzb, message))
Curses.refresh
end
end
end
rescue LoadError
# A log method which simply prints if Curses is unavailable.
# Which is on windows if I understood correctly.
def log(nzb, message)
puts log_message(nzb, message)
end
end
if options[:host] and options[:output_directory] and !ARGV.empty?
NZB.setup(options)
ARGV.each do |nzb_file|
NZB.queue(nzb_file).on_update do |nzb|
log(nzb, "#{ '%0.2f' % nzb.downloaded_percentage }%")
end
end
NZB::Connection.start_pool!
else
# Print banner
puts parser
end