方便python程序使用yupoo(一个相册)提供的api
by huangyi <[email protected]>
1 from xml.dom import minidom
2 from urllib2 import urlopen
3
4 API_KEY = 'put your api key here'
5 base_url = 'http://www.yupoo.com/api/rest/?%s'
6 src_url = 'http://photo%(host)s.yupoo.com/%(dir)s/%(filename)s.jpg'
7 link_url = 'http://www.yupoo.com/photos/view?id=%(id)s'
8
9 def args( **kw ):
10 return '&'.join( [ '%s=%s'%(k,v) for k,v in kw.iteritems()])
11
12 def call(**kw):
13 url = base_url % args( api_key=API_KEY, **kw )
14 f = urlopen( url )
15 xmldoc = minidom.parse( f )
16 return xmldoc.firstChild.firstChild
17
18 def photos_search( tags, **kw ):
19 result = call( method='yupoo.photos.search', tags=tags, **kw )
20 pics = []
21 for n in result.firstChild.childNodes:
22 picurl = src_url % dict(
23 host=n.getAttribute('host'),
24 dir=n.getAttribute('dir'),
25 filename=n.getAttribute('filename'))
26 linkurl = link_url % dict( id=n.getAttribute('id') )
27 pics.append( (picurl,linkurl) )
28 return pics
29
30 if __name__=='__main__':
31 urls = photos_search('mm',per_page=50)
32 assert len(urls)<=50
33 for link,src in urls:
34 print link,src
评论
有什么想法,就加在这里吧 _