Browse files

gist upload

  • Loading branch information...
1 parent 29a7596 commit e6e62e24c2db6dd3ff07c39e3f369b285552a76a @kennethreitz committed Nov 26, 2011
Showing with 27 additions and 14 deletions.
  1. +27 −14 dirsnap/gist.py
View
41 dirsnap/gist.py
@@ -1,24 +1,37 @@
# -*- coding: utf-8 -*-
-import json
+"""
+dirsnap.gist
+~~~~~~~~~~~~
+
+Uploads the given file to Gist, anonomously.
+"""
+
+import os
import requests
+GIST_URL = 'https://gist.github.com/gists'
+GIST_TEMPLATE = 'https://gist.github.com/raw/{0}/{1}'
+
-url = 'https://api.github.com/gists'
+def upload(path):
+ """Uploads the file at the given path to gist.github.com."""
+ filename = path.split(os.path.sep)[-1]
-payload = {
- 'description': 'dirsnap catpured TIMESTAMP',
- 'public': False,
- 'files': {
- 'filename': {
- 'content': 'blah'
- }
+ with open(path, 'rb') as f:
+ content = f.read()
+
+ payload = {
+ 'file_ext[gistfile#0]': '.txt',
+ 'file_name[gistfile#0]': filename,
+ 'file_contents[gistfile#0]': content
}
-}
-payload = json.dumps(payload)
-r = requests.post(url, data=payload)
-print r
-print r.headers
+ # Send it up to GitHub.
+ r = requests.post(GIST_URL, data=payload, allow_redirects=True)
+
+ gist_id = r.url.split(os.path.sep)[-1]
+
+ return GIST_TEMPLATE.format(gist_id, filename)

0 comments on commit e6e62e2

Please sign in to comment.