python批量上传图片到Flickr(linux)
时间:2010-09-30 08:58来源:未知 作者:admin 点击:
214次
前提条件:
1、安装Python flickr api库
首先安装easy_install
wget http://peak.telecommunity.com/dist/ez_setup.py |
现在以后,安装:
sudo python ez_setup.py |
安装成功以后,安装flikcr库
sudo easy_install flickrapi |
2、申请一个flick api key,不再废话了,非常简单。
开始,一个Simple程序:
- #!/usr/bin/python
- import flickrapi
- import os
- import time
- #填入申请的api_key和secret_key
- api_key ='Flickr api key'
- secret_key='Flickr api secret key'
- flickr = flickrapi.FlickrAPI(api_key,secret_key)
- (token, frob) = flickr.get_token_part_one(perms='write')
- if not token:
- raw_input('Press enter after authorized this program')
- flickr.get_token_part_two((token,frob))
- #一个类,防止重复上传
- class UploadHistory:
- def __init__(self,filename='uh.dat'):
- self.filename=filename
- self.uploads=[]
- def readcfg(self):
- if os.path.exists(self.filename):
- f=open(self.filename,'r')
- for line in f.readlines():
- self.uploads.append(line[0:-len(os.linesep)])
- f.close()
- def writecfg(self):
- f=open(self.filename,'w')
- for up in self.uploads:
- f.write(up+os.linesep)
- f.close()
- def isUploaded(self,filename):
- if self.uploads.count(filename)>0:
- return True
- else:
- self.uploads.append(filename)
- return False
- def addUpload(self):
- pass
- #上传一个目录下面的文件
- homedir=os.environ['HOME']
- today=time.strftime('%Y-%m-%d')
- savedir=homedir+'/temp/'+today
- files=os.listdir(savedir)
- uh=UploadHistory()
- for f in files:
- if f[-4:]=='.jpg':
- rf = savedir + '/' + f
- if not uh.isUploaded(rf):
- flickr.upload(filename=rf,tags='iheartchaos',is_public=0)
- uh.writecfg()
第一次运行,需要Web验证,以后就不需要了。