|
a/scripts/wiki-post.py |
|
b/scripts/wiki-post.py |
|
... |
|
... |
65 |
digest = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()
|
65 |
digest = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()
|
66 |
params.append(('api_signature', digest))
|
66 |
params.append(('api_signature', digest))
|
67 |
return params
|
67 |
return params
|
68 |
|
68 |
|
69 |
def main():
|
69 |
def main():
|
70 |
usage = 'usage: %prog [options] PageName [file]'
|
70 |
usage = 'usage: %prog [options] [PageName [file]]'
|
71 |
op = OptionParser(usage=usage)
|
71 |
op = OptionParser(usage=usage)
|
72 |
op.add_option('-c', '--config', metavar='CONFIG')
|
72 |
op.add_option('-c', '--config', metavar='CONFIG')
|
73 |
op.add_option('-a', '--api-key', metavar='KEY')
|
73 |
op.add_option('-a', '--api-key', metavar='KEY')
|
74 |
op.add_option('-s', '--secret-key', metavar='KEY')
|
74 |
op.add_option('-s', '--secret-key', metavar='KEY')
|
75 |
op.add_option('-u', '--url', metavar='URL')
|
75 |
op.add_option('-u', '--url', metavar='URL')
|
76 |
(options, args) = op.parse_args()
|
76 |
(options, args) = op.parse_args()
|
77 |
|
77 |
|
|
|
78 |
pagename = None
|
|
|
79 |
markdown = None
|
|
|
80 |
method = 'GET'
|
|
|
81 |
|
|
|
82 |
pagename_given = len(args) >= 1
|
|
|
83 |
if pagename_given:
|
78 |
page = args[0]
|
84 |
pagename = args[0]
|
79 |
f = open(args[1], 'r') if len(args)>1 else stdin
|
85 |
|
|
|
86 |
filename_given = len(args) > 1
|
|
|
87 |
if filename_given:
|
|
|
88 |
method = 'PUT'
|
|
|
89 |
f = open(args[1], 'r')
|
80 |
markdown = f.read()
|
90 |
markdown = f.read()
|
81 |
|
91 |
|
82 |
config = ConfigParser()
|
92 |
config = ConfigParser()
|
83 |
config.read([str(os.path.expanduser('~/.forge-api.ini')), str(options.config)])
|
93 |
config.read([str(os.path.expanduser('~/.forge-api.ini')), str(options.config)])
|
84 |
|
94 |
|
85 |
api_key = options.api_key or config.get('keys', 'api-key')
|
95 |
api_key = options.api_key or config.get('keys', 'api-key')
|
86 |
secret_key = options.secret_key or config.get('keys', 'secret-key')
|
96 |
secret_key = options.secret_key or config.get('keys', 'secret-key')
|
87 |
# print an error message if no keys are found
|
97 |
# print an error message if no keys are found
|
88 |
|
98 |
|
89 |
url = urljoin(options.url or config.get('wiki', 'url'), page)
|
99 |
url = options.url or config.get('wiki', 'url')
|
|
|
100 |
if pagename_given:
|
|
|
101 |
url = urljoin(url, pagename)
|
90 |
|
102 |
|
91 |
sign = Signer(secret_key, api_key)
|
103 |
sign = Signer(secret_key, api_key)
|
|
|
104 |
params = [('text', markdown)] if method=='PUT' else []
|
92 |
params = sign(urlparse(url).path, [('text', markdown)])
|
105 |
params = sign(urlparse(url).path, params)
|
93 |
try:
|
106 |
try:
|
|
|
107 |
if method=='PUT':
|
94 |
result = urlopen(url, urlencode(params))
|
108 |
result = urlopen(url, urlencode(params))
|
|
|
109 |
else:
|
|
|
110 |
result = urlopen(url+'?'+urlencode(params))
|
95 |
stdout.write(result.read())
|
111 |
stdout.write(result.read())
|
96 |
except HTTPError, e:
|
112 |
except HTTPError, e:
|
97 |
stdout.write(e.read())
|
113 |
stdout.write(e.read())
|
98 |
|
114 |
|
99 |
if __name__ == '__main__':
|
115 |
if __name__ == '__main__':
|