Skip to content

Instantly share code, notes, and snippets.

@ntang-livestream
Created August 4, 2012 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ntang-livestream/3255072 to your computer and use it in GitHub Desktop.
Save ntang-livestream/3255072 to your computer and use it in GitHub Desktop.
FAP: Fairly Automated Paster
#!/usr/bin/env python
#
# Fairly Automated Paster
#
# Automatically creates gists ('gisting') by pasting text.
# FAP lets you gist really quickly and easily, all over the place.
# This first version only allows you to create anonymous gists.
#
# Nicholas Tang (nicholastang@gmail.com)
import requests
import re
import json
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file",
help="the name of a text file to use as the body of the gist")
parser.add_argument("-d", "--description",
help="description of the gist")
parser.add_argument("-p", "--private",
help="make the gist private", action="store_true")
args = parser.parse_args()
uri = 'https://api.github.com/gists'
timeout_secs = 10.000
headers = {'content-type': 'application/json'}
if args.file:
try:
file = open(args.file, 'r')
except Exception, e:
print "Error reading from file %s: %s" % (args.file, e)
gist_body = file.read()
file.close()
gist_filename = args.file
else:
gist_body = ''
print "Input text, with '<<<EOF' to end input:"
while True:
line = raw_input()
if re.search('<<<EOF', line):
break
else:
gist_body += line + '\n'
gist_filename = "file.txt"
if args.description:
gist_description = args.description
else:
gist_description = ""
if args.private:
gist_public = False
else:
gist_public = True
gist_post_data = {'description': gist_description, 'public': gist_public,
'files': {gist_filename: {'content': gist_body}}}
json_post_data = json.dumps(gist_post_data)
try:
r = requests.post(uri, data=json_post_data, headers=headers)
except Exception, e:
print "Error posting to github: %s" % (e)
exit()
github_response = json.loads(r.text)
if r.headers['Status'] == '201 Created':
print "Gisted successfully!"
id = github_response['id']
html_url = github_response['html_url']
print "Gist %s landed here: %s" % (str(id), str(html_url))
else:
print "Couldn't gist, status code: %s" % (r.headers['Status'])
message = github_response['message']
print "Error message: %s" % (str(message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment