Skip to content

Instantly share code, notes, and snippets.

@cdunklau
Created May 28, 2014 11:50
Show Gist options
  • Save cdunklau/cf87313173622c84bd81 to your computer and use it in GitHub Desktop.
Save cdunklau/cf87313173622c84bd81 to your computer and use it in GitHub Desktop.
Pastebin a file to bpaste.net
#!/usr/bin/env python
import argparse
import requests
SPACEPASTE_URL = 'http://bpaste.net/'
parser = argparse.ArgumentParser()
parser.add_argument(
'file',
metavar='FILE',
help="The file to upload",
)
parser.add_argument(
'-l', '--lang',
metavar='LANGUAGE',
default=None,
help="The language to use for highlighting",
)
ext_to_lang = {
'py': 'python',
'txt': 'text',
}
def main():
args = parser.parse_args()
fname = args.file
with open(fname) as fp:
text = fp.read()
if args.lang is None:
lang = ext_to_lang.get(fname.rsplit('.', 1)[-1], 'text')
else:
lang = ext_to_lang.get(args.lang, 'text')
data = {
'code': text,
'language': lang,
'webpage': '',
'private': 'on',
}
resp = requests.post(SPACEPASTE_URL, data=data)
resp.raise_for_status()
print resp.url
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment