On Thu, Aug 2, 2012 at 11:59 AM, David Nadlinger <span dir="ltr"><<a href="mailto:code@klickverbot.at" target="_blank">code@klickverbot.at</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Thu, Aug 2, 2012 at 7:53 PM, Walter Bright <<a href="mailto:walter@digitalmars.com">walter@digitalmars.com</a>> wrote:<br>
> Can you make a script for me that uploads to the github dmd download page?<br>
<br>
</div><a href="http://developer.github.com/v3/repos/downloads/+" target="_blank">http://developer.github.com/v3/repos/downloads/+</a><br>
<br>
Good opportunity to dog-food std.net.curl.<br>
<span class="HOEnZb"><font color="#888888"><br>
David<br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
dmd-beta mailing list<br>
<a href="mailto:dmd-beta@puremagic.com">dmd-beta@puremagic.com</a><br>
<a href="http://lists.puremagic.com/mailman/listinfo/dmd-beta" target="_blank">http://lists.puremagic.com/mailman/listinfo/dmd-beta</a><br>
</div></div></blockquote></div><br><div>This is as far as I got before giving up due to libcurl's low level interface (std.net.curl doesn't have a way to upload a file with extra fields, if it did this would have been amazingly simple):</div>
<div><br></div><div><div>#!/usr/bin/rdmd</div><div><br></div><div>import std.stdio, std.net.curl, std.string, std.json, std.conv;</div><div>import etc.c.curl;</div><div><br></div><div>void main(string[] args)</div><div>{</div>
<div> write("GitHub username: ");</div><div> auto username = readln().chomp();</div><div><br></div><div> write("GitHub password: ");</div><div> auto password = readln().chomp();</div><div><br>
</div><div> write("GitHub repo: ");</div><div> auto repo = readln().chomp();</div><div><br></div><div> write("Description: ");</div><div> auto desc = readln().chomp();</div><div><br></div><div>
write("MIME type: ");</div><div> auto mime = readln().chomp();</div><div><br></div><div> auto client = HTTP();</div><div> client.setAuthentication(username, password);</div><div><br></div><div> auto create_url = "<a href="https://api.github.com/repos/">https://api.github.com/repos/</a>" ~ username ~ "/" ~ repo</div>
<div> ~ "/downloads";</div><div><br></div><div> auto create_json = format(</div><div> q"#{ "name": "%s", "size": %d, "description": "%s",</div>
<div> "content_type": "%s" }#",</div><div> args[1], File(args[1]).size(), desc, mime);</div><div> auto create_response = post(create_url, create_json, client);</div><div> auto cj = parseJSON(create_response);</div>
<div><br></div><div> Curl curl;</div><div> curl.initialize();</div><div> curl.set(CurlOption.url, cj["s3_url"].str);</div><div><br></div><div><br></div><div> // missing from etc.c.curl</div><div> enum CURLFORM_COPYNAME = 1;</div>
<div> enum CURLFORM_COPYCONTENTS = 4;</div><div> enum CURLFORM_FILE = 10;</div><div> enum CURLFORM_CONTENTTYPE = 14;</div><div> enum CURLFORM_END = 17;</div><div><br></div><div> curl_httppost *post=null;</div>
<div> curl_httppost *last=null;</div><div> void formadd(string key, string value) {</div><div> curl_formadd(&post, &last, CURLFORM_COPYNAME, key.toStringz(),</div><div> CURLFORM_COPYCONTENTS, value.toStringz(), CURLFORM_END);</div>
<div> }</div><div> formadd("key", cj["path"].str);</div><div> formadd("acl", cj["acl"].str);</div><div> formadd("success_action_status", "201");</div>
<div> formadd("Filename", cj["name"].str);</div><div> formadd("AWSAccessKeyId", cj["accesskeyid"].str);</div><div> formadd("Policy", cj["policy"].str);</div>
<div> formadd("Signature", cj["signature"].str);</div><div> curl_formadd(&post, &last,</div><div> CURLFORM_COPYNAME, "Content-Type",</div><div> CURLFORM_CONTENTTYPE, cj["mime_type"].str.toStringz(),</div>
<div> CURLFORM_END);</div><div> curl_formadd(&post, &last,</div><div> CURLFORM_COPYNAME, "file",</div><div> CURLFORM_FILE, args[1].toStringz(),</div><div> CURLFORM_END);</div>
<div> curl_formadd(&post, &last,</div><div> CURLFORM_COPYNAME, "name",</div><div> CURLFORM_COPYCONTENTS, cj["name"].str.toStringz(),</div><div> CURLFORM_END);</div>
<div> curl.set(CurlOption.httppost, post);</div><div> curl.onProgress = delegate int(size_t dltotal, size_t dlnow, size_t ultotal,</div><div> size_t ulnow)</div><div> {</div><div> writeln("Progress: uploaded bytes ", ulnow, " of ", ultotal);</div>
<div> return 0;</div><div> };</div><div> curl.perform();</div><div> curl_formfree(post);</div><div>}</div><div><br></div></div>