Python ImageShack uploader script

python-powered-h-140x182Do you often take screenshots which you want to share with others on forums, IRC, MSN or such? Wouldn’t it be nice if you could just run a script and the file would be uploaded and ready for copy/paste? Well a nice guy over at the ArchLinux.org forum called “solarwind” wrote a script in python which uploads a image from command line, and outputs the link and such when its done. Ive used it a couple of times and I have to say: Its awesome.

Heres the code, place this in a file called imgupload.py (or something similar):

#!/usr/bin/python
 
import sys, os
 
#Define globals.
tmp = "temp.data" #Temporary file filename.
img = "" #Image filename.
ext = "" #File extension.
 
#Define functions.
def uploadsingle():
    print("Uploading: " + img)
    os.system("curl -H Expect: -F fileupload=\"@" + img + "\" -F xml=yes -# \"http://www.imageshack.us/index.php\" > " + tmp)
 
    file = open(tmp, "r")
    content = file.read()
 
    #Get the image link.
    start = content.find("<image_link>")
    end = content.find("</image_link>")
    link = content[start + 12 : end]
 
    #Get the thumbnail link.
    start = content.find("<thumb_link>")
    end = content.find("</thumb_link>")
    forum_link = content[start + 12 : end]
 
    print("Image link:\t\t" + link)
    print("Link for forums:\t[url= + link + ][img]" + forum_link + "[/img][/url]")
 
def uploadall(extension):
    print("Uploading: *." + extension)
    outfile = open("links.txt", "a")
    list = os.listdir("./")
    for fname in list:
        if fname.endswith(ext):
            print("Uploading: " + fname)
            os.system("curl -H Expect: -F fileupload=\"@" + fname + "\" -F xml=yes -# \"http://www.imageshack.us/index.php\" > " + tmp)
            file = open(tmp, "r")
            content = file.read()
            #Get the image link.
            start = content.find("<image_link>")
            end = content.find("</image_link>")
            link = content[start + 12 : end]
            #Get the thumbnail link.
            start = content.find("<thumb_link>")
            end = content.find("</thumb_link>")
            forum_link = content[start + 12 : end]
            #Append link data to the outfile.
            outfile.write("File: " + fname + "\n")
            outfile.write("Image link:\t\t" + link + "\n")
            outfile.write("Link for forums:\t[url= + link + ][img]" + forum_link + "[/img][/url]\n\n")
            os.system("rm " + tmp) #Remove temporary file.
 
#Process command line arguments and execute program.
if sys.argv[1] == "all":
    ext = sys.argv[2]
    uploadall(ext)
else:
    img = sys.argv[1]
    uploadsingle()
 
os.system("rm " + tmp) #Remove temporary file.

Then if you want to run the file, just go to the folder where you stored the file and open up a command line and type the following which should give the result (example run):

$ python imgupload.py compiz-fusion.png
Uploading: compiz-fusion.png
Image link:		http://img246.imageshack.us/img246/7740/compizfusiond.png
Link for forums:	[URL=http://img246.imageshack.us/img246/7740/compizfusiond.png]
                        [IMG]http://img246.imageshack.us/img246/7740/compizfusiond.th.png[/IMG][/URL]

TIPS:
If you’re a GNU/Linux user you can move this script into the bin folder for easy access:

$ mv imgupload.py imgup
$ chmod +x imgup
$ mv imgup /usr/bin/
 
# Then you can simply type:
$ imgup yourimage

Also solarwind came with nice tip,

solarwind says:
Nice. Note that you can also upload all of a certain type of images in a folder. Let’s say you have a bunch of png files. To upload them all, simply type:

imgupload.py all png

All png files in the folder will be uploaded.:

About Torstein Skulbru

23 year old, Bachelor ICT(Information and Communication Technology) student at University of Bergen (NO)