VLAAH 파이썬 API

vlaah-python 라이브러리는 VLAAH의 웹 API를 파이썬으로 감싼 것입니다.

사용법

먼저 VLAAH API 페이지에서 새 애플리케이션 키를 받으시길 바랍니다. 이 애플리케이션 키는 16진수 64자리로 예를 들면 다음과 같습니다.1

3e102b5df7b5731f68a61a34c3d58185fe7e1585ef3bf5e99c0e42d92c34cf44

해당 애플리케이션 키를 사용해서 세션 객체를 생성합니다. 세션 객체에 User-Agent 문자열을 넣을 수도 있습니다.

>>> from vlaah import Session
>>> sess = Session('3e102b5df7b5731f68a61a34c3d58185fe7e1585'
           'ef3bf5e99c0e42d92c34cf44', 'mearieflo/20081116')
>>> sess
<VLAAH Session: agent='mearieflo/20081114'
 appkey='3e102b5df7b5731f68a61a34c3d58185fe7e1585ef3bf5e99c0e42d92c34cf44'
 base='http://vlaah.com/'>

세션 객체의 find 메소드로 각 토픽에 해당하는 객체를 얻을 수 있습니다.

>>> sess.find(u'~dahlia')
<VLAAH Person: name='~dahlia' nick=u'\ud64d\ubbfc\ud76c'>
>>> sess.find(u'?35337')
<VLAAH Comment: name='?35337' topic=u'\uc720\ud589\uc5b4'
 type='comment-minus' author='~shinvee'>
>>> sess.find(u'달이 차오른다 가자')
<VLAAH Topic: name=u'\ub2ec\uc774 \ucc28\uc624\ub978\ub2e4 \uac00\uc790'>

각 객체 별로 여러 속성과 메소드가 있습니다. 예를 들어 사람(Person) 객체에는 개인 이미지 주소를 가리키는 picture_url 속성이 있습니다.

>>> import urllib
>>> urllib.urlretrieve(sess.find(u'~crocodile').picture_url)
('/var/folders/X7/X7Zjqfg5FquAv0k9T-M42U+++TI/-Tmp-/tmp5Z9qAv.png',
 <httplib.HTTPMessage instance at 0xc6d4b8>)

VLAAH의 모든 주제에는 댓글이 붙을 수 있습니다. 댓글의 목록은 comments 메소드로 얻어 올 수 있습니다. 이 메소드의 결과는 리스트처럼 보이지만 실제로는 필요한 만큼 조금 조금씩 읽어 옵니다.

>>> sess.find(u'~dahlia').comments()
[<VLAAH Comment: name='?47454' topic='~dahlia' type='comment-minus'
 author='~crocodile'>, <VLAAH Comment: name='?46843' topic='~dahlia'
 type='comment-minus' author='~6601'>, ... (103 more)]

마찬가지로 객체의 속성으로 딸려 있는 여러 객체(이를테면 Commentauthor 속성)들은 필요할 때만 새로 읽어 들입니다. 이런 객체들은 LazyPerson과 같은 클래스로 처리되지만 사용자가 보기에는 투명하게 처리됩니다.

>>> sess.find(u'?45678').author
<VLAAH Person: name='~heungsub' nick=u'\uc774\ud765\uc12d'>
>>> type(_)
<class 'vlaah.person.LazyPerson'>

이런 특징만 빼면 라이브러리 API는 매우 직관적이기 때문에 원하는 코드를 빠르게 작성할 수 있습니다. 예를 들어 다음 코드는 특정 사용자 페이지에 "안녕하세요"라고 댓글을 단 모든 여자들(^^;)의 생일을 보여 줍니다.

>>> [cmt.author.birthday for cmt in sess.find(u'~dahlia').comments()
...  if cmt.author.gender == 'female' and
...     (cmt.body and u'안녕하세요' in cmt.body.as_text())]
[datetime.date(1990, 11, 5), datetime.date(1990, 2, 11),
 datetime.date(1989, 6, 1), datetime.date(1990, 3, 12)]

사용하는 프로젝트

vlaah-python 라이브러리를 사용해서 뭔가 그럴듯한 걸 만드셨다면 연락해 주세요. 이곳에 싣도록 하겠습니다.

내려 받기

vlaah-python 라이브러리의 최신 버전은 0.9.0으로 여기에서 받을 수 있습니다.

개발 버전은 메아리 머큐리얼 저장소에서 받을 수 있으며, 다음 명령을 사용하면 됩니다.

$ hg clone http://hg.mearie.org/vlaah-python/

vlaah-python은 MIT 라이선스 하에 배포되며 저작권 명시만 하면 마음대로 쓸 수 있습니다.


  1. 이 키는 예시를 들기 위해 아무렇게나 만든 것입니다. 좀 더 정확하게는, this is an example of VLAAH application key.의 SHA-256 해시입니다.


(rev 1d46270eb038)