Monday, October 22, 2012

Pastebin Console Script

So, another one cool and simple script for today. I wrote very minimalistic script to post your files to pastebin right from console. I didn't want it to grow large so it can not identify file type. I hope it won't be hard for you to implement this feature. Also there are many options you can pass to script. You can pass only file you want to upload and mode: anonymous(actually it 'guest', but you can also implement anon mode, I'll show how to do it) or not.

Script gets paste name from file name you pass. Also it gets the extension. It will help you to implement syntax detection. All available features you can see right here: API page
Let's start. First of all you should register at Paste Bin. When you are done you'll see your unique developer API key on the API page. This key is your authority credential to Pastebin service. After this is done let's look at some other parameters.

pastebin_vars = {'api_dev_key': dev_key,
    'api_option': 'paste', 'api_paste_code': data,
    'api_user_key': user_key,
    'api_paste_name': paste_name}

These are parameters to send to the Pastebin.
api_dev_key - this is your unique developer key.
api_option - we will send data to the server, so this option tells server to paste our data
api_paste_data - here is our paste. It will be read from file. I'll show it a bit later
api_user_key - this parameter tells server to paste it as your text or as guest's
api_paste_name - this parameter is our paste name
There are also some more options. You can see it at API page. Now we are interested in paste_data, user_key and paste_name.
Data. Here is some code to read it from file:

f = open(sys.argv[1], 'r')
data =

Yup, that's all :) Two lines of code. You can make it more complicated and safe with try: or with.

User key. The simplest way to get it is here: Get User Key.

Paste name. We'll get it from file name. I chose regular expressions, my favorite technique - groups.
Here is some code:

def getName():
    name = sys.argv[1]
    m = re.match(r"(?P<name>[\w\S]+)\.(?P<ext>\w+)", name)
    name =
    return name

dev_key = 'your dev key'
user_key = 'your user key'
paste_name = getName()
if len(sys.argv) == 3:
    if sys.argv[2] == 'anon':
        user_key = ''

So you can see getName() function and assigning parameters. As you can see if you pass the anon parameter user_key will be passed as empty string, which means guest paste. And also you can catch sight of regular expression. It parses file name into name and extension, so in future you can easily add syntax detection. To make it work don't forget to import sys and re modules.
Last but not the least:

response = urllib.urlopen('',
url =

These two lines post your file to pastebin and url variable contains your unique paste's url.
That's all. Pretty easy though :)
P.S. Anon mode... api_paste_private allows you to implement 3 mode of visibility:
0 - Public
1 - Unlisted
2 - Private

Full Script you can see here gist:github
P.P.S. You can find there # print usage()
I haven't written this function 'cause it is too easy, some print about how to use script
python myfile.txt anon or python myfile.txt

No comments:

Post a Comment