hastebinit - quickly paste snippets into hastebin.com

11 October 2012   7 comments   Python, Linux

Powered by Fusion×

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!

Comments

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

Thanks for that.
Kim
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
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
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
Sadly it's not Py3k compatible, so I made my own.
chris
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.
Thank you for posting a comment

Your email will never ever be published


Related posts

Previous:
How I stopped worrying about IO blocking Tornado 18 September 2012
Next:
Fastest way to thousands-commafy large numbers in Python/PyPy 13 October 2012
Related by keywords:
Highlighted code syntax in Keynote 30 August 2014
String comparison function in Python (alpha) 22 December 2007
TornadoGists.org - launched and ready! 06 April 2011