I'm quite fond of hastebin.com. It's fast. It's reliable. And it's got nice keyboard shortcuts that work for my taste.

So, I created a little program to quickly throw things into hastebin. You can have one too:

First create ~/bin/hastebinit and paste in:

#!/usr/bin/python

import urllib2
import os
import json

URL = 'http://hastebin.com/documents'

def run(*args):
    if args:
        content = [open(x).read() for x in args]
        extensions = [os.path.splitext(x)[1] for x in args]
    else:
        content = [sys.stdin.read()]
        extensions = [None]

    for i, each in enumerate(content):
        req = urllib2.Request(URL, each)
        response = urllib2.urlopen(req)
        the_page = response.read()
        key = json.loads(the_page)['key']
        url = "http://hastebin.com/%s" % key
        if extensions[i]:
            url += extensions[i]
        print url

if __name__ == '__main__':
    import sys
    sys.exit(run(*sys.argv[1:]))

Then run: chmod +x ~/bin/hastebinit

Now you can do things like:

$ cat ~/myfile | hastebinit
$ hastebinit < ~/myfile
$ hastebinit ~/myfile myotherfile

Hopefully it'll one day help at least one more soul out there!

Cesar - 11 October 2012 [«« Reply to this]
This looks pretty sweet, though hastebin also seems to have a command-line utility:
https://github.com/seejohnrun/haste-client
Peter Bengtsson - 11 October 2012 [«« Reply to this]
Grrr! If I had only known :(

Thanks for that.
Kim - 11 October 2012 [«« Reply to this]
That's one beutiful site, I'm surprised I haven't heard of it before.

Thanks for sharing your script I'll definitely try to find some use for this.
Anonymous - 11 October 2012 [«« Reply to this]
Just a little cent.

If the system can not found the script in ~/bin/ , the script invocation should be like:

$ cat ~/myfile | ~/bin/hastebinit
$ ~/bin/hastebinit < ~/myfile
$ ~/bin/hastebinit ~/myfile myotherfile

This is because the executable files under ~/bin/ are not always included in the path environment variable. Some distributions like Debian and derivatives do it this way, if you create the bin folder, but for other distributions you may have to add it to the path manually.

Or you can add ~/bin/ to your path editing .bashrc or other terminal initialization script, and using the next configuration lines:

PATH=~/bin:"${PATH}"
export PATH

I have actually modified the configuration to use .bin (with the dot), because I do not like the bin folder showing in my beautifully organized home folder...
Matt - 11 October 2012 [«« Reply to this]
Thanks. That is a nice and simple version in python. Great for those who don't want to install Ruby for https://github.com/seejohnrun/haste-client
Simonas - 11 October 2012 [«« Reply to this]
Sadly it's not Py3k compatible, so I made my own.
chris - 11 October 2012 [«« Reply to this]
Thanks for this. I discovered hastebin and found a script that I could use with it. I won't install Ruby on our Unix server (even if I could get it compiled - doubtful) so the Python version is a welcome alternative.


Your email will never ever be published