~infernoxu/ucarenya/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
====== Image Poster ======

Just write two image posters. Take filename in stdin, usage is like [[man>more]].

==== imgur ====
[[http://imgur.com/|imgur]] needs an API dev key, need ot get one from their website.
<code python>

#!usr/bin/python
with open(__import__("sys").stdin.read(), "r+b") as fi: content = __import__("base64").b64encode(fi.read()); 
fw = __import__("urllib2").urlopen("http://api.imgur.com/2/upload", __import__("urllib").urlencode({"key" : "Get Key From http://imgur.com/register/api_anon", "image" : content,})); 
print (__import__("re").search("\<original\>(.*)\<\/original\>", fw.read())).group(1); fw.close();

</code>

==== imm.io ====
[[http://imm.io/|imm.io]] is more simple... But its API is form based, more reliable but not so easy as simple url requests toying above. 
For Python I use the [[http://atlee.ca/software/poster/|poster]] package to create form data. 

<code python>
#!usr/bin/python

# Register the streaming http handlers with urllib2
__import__("poster").streaminghttp.register_openers()

# Start the multipart/form-data encoding of the file read
with open(__import__("sys").stdin.read(), "r+b") as fi: 
    datagen, headers = __import__("poster").encode.multipart_encode({"image": fi})

    # Create the Request object
    request = __import__("urllib2").Request("http://imm.io/store", datagen, headers)
    # Actually do the request, and get the response
    fw = __import__("urllib2").urlopen(request)
    print (__import__("re").search("uri\":\"(.*)\",\"link", fw.read())).group(1); fw.close();

</code>

Forgive my ugly regex patterns...

{{tag>web media python cli}}

~~LINKBACK~~
~~DISQUS~~