jphastings-jd-control 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/jd-control.rb +100 -40
  2. metadata +11 -1
data/jd-control.rb CHANGED
@@ -5,6 +5,8 @@
5
5
  require 'extensions'
6
6
  require 'rubygems'
7
7
  require 'httparty'
8
+ require 'tempfile'
9
+ require 'hpricot'
8
10
 
9
11
  module JDownloader
10
12
  # Allows control of JDownloader via the 'Remote Control' plugin
@@ -14,7 +16,7 @@ module JDownloader
14
16
  include HTTParty
15
17
 
16
18
  # The ip address of the computer running JDownloader (probably 127.0.0.1)
17
- def initialize(host = "127.0.0.1",port = 5000) # Can't remember the default port!
19
+ def initialize(host = "127.0.0.1",port = 10025)
18
20
  self.class.base_uri "http://#{host}:#{port}/"
19
21
  end
20
22
 
@@ -55,13 +57,28 @@ module JDownloader
55
57
  end
56
58
 
57
59
  # Creates a new package with the given array of links
58
- def download(links)
60
+ def add_link(links)
59
61
  links = [links] if links.is_a?(String)
60
62
  self.class.get("/action/add/links/grabber0/start1/"+links.join(" "))
61
63
  end
62
64
 
65
+ alias :add_links :add_link
66
+
67
+ # Will add a DLC to the download queue, pass the DLC or a local file
68
+ def add_dlc(dlc_or_file)
69
+ if dlc_or_file.is_a?(String)
70
+ file = Tempfile.open('dlc') do |f|
71
+ f.write dlc_or_file
72
+ end
73
+ dlc_or_file = file.path
74
+ else
75
+ raise "That file does not exist" if not File.exists?(dlc_or_file)
76
+ end
77
+ self.class.get("/action/add/container/#{dlc_or_file}")
78
+ end
79
+
63
80
  # Lists the details of any download or downloads (by id) or all downloads.
64
- def download(downloadids = nil)
81
+ def packages(downloadids = nil)
65
82
  downloadids = [downloadids] if downloadids.is_a?(Integer)
66
83
 
67
84
  dls = parse_packages(self.class.get("/get/downloads/alllist"))
@@ -71,37 +88,39 @@ module JDownloader
71
88
  return dls.delete_if {|id, package| not downloadids.include?(id)}
72
89
  end
73
90
  end
74
- alias :downloads :download
91
+ alias :package :packages
75
92
 
76
93
  private
77
94
  def parse_packages(string)
78
- Hash[*string.scan(/<package package_name="(.*?)" package_id="([0-9]+?)" package_percent="([0-9]+?\.[0-9]+?)" package_linksinprogress="([0-9]+?)" package_linkstotal="([0-9]+?)" package_ETA="((?:[0-9]+:)?[0-9]{2}:(?:-1|[0-9]{2}))" package_speed="([0-9]+?) ([G|M|K]B)\/s" package_loaded="([0-9]+?\.?[0-9]*?) ([G|M|K]B)" package_size="([0-9]+?\.[0-9]+?) ([G|M|K]B)" package_todo="([0-9]+?\.?[0-9]*?) ([G|M|K]B)" ?> ?(.*?)<\/package>/).collect { |p|
95
+ return {} if string.nil?
96
+
97
+ Hash[*Hpricot(string).search("package").collect { |package|
79
98
  m = nil
80
- [p[1].to_i, Package.new({
81
- :name => p[0],
82
- :id => p[1].to_i,
99
+ [package.attributes['package_id'].to_i, Package.new({
100
+ :name => package.attributes['package_name'],
101
+ :id => package.attributes['package_id'].to_i,
83
102
  :links => {
84
- :in_progress => p[3].to_i,
85
- :in_total => p[4].to_i
103
+ :in_progress => package.attributes['package_linksinprogress'].to_i,
104
+ :in_total => package.attributes['package_linkstotal'].to_i
86
105
  },
87
- :eta => (p[5] == "00:-1") ? nil : p[5].split(":").reverse.inject(0) { |sum, element| m = ((m.nil?) ? 1 : m*60 ); sum + element.to_i*m },
88
- :speed => p[6].to_i * parse_bytes(p[7]),
89
- :completed => p[2].to_f/100,
106
+ :eta => (package.attributes['package_ETA'] == "00:-1") ? nil : package.attributes['package_ETA'].split(":").reverse.inject(0) { |sum, element| m = ((m.nil?) ? 1 : m*60 ); sum + element.to_i*m },
107
+ :speed => package.attributes['package_speed'].split(" ")[0].to_f * parse_bytes(package.attributes['package_speed'].split(" ")[1]),
108
+ :completed => package.attributes['package_percent'].to_f/100,
90
109
  :size => {
91
- :loaded => p[8].to_i * parse_bytes(p[9]),
92
- :total => p[10].to_i * parse_bytes(p[11]),
93
- :todo => p[12].to_i * parse_bytes(p[13])
110
+ :loaded => package.attributes['package_loaded'].split(" ")[0].to_f * parse_bytes(package.attributes['package_loaded'].split(" ")[1]),
111
+ :total => package.attributes['package_size'].split(" ")[0].to_f * parse_bytes(package.attributes['package_size'].split(" ")[1]),
112
+ :todo => package.attributes['package_todo'].split(" ")[0].to_f * parse_bytes(package.attributes['package_todo'].split(" ")[1])
94
113
  },
95
- :files => Hash[*p[14].scan(/<file file_name="(.*?)" file_id="([0-9]+?)" file_package="([0-9]+?)" file_percent="([0-9]+?\.[0-9]+?)" file_hoster="(.*?)" file_status="(.*?)" file_speed="(-1|[0-9]+?)" ?>/).collect { |f|
96
- [f[1].to_i,{
97
- :name => f[0],
98
- :id => f[1].to_i,
99
- :package_id => f[2].to_i,
100
- :completed => f[3].to_f/100,
101
- :hoster => f[4],
102
- :status => parse_status(f[5]),
103
- :speed => (f[6] == "-1") ? nil : f[6].to_i
104
- }]
114
+ :files => Hash[*package.search("file").collect { |file|
115
+ [file.attributes['file_id'].to_i,JDownloader::File.new({
116
+ :name => file.attributes['file_name'],
117
+ :id => file.attributes['file_id'].to_i,
118
+ #:package_id => file.attributes['file_package'].to_i,
119
+ :completed => file.attributes['file_percent'].to_f/100,
120
+ :hoster => file.attributes['file_hoster'],
121
+ :status => parse_status(file.attributes['file_status']),
122
+ #:speed => (file.attributes['file_speed'] == "-1") ? nil : file.attributes['file_speed'].to_i
123
+ })]
105
124
  }.flatten]
106
125
 
107
126
  })]
@@ -111,12 +130,14 @@ module JDownloader
111
130
 
112
131
  def parse_bytes(bytes)
113
132
  case bytes
114
- when "GB"
133
+ when "GB","GB/s"
115
134
  return 1048576
116
- when "MB"
135
+ when "MB","MB/s"
117
136
  return 1024
118
- when "KB"
137
+ when "KB","KB/s"
119
138
  return 1
139
+ when "B","B/s"
140
+ return 1/1024
120
141
  else
121
142
  raise "Unknown unit: #{bytes}"
122
143
  end
@@ -128,27 +149,36 @@ module JDownloader
128
149
  {
129
150
  :description => :finished
130
151
  }
131
- when /^Wait ([0-9]{2}):([0-9]{2}) min(?:\. for (.+))?$/
152
+ when /^Wait ([0-9]{2})?:?([0-9]{2}):([0-9]{2}) min(?:\. for (.+))?$/
132
153
  {
133
154
  :description => :wait,
134
- :wait => $3,
135
- :time => $1.to_i * 60 + $2.to_i
155
+ :wait => $4,
156
+ :time => ETA.new($1.to_i * 3600 + $2.to_i * 60 + $3.to_i)
157
+ }
158
+ when "Connecting..."
159
+ {
160
+ :description => :wait,
161
+ :wait => :connecting
136
162
  }
137
163
  when "[wait for new ip]"
138
164
  {
139
165
  :description => :wait,
140
166
  :wait => :new_ip
141
167
  }
142
- when /ETA ([0-9]{2}):([0-9]{2}) @ ([0-9]+) ([M|K]B)\/s \([0-9]+\/[0-9]+\)/ # What are these last two digitas? Download slots used and available?
168
+ when /^ETA ([0-9]{2})?:?([0-9]{2}):([0-9]{2}) @ ([0-9]+\.[0-9]+) ([G|M|K]?B)\/s \(([0-9]+)\/([0-9]+)\)$/ # What are these last two digitas? Download slots used and available?
143
169
  {
144
170
  :description => :in_progress,
145
- :time => $1.to_i * 60 + $2.to_i,
146
- :speed => $3.to_i * parse_bytes($4)
171
+ :time => ETA.new($1.to_i * 3600 + $2.to_i * 60 + $3.to_i),
172
+ :speed => $4.to_f * parse_bytes($5),
173
+ :slots => {
174
+ :used => $6.to_i,
175
+ :free => $7.to_i - $6.to_i,
176
+ :total => $7.to_i
177
+ }
147
178
  }
148
179
  when ""
149
180
  {
150
- :description => :wait,
151
- :wait => :queued
181
+ :description => "Unknown"
152
182
  }
153
183
  else
154
184
  status
@@ -156,17 +186,47 @@ module JDownloader
156
186
  end
157
187
  end
158
188
 
189
+ # JDownloader packages contain files, this class allows interrogation of the files
190
+ class File
191
+ attr_reader :name, :id, :hoster, :status, :completed, :speed, :eta
192
+ def initialize(f)
193
+ @name = f[:name]
194
+ @id = f[:id]
195
+ @hoster = f[:hoster]
196
+ @status = f[:status]
197
+ @completed = Percentage.new(f[:completed])
198
+ @status = {:description => :finished} if @completed == 1
199
+ @speed = f[:status][:speed] if not f[:status][:speed].nil?
200
+ @eta = f[:status][:time] if not f[:status][:time].nil?
201
+ end
202
+
203
+ # Is the file waiting to be downloaded?
204
+ def waiting?
205
+ @status[:description] == :wait
206
+ end
207
+
208
+ # Is the file completed?
209
+ def finished?
210
+ @status[:description] == :finished
211
+ end
212
+ alias :completed? :finished?
213
+
214
+ def inspect
215
+ "#{@name}"
216
+ end
217
+ end
218
+
159
219
  # JDownloader packages can be accessed as objects, they're almost totally intuitive
160
220
  class Package
161
- attr_reader :name, :id, :eta, :speed, :completed, :size, :files
221
+ attr_reader :name, :id, :eta, :speed, :completed, :size, :files, :status
162
222
  def initialize(p)
163
223
  @name = p[:name]
164
224
  @id = p[:id]
165
- @package = p[:package_id]
225
+ #@package = p[:package_id]
166
226
  @eta = (p[:completed] >= 1.0) ? "finished" : (p[:eta].nil?)? "unknown" : ETA.new(p[:eta])
167
227
  @speed = p[:speed]
168
228
  @completed = Percentage.new(p[:completed])
169
- @file = p[:files]
229
+ @files = p[:files]
170
230
  end
171
231
 
172
232
  def inspect
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jphastings-jd-control
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Hastings-Spital
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hpricot
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
25
35
  description: Control JDownloader from ruby via the 'Remote Control' plugin
26
36
  email: jd-control@projects.kedakai.co.uk
27
37
  executables: []