aria2rpc 10 KB

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