forked from clickyotomy/twitter-message-bus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gist.py
executable file
·112 lines (92 loc) · 3.5 KB
/
gist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#! /usr/bin/env python2.7
'''
Post, fetch or delete a gist (containing the keybase-saltpack) on GitHub.
'''
import os
import json
import hashlib
from socket import getfqdn
from getpass import getuser
from datetime import datetime
import requests
# API URL, headers.
GITHUB_API_URL = 'https://api.github.com'
GITHUB_HEADERS = {
'Accept': 'application/vnd.github.v3.raw+json'
}
def http_debug(response):
'''
Print the HTTP request/response debug log.
'''
print 'http-request\n{0}\n'.format('-' * len('http-request'))
print 'url ({0}): {1}'.format(response.request.method,
response.request.url)
print 'request-headers:'
print json.dumps(dict(response.request.headers), indent=4)
if response.request.method != 'GET':
if response.request.body is not None:
print 'request-payload:'
print json.dumps(json.loads(response.request.body), indent=4)
print '\nhttp-response\n{0}\n'.format('-' * len('http-response'))
print 'status-code: {0} {1}'.format(response.status_code, response.reason)
print 'url: {0}'.format(response.url)
print 'time-elapsed: {0}s'.format(response.elapsed.total_seconds())
print 'response-headers:'
print json.dumps(dict(response.headers), indent=4)
print 'response-content:'
print None if response.content is '' else json.dumps(response.json(),
indent=4)
def github(http, uri, token, payload, debug=False):
'''
Make an HTTP request to the GitHub API.
'''
if uri is not None:
url = '/'.join([GITHUB_API_URL, uri.lstrip('/')])
if token is not None:
GITHUB_HEADERS.update({'Authorization': ' '.join(['token', token])})
request = getattr(requests, http)
try:
response = request(url, data=payload, headers=GITHUB_HEADERS)
if debug:
http_debug(response)
return response.json()
except (requests.exceptions.RequestException, ValueError):
return {}
def post(content, token=None, username=None, public=False, debug=False):
'''
Post a gist on GitHub.
'''
random = hashlib.sha1(os.urandom(16)).hexdigest()
username = getuser() if username is None else username
now = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
description = ('{hash} (twitter-message-bus); from {host} by {user} '
'at {time} UTC.').format(host=getfqdn(), user=username,
time=now, hash=random)
payload = json.dumps({
'files': {
'message': {
'content': content if content is not None else ''
}
},
'public': public,
'description': description
})
response = github(http='post', uri='gists', token=token, payload=payload,
debug=debug)
return (response['id'], random) if 'id' in response else (None, None)
def get(gist_id, token=None, debug=False):
'''
Get the contents of the gist from GitHub.
'''
response = github(http='get', uri='gists/{0}'.format(gist_id), token=token,
payload=None, debug=debug)
if 'files' in response:
return response['files']['message']['content']
return None
def delete(gist_id, token=None, debug=False):
'''
Delete a gist from GitHub.
'''
response = github(http='delete', uri='gists/{0}'.format(gist_id),
token=token, payload=None, debug=debug)
return True if response == {} else False