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.
- data/jd-control.rb +100 -40
- 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 =
|
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
|
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
|
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 :
|
91
|
+
alias :package :packages
|
75
92
|
|
76
93
|
private
|
77
94
|
def parse_packages(string)
|
78
|
-
|
95
|
+
return {} if string.nil?
|
96
|
+
|
97
|
+
Hash[*Hpricot(string).search("package").collect { |package|
|
79
98
|
m = nil
|
80
|
-
[
|
81
|
-
:name =>
|
82
|
-
:id =>
|
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 =>
|
85
|
-
:in_total =>
|
103
|
+
:in_progress => package.attributes['package_linksinprogress'].to_i,
|
104
|
+
:in_total => package.attributes['package_linkstotal'].to_i
|
86
105
|
},
|
87
|
-
:eta => (
|
88
|
-
:speed =>
|
89
|
-
:completed =>
|
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 =>
|
92
|
-
:total =>
|
93
|
-
:todo =>
|
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[*
|
96
|
-
[
|
97
|
-
:name =>
|
98
|
-
:id =>
|
99
|
-
|
100
|
-
:completed =>
|
101
|
-
:hoster =>
|
102
|
-
:status => parse_status(
|
103
|
-
|
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 => $
|
135
|
-
:time => $1.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
|
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 *
|
146
|
-
:speed => $
|
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 =>
|
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
|
-
|
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
|
-
@
|
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.
|
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: []
|