Skip to content

Instantly share code, notes, and snippets.

@Igglybuff
Last active August 29, 2015 14:17
Show Gist options
  • Save Igglybuff/362eed533b83613a2f2e to your computer and use it in GitHub Desktop.
Save Igglybuff/362eed533b83613a2f2e to your computer and use it in GitHub Desktop.
imgscrot.py
import base65
import json
import requests
import sys
import os
from base64 import b64encode
# cheeky little script which uploads an image to imgur.com then prints the direct link to the image
# add your imgur api key to your environment variables, or just throw it straight into the code.
# consider this...
# alias imgurscrot='tmpfile="$(mktemp -u).png"; scrot "$tmpfile" -s; python ~/path/to/imgur.py "$tmpfile"'
# igglybuff! (:
image = sys.argv
image.remove(image[0])
image = ' '.join(image)
myapikey = os.environ['IMGUR_API_KEY']
headers = {"Authorization": "Client-ID " + myapikey}
url = "https://api.imgur.com/3/upload.json"
j1 = requests.post(
url,
headers = headers,
data = {
'key': myapikey,
'image': b64encode(open(image, 'rb').read()),
'type': 'base64',
'name': '1.jpg',
'title': 'Picture no. 1'
}
)
output = json.loads(j1.text)
output = output['data']
output = output['link']
print output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment