hastebinit - quickly paste snippets into hastebin.com

11 October 2012   9 comments   Python, Linux

Mind That Age!

This blog post is 5 years old! Most likely, its content is outdated. Especially if it's technical.

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:


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]
        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

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!


This looks pretty sweet, though hastebin also seems to have a command-line utility:
Peter Bengtsson
Grrr! If I had only known :(

Thanks for that.
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.
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:

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...
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
Sadly it's not Py3k compatible, so I made my own.
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.
Aurea Eagle
I've been using https://www.pastefs.com for the same reason, It's new but I'm positive that you'll like it iA
Peter Bengtsson
I think hastebin is dead now. I personally always use GitHub Gists.
Thank you for posting a comment

Your email will never ever be published

Related posts

How I stopped worrying about IO blocking Tornado 18 September 2012
Fastest way to thousands-commafy large numbers in Python/PyPy 13 October 2012
Related by Keyword:
Highlighted code syntax in Keynote 30 August 2014
TornadoGists.org - launched and ready! 06 April 2011
String comparison function in Python (alpha) 22 December 2007
Related by Text:
jsonpprint - a Python script to format JSON data nicely 21 November 2010
Bash tip of the day: ff 25 March 2011
OpenID, Attribute Exchange, SReg, python-openid and Google 23 April 2010
Fastest cache backend possible for Django 07 April 2017
A neat trick to zip a git repo with a version number 01 September 2017