Linksnappy Command Line Downloader (Python)

Simple Python script to download files via Linksnappy.

#! /usr/bin/env python

import requests
import json
import sys

USERNAME = 'my username'
PASSWORD = 'my pass'

params = {'link': sys.argv[1],
'type': '',
'username': USERNAME,
'password': PASSWORD}

resp = requests.post('http://gen.linksnappy.com/genAPI.php',
data={'genLinks': json.dumps(params)})

url = json.loads(resp.text)['links'][0]['generated']

local_filename = url.split('/')[-1]

# http://stackoverflow.com/questions/16694907/how-to-download-large-file-in-python-with-requests-py
r = requests.get(url, stream=True)
    with open(local_filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
                f.flush()
print local_filename