�빫�� ����� ���

Posts tagged ‘open API’

Google AJAX APIs Playground 아아.. 귀엽다.


구글에서 여러 멋진 Open API를 발표했었습니다. 웹에서 차트를 그리고, 표를 작성하고, 이벤트 핸들링을 하게 하고 등등.. 그것들은 “아.. 멋지구나.” 라는 생각을 들게 합니다. 하지만 Open API는 Web API니깐, Web application이 있어야 동작시킬 수 있습니다. 물론 native program으로도 가능하겠지만 web application보다 부수적인 작업이 많이 들어가지요. 해야할 것이 많다는 것은 귀찮은 게 많다는 것이고, 그럼 귀찮아서 안하게되죠.

 

그런데.. 이것이 두둥… AJAX APIs Playground!!

Google Open API를 바로 작성하고, 실행해 볼 수 있는 web application입니다. 정말 말 그대로 놀이터죠. 훌륭합니다. 정말. ㅜㅜ

이미 핑을 보냈습니다:

[python] RSS to egloos


블로그를 한 곳으로 옮기려다보니 노가다가 엄청 끔찍했다.

그래서 생각난 것이 Open API로 포스팅이 가능한가 한 것이다.

찾아보면 나온다. -_-+

Web 핸들링하기엔 python이 좋단다. 음.. 한번도 안해봤는데..

그래도 개발자인데! 언어가 어떠면 어떠랴~~

 

——————————————————————————–

# -*- coding: utf-8 -*-

import xmlrpclib

 

# egloos

egloos = xmlrpclib.Server(“https://rpc.egloos.com/rpc1″)

datastruct={‘category’: ‘미분류’, ‘description’: “posted by xmlrpc”, ‘title’:'xmlrpc test’}

no = egloos.metaWeblog.newPost(“”, “userid”, “userpassword”, datastruct, True)

r = egloos.metaWeblog.getPost(no, “userid”, “userpassword”)

 

print r

——————————————————————————–

 

요렇게 하면 포스팅을 함수로 넣을 수 있다.

 

그럼 함수로 만들고

 

——————————————————————————–

    def SendEgloosPost(self, title, content):

        egloos = xmlrpclib.Server(“https://rpc.egloos.com/rpc1″)

        datastruct = {‘category’: ‘미분류’, ‘description’: content, ‘title’:title}

        no = egloos.metaWeblog.newPost(“”, self.username, self.password, datastruct, True)

——————————————————————————–

 

blogger에 있는 RSS를 다운받아서 xml을 파싱해서 넣으면 되겠다 싶었다.

클래스로 만들면

 

——————————————————————————–

# -*- coding:utf-8 -*-

 

import xml.parsers.expat

import xmlrpclib

 

class BodyLoader:

    username = ‘icksishu’

    password = ‘46b96c31354f9′

 

    def Run(self):

        self.body=[]

        self.item={}

        self.value=”

 

        self.handle={}

        self.handle["feed"]=(self.__Body_Start, self.__Body_End)

        self.handle["entry"]=(self.__Item_Start, self.__Item_End)

        self.handle["title"]=(self.__Property_Start, self.__Property_End)

        self.handle["content"]=(self.__Property_Start, self.__Property_End)

 

        self.p=xml.parsers.expat.ParserCreate()

        self.p.StartElementHandler=self.__Start

        self.p.EndElementHandler=self.__End

        self.p.CharacterDataHandler=self.__Property_Value

        self.p.Parse(xmlData)

 

        self.SendPost()

 

        #print self.body

 

    def __Start(self, name, attr):

        self.handle[name][0](name, attr)

 

    def __End(self, name):

        self.handle[name][1](name)

 

    def __Property_Start(self, name, attr):

        self.value = ”

 

    def __Property_End(self, name):

        self.item[name]=self.value.decode(‘utf-8′)

 

    def __Property_Value(self, data):

        self.value=data

 

    def __Item_Start(self, name, attr):

        self.item={}

 

    def __Item_End(self, name):

        self.body.append(self.item)

 

    def __Body_Start(self, name, attr):

        self.body=[]

 

    def __Body_End(self, name):

        pass

 

    def SendEgloosPost(self, title, content):

        egloos = xmlrpclib.Server(“https://rpc.egloos.com/rpc1″)

        datastruct = {‘category’: ‘미분류’, ‘description’: content, ‘title’:title}

        no = egloos.metaWeblog.newPost(“”, self.username, self.password, datastruct, True)

 

    def SendPost(self):

        for record in self.body:

            self.SendEgloosPost(record['title'], record['content'])

            print ‘title: ‘, record['title'], ‘\n’  

            print ‘content: ‘, record['content'], ‘\n’  

 

 

f = open(“samjv_simple.xml”)

 

xmlData = f.read() 

a=BodyLoader()

a.Run()

 

f.close()

 

——————————————————————————–

 

클래스하고 클래스 핸들링까지 작성 끝! 바로 run!!

 

왠걸 작동을 안한다.

parsing 하는 중에 제대로 값이 안넘어간다.

원인은 blogger RSS는 atom을 따르는데 xml에 속성들이 꽤 많다.

 

——————————————————————————–

<?xml version=’1.0′ encoding=’UTF-8′?>

<?xml-stylesheet href=”http://www.blogger.com/styles/atom.css” type=”text/css”?>

<feed xmlns=’http://www.w3.org/2005/Atom’ xmlns:openSearch=’http://a9.com/-/spec/opensearchrss/1.0/’ xmlns:georss=’http://www.georss.org/georss’>

<id>tag:blogger.com,1999:blog-29738970</id>

<updated>2009-02-19T03:29:40.932+09:00</updated>

<title type=’text’>은은한 자스민 벡터</title>

<subtitle type=’html’>(The jasmine Vector of distant peals)</subtitle>

<link rel=’http://schemas.google.com/g/2005#feed’ type=’application/atom+xml’ href=’http://sam-jv.blogspot.com/feeds/posts/default’/>

<link rel=’self’ type=’application/atom+xml’ href=’http://sam-jv.blogspot.com/feeds/posts/default’/>

<link rel=’alternate’ type=’text/html’ href=’http://sam-jv.blogspot.com/’/>

<link rel=’next’ type=’application/atom+xml’ href=’http://sam-jv.blogspot.com/feeds/posts/default?start-index=26&amp;max-results=25′/>

<author>

<name>Samuel</name>

<uri>http://www.blogger.com/profile/18326591820717088420</uri>

<email>noreply@blogger.com</email>

</author>

<generator version=’7.00′ uri=’http://www.blogger.com’>Blogger</generator>

<openSearch:totalResults>111</openSearch:totalResults>

<openSearch:startIndex>1</openSearch:startIndex>

<openSearch:itemsPerPage>25</openSearch:itemsPerPage>

<entry>

<id>tag:blogger.com,1999:blog-29738970.post-7428656882510326883</id>

<published>2009-01-14T14:36:00.001+09:00</published>

<updated>2009-01-14T23:59:58.422+09:00</updated>

<app:edited xmlns:app=’http://purl.org/atom/app#’>2009-01-14T23:59:58.422+09:00</app:edited>

<category scheme=’http://www.blogger.com/atom/ns#’ term=’번역’/>

<title type=’text’>Project Darkstar server tutorial 번역</title>

<content type=’html’> … 중략 …</content>

<link rel=’replies’ type=’application/atom+xml’ href=’http://sam-jv.blogspot.com/feeds/7428656882510326883/comments/default’ title=’게시물 덧글’/>

<link rel=’replies’ type=’text/html’ href=’http://sam-jv.blogspot.com/2009/01/project-darkstar-server-tutorial-ko.html#comment-form’ title=’0개의 덧글’/>

<link rel=’edit’ type=’application/atom+xml’ href=’http://www.blogger.com/feeds/29738970/posts/default/7428656882510326883?v=2′/>

<link rel=’self’ type=’application/atom+xml’ href=’http://sam-jv.blogspot.com/feeds/posts/default/7428656882510326883′/>

<link rel=’alternate’ type=’text/html’ href=’http://sam-jv.blogspot.com/2009/01/project-darkstar-server-tutorial-ko.html’ title=’Project Darkstar server tutorial 번역’/>

<author>

<name>Samuel</name>

<uri>http://www.blogger.com/profile/18326591820717088420</uri>

<email>noreply@blogger.com</email>

</author>

<thr:total xmlns:thr=’http://purl.org/syndication/thread/1.0′>0</thr:total>

</entry>

——————————————————————————–

 

이런 식인데 

<feed xmlns=’http://www.w3.org/2005/Atom’ xmlns:openSearch=’http://a9.com/-/spec/opensearchrss/1.0/’ xmlns:georss=’http://www.georss.org/georss’>

여기서부터 인식이 안된다.

음..

안되면 바꿔!

editor(난 acroedit를 쓴다) 매크로 기능을 써서 <entry>, <title>, <content>만 떼어 냈다.

 

——————————————————————————–

<feed>

<entry>

<title>Project Darkstar server tutorial 번역</title>

<content><br /><br />  … 중략 …]]></content>

</entry>

<entry>

<title>수트를 제대로 입는 Tip 95가지 -GQ</title>

<content><< 수트를 제대로 입는 Tip 95가지 -GQ >><br /><br /> … 중략 …]]></content>

</entry>

</feed>

——————————————————————————–

 

이런 식으로.

다시 run!

실패.. ~_~

뭐 뻔한게 <content>안에 html tag들이 있으니 xml 파싱에 실패한거였다.

다시 바꿔!

 

——————————————————————————–

<feed>

<entry>

<title>Project Darkstar server tutorial 번역</title>

<content><![CDATA[<br /><br /> ... 중략 ...]]></content>

</entry>

<entry>

<title>수트를 제대로 입는 Tip 95가지 -GQ</title>

<content><![CDATA[<< 수트를 제대로 입는 Tip 95가지 -GQ >><br /><br /><br /> ... 중략 ...]]></content>

</entry>

</feed>

——————————————————————————–

 

<![CDATA[ 태그를 활용하면 간단.

또 다시 run!!

아름답게 egloos로 올라갔다.

그럼 이제 wordpress로 올려볼까 했는데…

설치형 wordpress에 API를 어떻게 쓰는 지 도저히 찾을 수가 없다..

결국.. 노가다를.. -_-;;

 

그래도 중요한 건 생애 첫 python 프로그램을 만들었다는 거!