Differences between revisions 1 and 2
Revision 1 as of 2006-08-08 06:06:53
Size: 1375
Editor: HuangYi
Comment: 方便python程序使用yupoo(一个相册)提供的api
Revision 2 as of 2006-08-08 06:09:26
Size: 1390
Editor: HuangYi
Comment:
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
 * [[MailTo([email protected])]]  * by ["huangyi"] [[MailTo([email protected])]]

方便python程序使用yupoo(一个相册)提供的api

   1 from xml.dom import minidom
   2 from urllib2 import urlopen
   3 
   4 API_KEY = 'd48fd2a7914b4d5d3efcd52c6ab61e79'
   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

评论

有什么想法,就加在这里吧 _

yupoo_api_wrapper (last edited 2009-12-25 07:16:38 by localhost)