ImgurScrot Release29 Mar 2015
So, bit of code I wrote while halfway going mad arguing with API’s at BrumHack in order to retain my sanity a little. This really, really, simple piece of code basically just takes a screenshot using PyScreenshot and uploads it to imgur using the imgur api.
Now, to use it, just run it. You will need “pyscreenshot” and “requests” installed, which are both available via Pip. The rest is stdlib python. All it does is take a screenshot, upload to imgur, and print the URL you can access the image. Grab it from github here.
I guess I will first take the time to explain how the python screenshot library is used. See the following commented code block to see how it does its magic.
Nothing complex about this, if you are interested in the hows and whys of the pyscreenshot modules inner workings, I suggest you have a look at its source code or something.
Obviously, we want to store our image for just long enough to upload it, and writing a tempfile as I originally did is really ugly and tends to be non portable across platforms as tempfile locations vary. So… We want our image as base64, in order to upload it. How do we do this…
Now, because the python imgur library seems to be hideously broken at the moment and I simply could not get the bastard thing to work even with its own samples, I simply decided to use python-requests to manually make my API calls. Seeing as we are just uploading images anonymously, the only thing we give a shit about is the fact we can, er, upload a screenshot.
In order to upload the screenshot, we need to send an Authorization header with our ClientID in it, as part of a POST request containing the base64 encoded image and a title for it. We
get back a JSON response containing the URL to our image. In this example, our “image title” is Screenshot-
Well, that concludes the explaination of how it works. Have fun :)