aria2rpc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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("--connect-timeout SEC"){|val| options["connect-timeout"]=val}
  39. opt.on("--dry-run [BOOL]",["true","false"]){|val|
  40. options["dry-run"]=val||"true"
  41. }
  42. opt.on("--lowest-speed-limit SPEED"){|val| options["lowest-speed-limit"]=val}
  43. opt.on("--max-file-not-found NUM"){|val| options["max-file-not-found"]=val}
  44. opt.on("-m","--max-tries N"){|val| options["max-tries"]=val}
  45. opt.on("--no-proxy DOMAINS"){|val| options["no-proxy"]=val}
  46. opt.on("-o","--out FILE"){|val| options["out"]=val}
  47. opt.on("--proxy-method METHOD"){|val| options["proxy-method"]=val}
  48. opt.on("-R","--remote-time [BOOL]",["true","false"]){|val|
  49. options["remote-time"]=val||"true"
  50. }
  51. opt.on("-s","--split N"){|val| options["split"]=val}
  52. opt.on("-t","--timeout SEC"){|val| options["timeout"]=val}
  53. opt.on("--http-auth-challenge [BOOL]",["true","false"]){|val|
  54. options["http-auth-challenge"]=val||"true"
  55. }
  56. opt.on("--http-no-cache [BOOL]",["true","false"]){|val|
  57. options["http-no-cache"]=val||"true"
  58. }
  59. opt.on("--http-user USER"){|val| options["http-user"]=val}
  60. opt.on("--http-passwd PASSWD"){|val| options["http-passwd"]=val}
  61. opt.on("--http-proxy PROXY"){|val| options["http-proxy"]=val}
  62. opt.on("--https-proxy PROXY"){|val| options["https-proxy"]=val}
  63. opt.on("--referer REFERER"){|val| options["referer"]=val}
  64. opt.on("--enable-http-keep-alive [BOOL]",["true","false"]){|val|
  65. options["enable-http-keep-alive"]=val||"true"
  66. }
  67. opt.on("--enable-http-pipelining [BOOL]",["true","false"]){|val|
  68. options["enable-http-pipelining"]=val||"true"
  69. }
  70. opt.on("--header HEADER"){|val|
  71. options["header"] = [] if options["header"] == nil
  72. options["header"] << val
  73. }
  74. opt.on("--use-head [BOOL]",["true","false"]){|val|
  75. options["use-head"]=val||"true"
  76. }
  77. opt.on("-U","--user-agent USERAGENT"){|val| options["user-agent"]=val}
  78. opt.on("--ftp-user USER"){|val| options["ftp-user"]=val}
  79. opt.on("--ftp-passwd PASSWD"){|val| options["ftp-passwd"]=val}
  80. opt.on("-p","--ftp-pasv [BOOL]",["true","false"]){|val|
  81. options["ftp-pasv"]=val||"true"
  82. }
  83. opt.on("--ftp-proxy PROXY"){|val| options["ftp-proxy"]=val}
  84. opt.on("--ftp-type TYPE"){|val| options["ftp-type"]=val}
  85. opt.on("--ftp-reuse-connection [BOOL]",["true","false"]){|val|
  86. options["ftp-reuse-connection"]=val||"true"
  87. }
  88. opt.on("-n","--no-netrc [BOOL]",["true","false"]){|val|
  89. options["no-netrc"]=val||"true"
  90. }
  91. opt.on("--select-file INDEXES"){|val| options["select-file"]=val}
  92. opt.on("--bt-external-ip IPADDRESS"){|val| options["bt-external-ip"]=val}
  93. opt.on("--bt-hash-check-seed [BOOL]",["true","false"]){|val|
  94. options["bt-hash-check-seed"]=val||"true"
  95. }
  96. opt.on("--bt-max-open-files NUM"){|val| options["bt-max-open-files"]=val}
  97. opt.on("--bt-max-peers NUM"){|val| options["bt-max-peers"]=val}
  98. opt.on("--bt-metadata-only [BOOL]",["true","false"]){|val|
  99. options["bt-metadata-only"]=val||"true"
  100. }
  101. opt.on("--bt-min-crypto-level LEVEL",["plain","arc4"]){|val|
  102. options["bt-min-crypto-level"]=val
  103. }
  104. opt.on("--bt-prioritize-piece RANGE") {|val|
  105. options["bt-prioritize-piece"]=val
  106. }
  107. opt.on("--bt-require-crypto BOOL",["true","false"]){|val|
  108. options["bt-require-crypto"]=val
  109. }
  110. opt.on("--bt-request-peer-speed-limit SPEED"){|val|
  111. options["bt-request-peer-speed-limit"]=val
  112. }
  113. opt.on("--bt-save-metadata [BOOL]",["true","false"]){|val|
  114. options["bt-save-metadata"]=val||"true"
  115. }
  116. opt.on("--bt-seed-unverified [BOOL]",["true","false"]){|val|
  117. options["bt-seed-unverified"]=val||"true"
  118. }
  119. opt.on("--bt-stop-timeout SEC"){|val| options["bt-stop-timeout"]=val}
  120. opt.on("--bt-tracker-interval SEC"){|val| options["bt-tracker-interval"]=val}
  121. opt.on("--enable-peer-exchange [BOOL]",["true","false"]){|val|
  122. options["enable-peer-exchange"]=val||"true"
  123. }
  124. opt.on("--follow-torrent VALUE", ["true","false","mem"]){|val|
  125. options["follow-torrent"]=val
  126. }
  127. opt.on("-O","--index-out INDEXPATH"){|val|
  128. options["index-out"]=[] if options["index-out"] == nil
  129. options["index-out"] << val
  130. }
  131. opt.on("-u","--max-upload-limit SPEED"){|val| options["max-upload-limit"]=val}
  132. opt.on("--seed-ratio RATIO"){|val| options["seed-ratio"]=val}
  133. opt.on("--seed-time MINUTES"){|val| options["seed-time"]=val}
  134. opt.on("--follow-metalink VALUE", ["true","false","mem"]){|val|
  135. options["follow-metalink"]=val
  136. }
  137. opt.on("-C","--metalink-servers NUM"){|val| options["metalink-servers"]=val}
  138. opt.on("--metalink-language LANG"){|val| options["metalink-language"]=val}
  139. opt.on("--metalink-location LOCS"){|val| options["metalink-location"]=val}
  140. opt.on("--metalink-os OS"){|val| options["metalink-os"]=val}
  141. opt.on("--metalink-version VERSION"){|val| options["metalink-version"]=val}
  142. opt.on("--metalink-preferred-protocol PROTO"){|val|
  143. options["metalink-preferred-protocol"]=val
  144. }
  145. opt.on("--metalink-enable-unique-protocol BOOL",["true","false"]){|val|
  146. options["metalink-enable-unique-protocol"]=val
  147. }
  148. opt.on("--allow-overwrite BOOL",["true","false"]){|val|
  149. options["allow-overwrite"]=val
  150. }
  151. opt.on("--allow-piece-length-change BOOL",["true","false"]){|val|
  152. options["allow-piece-length-change"]=val
  153. }
  154. opt.on("--async-dns [BOOL]",["true","false"]){|val|
  155. options["async-dns"]=val||"true"
  156. }
  157. opt.on("--auto-file-renaming [BOOL]",["true","false"]){|val|
  158. options["auto-file-renaming"]=val||"true"
  159. }
  160. opt.on("--file-allocation METHOD",["none","prealloc","falloc"]){|val|
  161. options["file-allocation"]=val
  162. }
  163. opt.on("--max-download-limit LIMIT"){|val| options["max-download-limit"]=val}
  164. opt.on("--no-file-allocation-limit SIZE"){|val|
  165. options["no-file-allocation-limit"]=val
  166. }
  167. opt.on("-P","--parameterized-uri [BOOL]",["true","false"]){|val|
  168. options["parameterized-uri"]=val||"true"
  169. }
  170. opt.on("--realtime-chunk-checksum BOOL",["true","false"]){|val|
  171. options["realtime-chunk-checksum"]=val
  172. }
  173. opt.on("--max-overall-download-limit LIMIT"){|val| options["max-overall-download-limit"]=val}
  174. opt.on("--max-overall-upload-limit LIMIT"){|val| options["max-overall-upload-limit"]=val}
  175. opt.on("-j","--max-concurrent-downloads N"){|val| options["max-concurrent-downloads"]=val}
  176. opt.on("--server SERVER", "hostname of XML-RPC server. Default: localhost"){|val| options["server"]=val }
  177. opt.on("--port PORT", "port of XML-RPC server. Default: 6800"){|val| options["port"]=val }
  178. opt.on("--user USERNAME", "XML-RPC username"){|val| options["user"]=val }
  179. opt.on("--passwd PASSWORD", "XML-RPC password"){|val| options["passwd"]=val }
  180. opt.banner=<<EOS
  181. Usage: #{program_name} addUri URI... [options]
  182. #{program_name} addTorrent /path/to/torrent_file URI... [options]
  183. #{program_name} addMetalink /path/to/metalink_file [options]
  184. #{program_name} remove GID [options]
  185. #{program_name} changePosition GID pos how [options]
  186. #{program_name} tellStatus GID [options]
  187. #{program_name} tellActive [options]
  188. #{program_name} tellWaiting offset num [options]
  189. #{program_name} tellStopped offset num [options]
  190. #{program_name} getOption GID [options]
  191. #{program_name} getGlobalOption [options]
  192. #{program_name} getFiles GID [options]
  193. #{program_name} getUris GID [options]
  194. #{program_name} getPeers GID [options]
  195. #{program_name} purgeDownloadResult [options]
  196. #{program_name} changeOption GID [options]
  197. #{program_name} changeGlobalOption [options]
  198. #{program_name} getVersion [options]
  199. #{program_name} getSessionInfo [options]
  200. Options:
  201. EOS
  202. args=opt.parse(ARGV)
  203. end
  204. if !args or args.size == 0 then
  205. puts "No command specified"
  206. exit 1
  207. end
  208. command=args[0]
  209. resources=args[1..-1]
  210. auth=""
  211. if options.has_key?("user") then
  212. auth=options["user"]+":"+options["passwd"]+"@"
  213. end
  214. if not options.has_key?("server") then
  215. options["server"]="localhost"
  216. end
  217. if not options.has_key?("port") then
  218. options["port"]="6800"
  219. end
  220. client=XMLRPC::Client.new3({:host => options["server"],
  221. :port => options["port"],
  222. :path => "/rpc",
  223. :user => options["user"],
  224. :password => options["passwd"]})
  225. options.delete("server")
  226. options.delete("port")
  227. options.delete("user")
  228. options.delete("passwd")
  229. if command == "addUri" then
  230. result=client.call("aria2."+command, resources, options)
  231. elsif command == "addTorrent" then
  232. torrentData=IO.read(resources[0])
  233. result=client.call("aria2."+command,
  234. XMLRPC::Base64.new(torrentData), resources[1..-1], options)
  235. elsif command == "addMetalink" then
  236. metalinkData=IO.read(resources[0])
  237. result=client.call("aria2."+command,
  238. XMLRPC::Base64.new(metalinkData), options)
  239. elsif command == "tellStatus" then
  240. result=client.call("aria2."+command, resources[0])
  241. elsif command == "tellActive" then
  242. result=client.call("aria2."+command)
  243. elsif command == "tellWaiting" then
  244. result=client.call("aria2."+command, resources[0].to_i(), resources[1].to_i())
  245. elsif command == "tellStopped" then
  246. result=client.call("aria2."+command, resources[0].to_i(), resources[1].to_i())
  247. elsif command == "getOption" then
  248. result=client.call("aria2."+command, resources[0])
  249. elsif command == "getGlobalOption" then
  250. result=client.call("aria2."+command)
  251. elsif command == "remove" then
  252. result=client.call("aria2."+command, resources[0])
  253. elsif command == "changePosition" then
  254. result=client.call("aria2."+command, resources[0], resources[1].to_i(),
  255. resources[2])
  256. elsif command == "getFiles" then
  257. result=client.call("aria2."+command, resources[0])
  258. elsif command == "getUris" then
  259. result=client.call("aria2."+command, resources[0])
  260. elsif command == "getPeers" then
  261. result=client.call("aria2."+command, resources[0])
  262. elsif command == "purgeDownloadResult" then
  263. result=client.call("aria2."+command)
  264. elsif command == "changeOption" then
  265. result=client.call("aria2."+command, resources[0], options)
  266. elsif command == "changeGlobalOption" then
  267. result=client.call("aria2."+command, options)
  268. elsif command == "getVersion" then
  269. result=client.call("aria2."+command)
  270. elsif command == "getSessionInfo" then
  271. result=client.call("aria2."+command)
  272. else
  273. puts "Command not recognized"
  274. exit 1
  275. end
  276. pp result