aria2rpc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #!/usr/bin/env ruby
  2. # The MIT License
  3. #
  4. # Copyright (c) 2009 Tatsuhiro Tsujikawa
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. require 'xmlrpc/client'
  24. require 'pp'
  25. require 'optparse'
  26. program_name=File.basename($0)
  27. options={}
  28. args=nil
  29. OptionParser.new do |opt|
  30. opt.on("-d","--dir DIR"){|val| options["dir"]=val}
  31. opt.on("-V","--check-integrity [BOOL]", ["true","false"]){|val|
  32. options["check-integrity"]= val||"true"
  33. }
  34. opt.on("-c","--continue [BOOL]",["true","false"]){|val|
  35. options["continue"]=val||"true"
  36. }
  37. opt.on("--all-proxy PROXY"){|val| options["all-proxy"]=val}
  38. opt.on("--all-proxy-user USER"){|val| options["all-proxy-user"]=val}
  39. opt.on("--all-proxy-passwd PASSWD"){|val| options["all-proxy-passwd"]=val}
  40. opt.on("--connect-timeout SEC"){|val| options["connect-timeout"]=val}
  41. opt.on("--dry-run [BOOL]",["true","false"]){|val|
  42. options["dry-run"]=val||"true"
  43. }
  44. opt.on("--lowest-speed-limit SPEED"){|val| options["lowest-speed-limit"]=val}
  45. opt.on("--max-file-not-found NUM"){|val| options["max-file-not-found"]=val}
  46. opt.on("-m","--max-tries N"){|val| options["max-tries"]=val}
  47. opt.on("--no-proxy DOMAINS"){|val| options["no-proxy"]=val}
  48. opt.on("-o","--out FILE"){|val| options["out"]=val}
  49. opt.on("--proxy-method METHOD"){|val| options["proxy-method"]=val}
  50. opt.on("-R","--remote-time [BOOL]",["true","false"]){|val|
  51. options["remote-time"]=val||"true"
  52. }
  53. opt.on("-s","--split N"){|val| options["split"]=val}
  54. opt.on("-t","--timeout SEC"){|val| options["timeout"]=val}
  55. opt.on("--http-auth-challenge [BOOL]",["true","false"]){|val|
  56. options["http-auth-challenge"]=val||"true"
  57. }
  58. opt.on("--http-no-cache [BOOL]",["true","false"]){|val|
  59. options["http-no-cache"]=val||"true"
  60. }
  61. opt.on("--http-user USER"){|val| options["http-user"]=val}
  62. opt.on("--http-passwd PASSWD"){|val| options["http-passwd"]=val}
  63. opt.on("--http-proxy PROXY"){|val| options["http-proxy"]=val}
  64. opt.on("--http-proxy-user USER"){|val| options["http-proxy-user"]=val}
  65. opt.on("--http-proxy-passwd PASSWD"){|val| options["http-proxy-passwd"]=val}
  66. opt.on("--https-proxy PROXY"){|val| options["https-proxy"]=val}
  67. opt.on("--https-proxy-user USER"){|val| options["https-proxy-user"]=val}
  68. opt.on("--https-proxy-passwd PASSWD"){|val| options["https-proxy-passwd"]=val}
  69. opt.on("--referer REFERER"){|val| options["referer"]=val}
  70. opt.on("--enable-http-keep-alive [BOOL]",["true","false"]){|val|
  71. options["enable-http-keep-alive"]=val||"true"
  72. }
  73. opt.on("--enable-http-pipelining [BOOL]",["true","false"]){|val|
  74. options["enable-http-pipelining"]=val||"true"
  75. }
  76. opt.on("--header HEADER"){|val|
  77. options["header"] = [] if options["header"] == nil
  78. options["header"] << val
  79. }
  80. opt.on("--use-head [BOOL]",["true","false"]){|val|
  81. options["use-head"]=val||"true"
  82. }
  83. opt.on("-U","--user-agent USERAGENT"){|val| options["user-agent"]=val}
  84. opt.on("--ftp-user USER"){|val| options["ftp-user"]=val}
  85. opt.on("--ftp-passwd PASSWD"){|val| options["ftp-passwd"]=val}
  86. opt.on("-p","--ftp-pasv [BOOL]",["true","false"]){|val|
  87. options["ftp-pasv"]=val||"true"
  88. }
  89. opt.on("--ftp-proxy PROXY"){|val| options["ftp-proxy"]=val}
  90. opt.on("--ftp-proxy-user USER"){|val| options["ftp-proxy-user"]=val}
  91. opt.on("--ftp-proxy-passwd PASSWD"){|val| options["ftp-proxy-passwd"]=val}
  92. opt.on("--ftp-type TYPE"){|val| options["ftp-type"]=val}
  93. opt.on("--ftp-reuse-connection [BOOL]",["true","false"]){|val|
  94. options["ftp-reuse-connection"]=val||"true"
  95. }
  96. opt.on("-n","--no-netrc [BOOL]",["true","false"]){|val|
  97. options["no-netrc"]=val||"true"
  98. }
  99. opt.on("--reuse-uri [BOOL]",["true","false"]){|val|
  100. options["reuse-uri"]=val||"true"
  101. }
  102. opt.on("--select-file INDEXES"){|val| options["select-file"]=val}
  103. opt.on("--bt-enable-lpd [BOOL]",["true","false"]){|val|
  104. options["bt-enable-lpd"]=val||"true"
  105. }
  106. opt.on("--bt-external-ip IPADDRESS"){|val| options["bt-external-ip"]=val}
  107. opt.on("--bt-hash-check-seed [BOOL]",["true","false"]){|val|
  108. options["bt-hash-check-seed"]=val||"true"
  109. }
  110. opt.on("--bt-max-open-files NUM"){|val| options["bt-max-open-files"]=val}
  111. opt.on("--bt-max-peers NUM"){|val| options["bt-max-peers"]=val}
  112. opt.on("--bt-metadata-only [BOOL]",["true","false"]){|val|
  113. options["bt-metadata-only"]=val||"true"
  114. }
  115. opt.on("--bt-min-crypto-level LEVEL",["plain","arc4"]){|val|
  116. options["bt-min-crypto-level"]=val
  117. }
  118. opt.on("--bt-prioritize-piece RANGE") {|val|
  119. options["bt-prioritize-piece"]=val
  120. }
  121. opt.on("--bt-require-crypto BOOL",["true","false"]){|val|
  122. options["bt-require-crypto"]=val
  123. }
  124. opt.on("--bt-request-peer-speed-limit SPEED"){|val|
  125. options["bt-request-peer-speed-limit"]=val
  126. }
  127. opt.on("--bt-save-metadata [BOOL]",["true","false"]){|val|
  128. options["bt-save-metadata"]=val||"true"
  129. }
  130. opt.on("--bt-seed-unverified [BOOL]",["true","false"]){|val|
  131. options["bt-seed-unverified"]=val||"true"
  132. }
  133. opt.on("--bt-stop-timeout SEC"){|val| options["bt-stop-timeout"]=val}
  134. opt.on("--bt-tracker-interval SEC"){|val| options["bt-tracker-interval"]=val}
  135. opt.on("--enable-peer-exchange [BOOL]",["true","false"]){|val|
  136. options["enable-peer-exchange"]=val||"true"
  137. }
  138. opt.on("--follow-torrent VALUE", ["true","false","mem"]){|val|
  139. options["follow-torrent"]=val
  140. }
  141. opt.on("-O","--index-out INDEXPATH"){|val|
  142. options["index-out"]=[] if options["index-out"] == nil
  143. options["index-out"] << val
  144. }
  145. opt.on("-u","--max-upload-limit SPEED"){|val| options["max-upload-limit"]=val}
  146. opt.on("--seed-ratio RATIO"){|val| options["seed-ratio"]=val}
  147. opt.on("--seed-time MINUTES"){|val| options["seed-time"]=val}
  148. opt.on("--follow-metalink VALUE", ["true","false","mem"]){|val|
  149. options["follow-metalink"]=val
  150. }
  151. opt.on("-C","--metalink-servers NUM"){|val| options["metalink-servers"]=val}
  152. opt.on("--metalink-language LANG"){|val| options["metalink-language"]=val}
  153. opt.on("--metalink-location LOCS"){|val| options["metalink-location"]=val}
  154. opt.on("--metalink-os OS"){|val| options["metalink-os"]=val}
  155. opt.on("--metalink-version VERSION"){|val| options["metalink-version"]=val}
  156. opt.on("--metalink-preferred-protocol PROTO"){|val|
  157. options["metalink-preferred-protocol"]=val
  158. }
  159. opt.on("--metalink-enable-unique-protocol BOOL",["true","false"]){|val|
  160. options["metalink-enable-unique-protocol"]=val
  161. }
  162. opt.on("--allow-overwrite BOOL",["true","false"]){|val|
  163. options["allow-overwrite"]=val
  164. }
  165. opt.on("--allow-piece-length-change BOOL",["true","false"]){|val|
  166. options["allow-piece-length-change"]=val
  167. }
  168. opt.on("--async-dns [BOOL]",["true","false"]){|val|
  169. options["async-dns"]=val||"true"
  170. }
  171. opt.on("--auto-file-renaming [BOOL]",["true","false"]){|val|
  172. options["auto-file-renaming"]=val||"true"
  173. }
  174. opt.on("--file-allocation METHOD",["none","prealloc","falloc"]){|val|
  175. options["file-allocation"]=val
  176. }
  177. opt.on("--max-download-limit LIMIT"){|val| options["max-download-limit"]=val}
  178. opt.on("--no-file-allocation-limit SIZE"){|val|
  179. options["no-file-allocation-limit"]=val
  180. }
  181. opt.on("-P","--parameterized-uri [BOOL]",["true","false"]){|val|
  182. options["parameterized-uri"]=val||"true"
  183. }
  184. opt.on("--realtime-chunk-checksum BOOL",["true","false"]){|val|
  185. options["realtime-chunk-checksum"]=val
  186. }
  187. opt.on("--remove-control-file [BOOL]",["true","false"]){|val|
  188. options["remove-control-file"]=val||"true"
  189. }
  190. opt.on("--max-overall-download-limit LIMIT"){|val| options["max-overall-download-limit"]=val}
  191. opt.on("--max-overall-upload-limit LIMIT"){|val| options["max-overall-upload-limit"]=val}
  192. opt.on("-j","--max-concurrent-downloads N"){|val| options["max-concurrent-downloads"]=val}
  193. opt.on("--server SERVER", "hostname of XML-RPC server. Default: localhost"){|val| options["server"]=val }
  194. opt.on("--port PORT", "port of XML-RPC server. Default: 6800"){|val| options["port"]=val }
  195. opt.on("--user USERNAME", "XML-RPC username"){|val| options["user"]=val }
  196. opt.on("--passwd PASSWORD", "XML-RPC password"){|val| options["passwd"]=val }
  197. opt.banner=<<EOS
  198. Usage: #{program_name} addUri URI... [options]
  199. #{program_name} addTorrent /path/to/torrent_file URI... [options]
  200. #{program_name} addMetalink /path/to/metalink_file [options]
  201. #{program_name} remove GID [options]
  202. #{program_name} changePosition GID pos how [options]
  203. #{program_name} tellStatus GID [options]
  204. #{program_name} tellActive [options]
  205. #{program_name} tellWaiting offset num [options]
  206. #{program_name} tellStopped offset num [options]
  207. #{program_name} getOption GID [options]
  208. #{program_name} getGlobalOption [options]
  209. #{program_name} getFiles GID [options]
  210. #{program_name} getUris GID [options]
  211. #{program_name} getPeers GID [options]
  212. #{program_name} purgeDownloadResult [options]
  213. #{program_name} changeOption GID [options]
  214. #{program_name} changeGlobalOption [options]
  215. #{program_name} getVersion [options]
  216. #{program_name} getSessionInfo [options]
  217. Options:
  218. EOS
  219. args=opt.parse(ARGV)
  220. end
  221. if !args or args.size == 0 then
  222. puts "No command specified"
  223. exit 1
  224. end
  225. command=args[0]
  226. resources=args[1..-1]
  227. auth=""
  228. if options.has_key?("user") then
  229. auth=options["user"]+":"+options["passwd"]+"@"
  230. end
  231. if not options.has_key?("server") then
  232. options["server"]="localhost"
  233. end
  234. if not options.has_key?("port") then
  235. options["port"]="6800"
  236. end
  237. client=XMLRPC::Client.new3({:host => options["server"],
  238. :port => options["port"],
  239. :path => "/rpc",
  240. :user => options["user"],
  241. :password => options["passwd"]})
  242. options.delete("server")
  243. options.delete("port")
  244. options.delete("user")
  245. options.delete("passwd")
  246. if command == "addUri" then
  247. result=client.call("aria2."+command, resources, options)
  248. elsif command == "addTorrent" then
  249. torrentData=IO.read(resources[0])
  250. result=client.call("aria2."+command,
  251. XMLRPC::Base64.new(torrentData), resources[1..-1], options)
  252. elsif command == "addMetalink" then
  253. metalinkData=IO.read(resources[0])
  254. result=client.call("aria2."+command,
  255. XMLRPC::Base64.new(metalinkData), options)
  256. elsif command == "tellStatus" then
  257. result=client.call("aria2."+command, resources[0])
  258. elsif command == "tellActive" then
  259. result=client.call("aria2."+command)
  260. elsif command == "tellWaiting" then
  261. result=client.call("aria2."+command, resources[0].to_i(), resources[1].to_i())
  262. elsif command == "tellStopped" then
  263. result=client.call("aria2."+command, resources[0].to_i(), resources[1].to_i())
  264. elsif command == "getOption" then
  265. result=client.call("aria2."+command, resources[0])
  266. elsif command == "getGlobalOption" then
  267. result=client.call("aria2."+command)
  268. elsif command == "remove" then
  269. result=client.call("aria2."+command, resources[0])
  270. elsif command == "changePosition" then
  271. result=client.call("aria2."+command, resources[0], resources[1].to_i(),
  272. resources[2])
  273. elsif command == "getFiles" then
  274. result=client.call("aria2."+command, resources[0])
  275. elsif command == "getUris" then
  276. result=client.call("aria2."+command, resources[0])
  277. elsif command == "getPeers" then
  278. result=client.call("aria2."+command, resources[0])
  279. elsif command == "purgeDownloadResult" then
  280. result=client.call("aria2."+command)
  281. elsif command == "changeOption" then
  282. result=client.call("aria2."+command, resources[0], options)
  283. elsif command == "changeGlobalOption" then
  284. result=client.call("aria2."+command, options)
  285. elsif command == "getVersion" then
  286. result=client.call("aria2."+command)
  287. elsif command == "getSessionInfo" then
  288. result=client.call("aria2."+command)
  289. else
  290. puts "Command not recognized"
  291. exit 1
  292. end
  293. pp result