Instant upload images to imgur from android

Python needs to be embraced for it's capacity to gel with android so well and make things easier with simple and fun scripts.

After installing G+ android app, /me was curious about instant upload of images that the app provides, so decided to make a simple script to emulate the same, but as imgur is one my favs, used the same.

Instant upload images to imgur is a simple script that does :

  • Invoke camera to take a picture.
  • Upload the picture to imgur.
  • SMS the link to the img.
  • Copies the link to the cilpboard.
  • Open the img url in the default browser.

P.S : Additionally sending email, creating QR code and much more can be done with the img url.

The code below works fine, once you assign a number the SMS_TO variable and suggest to use your own API key from imgur.

""" Instanly upload to IMGUR, SMS, copies to clip and opens the img URL """
import android
from json import loads as json_decode
from urllib2 import Request, urlopen
from urllib import urlencode
import base64

__author__ = 'Hemanth H.M <[email protected]>'
__copyright__ = 'Copyright (c) 2010, h3manth.com.'
__license__ = 'GNU GPLv3'

""" The API_URL, API_KEY and IMG_APTH. """
URL = 'http://api.imgur.com/2/upload.json'
""" Please change the key to your key """
API_KEY = '6999d11def1e31418e8eaea7cae66761'
IMG_PATH = '/sdcard/imgur.jpg'

""" Number to which the link must be texted """
SMS_TO="YOUR_MOBILE_NUM"

droid = android.Android()
""" Invoke camrea to capture picture and save it to IMG_PATH """
droid.cameraInteractiveCapturePicture(IMG_PATH)

try:
    with file(IMG_PATH,'rb') as pic:
        droid.dialogCreateSpinnerProgress("Uploading","Please wait...")
        droid.dialogShow()
        image = pic.read()
        post = {
            'key': API_KEY,
            'image': base64.b64encode(image),
            'path': IMG_PATH
        }

        data = urlencode(post)
   
        try:
            img_url = urlopen(Request(URL, data))
   
        except Exception, e:
            droid.dialogDismiss()
            droid.dialogCreateAlert("Uploading..","FAILED!");
            exit;

        droid.dialogDismiss()
        response = json_decode(img_url.read())

        link = response['upload']['links']['original']
        droid.smsSend(SMS_TO,link);
        droid.setClipboard(link)
        droid.dialogCreateAlert("Uploaded,Sms'd, copied to clipboard, now view the link!");
        droid.view(link)
except IOError:
    droid.dialogCreateAlert("Sorry could not find the image!");

If this was fun, have a look at Sending Random jokes from android

Share this