Created
July 4, 2014 16:19
-
-
Save jarek-przygodzki/a874f2d41eca85f17fe6 to your computer and use it in GitHub Desktop.
aria2 add download using JSON-RPC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* scriptcs -install Newtonsoft.Json | |
* scriptcs -install CommandLineParser | |
*/ | |
using System.Net; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using CommandLine; | |
using CommandLine.Text; | |
class Options | |
{ | |
[Option('u', "uri", Required = true, HelpText = "File URI")] | |
public string Uri { get; set; } | |
[Option('h', "host", Required = false, HelpText = "Address of aria2 server", DefaultValue="http://localhost:6800/jsonrpc")] | |
public string Host { get; set; } | |
[Option('o', "output", Required = false, HelpText = "Output file name")] | |
public string OutputFile { get; set; } | |
[HelpOption] | |
public string GetUsage() | |
{ | |
return HelpText.AutoBuild(this, | |
(HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current)); | |
} | |
} | |
string BuildJsonRequest(string uri, string name=null) | |
{ | |
var jsonObject = new JObject(); | |
jsonObject["jsonrpc"] = "2.0"; | |
jsonObject["method"] = "aria2.addUri"; | |
jsonObject["id"] = "1"; | |
var requestParams = new JArray(); | |
var uris = new JArray(); | |
uris.Add(uri); | |
requestParams.Add(uris); | |
if (name != null) | |
{ | |
var options = new JObject(); | |
options["out"] = name; | |
requestParams.Add(options); | |
} | |
jsonObject["params"] = requestParams; | |
string json = JsonConvert.SerializeObject(jsonObject); | |
//var json = jsonObject.ToString(); | |
return json; | |
} | |
var options = new Options(); | |
if (CommandLine.Parser.Default.ParseArguments(Env.ScriptArgs.ToArray(), options)) | |
{ | |
var jsonRequest = BuildJsonRequest(options.Uri, options.OutputFile); | |
using (var webClient = new WebClient()) | |
{ | |
var response = webClient.UploadString(options.Host, "POST", jsonRequest); | |
Console.WriteLine(response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment