05 스프링노트(Springnote) API

원문 : http://nakada.springnote.com/pages/400945

 

들어가기 전에

 

0.시작하면서...

 파이썬(Python)에서 스프링노트에 접근하는 내용을 스크랩했다.

 

1.스크랩 내용

  • 참고한 소스 
    • 하늘모자님이 작성하신 python에서 스노피 소스 읽고 쓰기 http://doc.springnote.com/pages/313680 를 참고하여 8월 1일부터 바뀐 API 인증에 맞게 하였습니다.
    • ias 님이 루비로 작성하신 스노피 소스 읽고 쓰기 http://dev.springnote.com/pages/3364 를 참고하여 새로 바뀐 인증방식에 적용 하였습니다.
  • 변화된 사항 
    • 하늘모자님의 소스에서 바뀐 부분은 HTTPS 을 이용한 인증부분과 쉘상에서 입력받을때 \n 을 무조건 없애던것을 수정하였으며, 탭을 공백 4개로 표시하게 하였습니다.
    • ias 님의 소스에서와 같이 파일을 read 하여 data 를 작성하는것은 src=sys.stdin.read() 부분만 src = open('파일이름').read() 로 해주시면 됩니다.
  • 특이사항 
    • from xml.etree.ElementTree import XML 부분의 경우 python 2.5 이상에서만 됩니다. 굳이 필요하지 않으므로 해당 부분을 제거하고 소스에서  print XML(r).findtext('source')  부분만 그냥 print r 로 해도 되며 다른 xml 모듈을 사용해서 가공하시면 됩니다.
    • multipart/form-data 를 통한 파일 첨부는 제 능력 부족으로 구현하지 못하고 있습니다. 정보 있으시면 연락주세요
    • 아직 잘 작성하지 못해서 좋은 정보나 관련된 정보 주시면 감사하게 받겠습니다. 연락은 제 블로그나 메일로 주세요
  • 연락처 

 

  1. #!/usr/bin/python
  2. import sys
  3. from ConfigParser import SafeConfigParser
  4. import base64
  5. import urllib
  6. import httplib
  7. import cgi
  8. from xml.etree.ElementTree import XML
  9. class SpringNote:
  10.     def __init__(self):
  11.         self.config = SafeConfigParser()
  12.         self.config.read('spring.conf')
  13.         open_id = self.config.get('springnote','open_id')
  14.         user_key = self.config.get('springnote','user_key')
  15.         app_key = self.config.get('springnote','app_key')
  16.         username = urllib.quote(open_id)
  17.         passwd = urllib.quote('%s.%s' % (user_key, app_key))
  18.         self.basic_auth = 'Basic %s' % base64.b64encode('%s:%s' % (username, passwd))
  19.     def httpReq(self, method, uri, data = None):
  20.         HOSTNAME = 'api.springnote.com'
  21.         conn = httplib.HTTPSConnection(HOSTNAME)
  22.         conn.putrequest(method, uri)
  23.         conn.putheader('Authorization', self.basic_auth)
  24.         if data :
  25.             conn.putheader('Content-Type','application/xml')
  26.             conn.putheader('Content-Length', len(data))
  27.         conn.endheaders()
  28.         if data :
  29.             conn.send(data)
  30.         response = conn.getresponse()
  31.         r = response.read()
  32.         conn.close()
  33.         print XML(r).findtext('source')
  34.     def getPage(self, page_id):
  35.         self.httpReq('GET','/pages/%s.xml' % page_id)
  36.     def putPage(self, page_id):
  37.         print 'input page content:'
  38.         src = sys.stdin.read()
  39.         src_html = '<p>%s</p>' % cgi.escape(src).replace('\r', '').replace('\t','&nbsp;'*4).replace('\n', '</p>\n<p>').replace('<p></p>','<p>&nbsp;</p>')
  40.         data = "<page><source>%s</source></page>" % cgi.escape(src_html)
  41.         self.httpReq('PUT', '/pages/%s.xml' % page_id, data)
  42.     def main(self, cmd, arg):
  43.         if cmd == 'get':
  44.             self.getPage(arg)
  45.         elif cmd == 'put':
  46.             self.putPage(arg)
  47. if __name__ == "__main__":
  48.     SpringNote().main(sys.argv[1], sys.argv[2])

 

이 글은 스프링노트에서 작성되었습니다.

+ Recent posts