스프링노트 Python 예제
스프링노트 Python 예제
스노피 소스 읽고 쓰기의 내용과 같은 페이지의 루비스크립트 소스 샘플을 참고했습니다.
사용법
페이지의 내용을 가져올 때:
- python test_springnote.py get yyy
(yyy는 페이지번호입니다)
페이지의 내용을 올릴 때:
- python test_springnote.py put yyy
(yyy는 페이지번호입니다)
내용을 입력후에 Ctrl-Z (Linux의 경우에는 Ctrl-D)
springnote.conf
설정파일입니다.
- [springnote]
- open_id: open ID
- user_key: user key
- app_id: application ID
- app_key: application key
test_springnote.py
-
import sys
-
import random
-
import datetime
-
from ConfigParser import SafeConfigParser
-
import sha
-
import base64
-
import urllib
-
import httplib
-
import cgi
-
from xml.etree.ElementTree import XML
-
class TestSpringNote:
-
def __init__(self):
-
self.config = SafeConfigParser()
-
self.config.read("springnote.conf")
-
open_id = self.config.get('springnote', 'open_id')
-
user_key = self.config.get('springnote', 'user_key')
-
app_id = self.config.get('springnote', 'app_id')
-
app_key = self.config.get('springnote', 'app_key')
-
nonce = "%016x" % random.randint(0, 16 ** 16 - 1)
-
created = datetime.datetime.utcnow().isoformat()[:19] + 'Z'
-
digested_key = sha.new(nonce + created + user_key + app_key).hexdigest()
-
username = urllib.quote(open_id)
-
password = urllib.quote('%s,%s,%s,%s' % (app_id, nonce, created, digested_key))
-
self.basic_auth = 'Basic %s' % base64.b64encode('%s:%s' % (username, password))
-
def httpReq(self, method, uri, data = None):
-
con = httplib.HTTPConnection('api.springnote.com:80')
-
con.putrequest(method, uri)
-
con.putheader('Authorization', self.basic_auth)
-
if data: con.putheader('Content-Length', len(data))
-
con.endheaders()
-
if data: con.send(data)
-
res = con.getresponse()
-
r = res.read()
-
con.close()
-
print XML(r).findtext('source')
-
-
def getPage(self, page_id):
-
self.httpReq('GET', '/pages/%s.xml' % page_id)
-
def putPage(self, page_id):
-
print 'input page content:'
-
src = sys.stdin.read()
-
src_html ='<p>%s</p>' % cgi.escape(src).replace('\r', '').replace('\n', '</p>\n<p>')
-
data = '<page><source>%s</source></page>' % cgi.escape(src_html)
-
self.httpReq('PUT', '/pages/%s.xml' % page_id, data)
-
def main(self, cmd, arg):
-
if cmd == 'get':
-
self.getPage(arg)
-
elif cmd == 'put':
-
self.putPage(arg)
-
if __name__ == "__main__":
-
TestSpringNote().main(sys.argv[1], sys.argv[2])
Comments (0)