|
a/scripts/wiki-post.py |
|
b/scripts/wiki-post.py |
|
... |
|
... |
5 |
from datetime import datetime
|
5 |
from datetime import datetime
|
6 |
import os
|
6 |
import os
|
7 |
import urllib
|
7 |
import urllib
|
8 |
from urllib2 import urlopen, HTTPError
|
8 |
from urllib2 import urlopen, HTTPError
|
9 |
from urlparse import urlparse, urljoin
|
9 |
from urlparse import urlparse, urljoin
|
|
|
10 |
import urllib
|
10 |
from optparse import OptionParser
|
11 |
from optparse import OptionParser
|
11 |
from ConfigParser import ConfigParser
|
12 |
from ConfigParser import ConfigParser
|
12 |
|
13 |
|
13 |
def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
|
14 |
def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
|
14 |
"""
|
15 |
"""
|
|
... |
|
... |
57 |
def __init__(self, secret_key, api_key):
|
58 |
def __init__(self, secret_key, api_key):
|
58 |
self.secret_key = secret_key
|
59 |
self.secret_key = secret_key
|
59 |
self.api_key = api_key
|
60 |
self.api_key = api_key
|
60 |
|
61 |
|
61 |
def __call__(self, path, params):
|
62 |
def __call__(self, path, params):
|
|
|
63 |
if self.api_key is None:
|
|
|
64 |
return params
|
62 |
params.append(('api_key', self.api_key))
|
65 |
params.append(('api_key', self.api_key))
|
63 |
params.append(('api_timestamp', datetime.utcnow().isoformat()))
|
66 |
params.append(('api_timestamp', datetime.utcnow().isoformat()))
|
64 |
message = path + '?' + urlencode(sorted(params))
|
67 |
message = path + '?' + urlencode(sorted(params))
|
65 |
digest = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()
|
68 |
digest = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()
|
66 |
params.append(('api_signature', digest))
|
69 |
params.append(('api_signature', digest))
|
|
... |
|
... |
70 |
usage = 'usage: %prog [options] [PageName [file]]'
|
73 |
usage = 'usage: %prog [options] [PageName [file]]'
|
71 |
op = OptionParser(usage=usage)
|
74 |
op = OptionParser(usage=usage)
|
72 |
op.add_option('-c', '--config', metavar='CONFIG')
|
75 |
op.add_option('-c', '--config', metavar='CONFIG')
|
73 |
op.add_option('-a', '--api-key', metavar='KEY')
|
76 |
op.add_option('-a', '--api-key', metavar='KEY')
|
74 |
op.add_option('-s', '--secret-key', metavar='KEY')
|
77 |
op.add_option('-s', '--secret-key', metavar='KEY')
|
|
|
78 |
op.add_option('', '--anon', action='store_true')
|
75 |
op.add_option('-u', '--url', metavar='URL')
|
79 |
op.add_option('-u', '--url', metavar='URL')
|
76 |
(options, args) = op.parse_args()
|
80 |
(options, args) = op.parse_args()
|
77 |
|
81 |
|
78 |
pagename = None
|
82 |
pagename = None
|
79 |
markdown = None
|
83 |
markdown = None
|
|
... |
|
... |
90 |
markdown = f.read()
|
94 |
markdown = f.read()
|
91 |
|
95 |
|
92 |
config = ConfigParser()
|
96 |
config = ConfigParser()
|
93 |
config.read([str(os.path.expanduser('~/.forge-api.ini')), str(options.config)])
|
97 |
config.read([str(os.path.expanduser('~/.forge-api.ini')), str(options.config)])
|
94 |
|
98 |
|
|
|
99 |
api_key = None
|
|
|
100 |
secret_key = None
|
|
|
101 |
if not options.anon:
|
95 |
api_key = options.api_key or config.get('keys', 'api-key')
|
102 |
api_key = options.api_key or config.get('keys', 'api-key')
|
96 |
secret_key = options.secret_key or config.get('keys', 'secret-key')
|
103 |
secret_key = options.secret_key or config.get('keys', 'secret-key')
|
97 |
# print an error message if no keys are found
|
|
|
98 |
|
104 |
|
99 |
url = options.url or config.get('wiki', 'url')
|
105 |
url = options.url or config.get('wiki', 'url')
|
100 |
if pagename_given:
|
106 |
if pagename_given:
|
101 |
url = urljoin(url, pagename)
|
107 |
url = urljoin(url, urllib.quote(pagename))
|
|
|
108 |
print url
|
102 |
|
109 |
|
103 |
sign = Signer(secret_key, api_key)
|
110 |
sign = Signer(secret_key, api_key)
|
104 |
params = [('text', markdown)] if method=='PUT' else []
|
111 |
params = [('text', markdown)] if method=='PUT' else []
|
105 |
params = sign(urlparse(url).path, params)
|
112 |
params = sign(urlparse(url).path, params)
|
106 |
try:
|
113 |
try:
|