Page Actions
Wiki Actions
User Actions
Submit This Story
Table of Contents

Image Poster

Just write two image posters. Take filename in stdin, usage is like more.

imgur

imgur needs an API dev key, need ot get one from their website.

#!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();

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 poster package to create form data.

#!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();

Forgive my ugly regex patterns…

Discussion

Enter your comment
 
 
blog/2011/07/img_poster.txt · Last modified: 2011/07/05 22:14 by MeaCulpa     Back to top
Recent changes RSS feed Creative Commons License Powered by PHP Driven by DokuWiki