import md5, urllib, httplib, camera, key_codes, os, os.path
API_KEY = "c62dcNiOgTeVhAaLdIxDsdf17ee3afe9"
SECRET = "c932fFxAkK9E648d"
API_URL = "http://flickr.com/services/rest/"
UPLOAD_URL = "http://api.flickr.com/services/upload/"
IMG_FILE = u"E:\\Data\\Instaflickr\\instaflickr.jpg"
TOKEN_FILE = u"E:\\Data\\Instaflickr\\instaflickr.txt"
BLOCKSIZE = 8192
PROGRESS_COLOR = (0, 255, 0)
TEXT_COLOR = (255, 0, 0)
if not os.path.exists("E:\\Data\\Instaflickr"):
os.makedirs("E:\\Data\\Instaflickr")
def naive_xml_parser(key, xml):
key = key.lower()
for tag in xml.split("<"):
tokens = tag.split()
if tokens and tokens[0].lower().startswith(key):
return tag.split(">")[1].strip()
return None
def load_token():
global flickr_token
try:
flickr_token = file(TOKEN_FILE).read()
except:
new_token()
def new_token():
global flickr_token
mini_token = appuifw.query(u"Give Flickr mini-token "
u"(e.g. 123456100)", "number")
if not mini_token:
return
params = {"method": "flickr.auth.getFullToken",
"api_key": API_KEY,
"mini_token": str(mini_token)}
flickr_token = naive_xml_parser("token",
flickr_signed_call(params))
if flickr_token:
try:
f = file(TOKEN_FILE, "w")
f.write(flickr_token)
f.close()
appuifw.note(u"Token saved!", "info")
except:
appuifw.note(u"Could not save token",
"error")
else:
appuifw.note(u"Invalid token", "error")
def flickr_signed_call(params):
keys = params.keys()
keys.sort()
msg = SECRET
for k in keys:
if k != "photo":
msg += k + params[k]
params['api_sig'] = md5.new(msg).hexdigest()
if "photo" in params:
return flickr_multipart_post(params)
else:
url = API_URL + "?" + urllib.urlencode(params)
return urllib.urlopen(url).read()
def flickr_multipart_post(params):
BOUNDARY = "----ds9io349sfdfd!%#!dskm"
body = []
for k, v in params.items():
body.append("--" + BOUNDARY)
part_head = 'Content-Disposition: '
'form-data; name="%s"' % k
if k == "photo":
body.append(part_head +
';filename="up.jpg"')
body.append('Content-Type: image/jpeg')
else:
body.append(part_head)
body.append('')
body.append(v)
body.append("--" + BOUNDARY + "--")
body_txt = "\r\n".join(body)
proto, tmp, host, path = UPLOAD_URL.split('/', 3)
h = httplib.HTTP(host)
h.putrequest('POST', "/%s" % path)
h.putheader('content-type',
"multipart/form-data; boundary=%s" % BOUNDARY)
h.putheader('content-length', str(len(body_txt)))
h.endheaders()
try:
offs = 0
for i in range(0, len(body_txt), BLOCKSIZE):
offs += BLOCKSIZE
h.send(body_txt[i: offs])
progress_bar(min(1.0,
offs / float(len(body_txt))))
errcode, errmsg, headers = h.getreply()
return h.file.read()
except:
return None
def progress_bar(p):
y = canvas.size[1] / 2
max_w = canvas.size[0] - 30
canvas.rectangle((15, y, p * max_w, y + 10),\
fill = PROGRESS_COLOR)
def show_text(txt):
s = canvas.size
canvas.text((10, s[1] / 2 - 15), txt,\
fill=TEXT_COLOR, font="title")
def finder_cb(im):
canvas.blit(im)
def start_viewfinder():
if flickr_token:
camera.start_finder(finder_cb)
canvas.bind(key_codes.EKeySelect, take_photo)
else:
appuifw.note(u"Give a Flickr token first",
"error")
def take_photo():
canvas.bind(key_codes.EKeySelect, None)
camera.stop_finder()
show_text(u"Hold still!")
image = camera.take_photo(size = (640, 480))
s = canvas.size
canvas.blit(image,target=(0,0,s[0],(s[0]/4*3)), scale=1)
show_text(u"Uploading to Flickr...")
image.save(IMG_FILE)
jpeg = file(IMG_FILE, "r").read()
params = {'api_key': API_KEY, 'title': 'InstaFlickr',
'auth_token': flickr_token,\
'photo': jpeg}
ret = flickr_signed_call(params)
canvas.clear((255, 255, 255))
if ret:
show_text(u"Photo sent ok!")
else:
show_text(u"Network error")
def access_point():
global ap_id
ap_id = socket.select_access_point()
apo = socket.access_point(ap_id)
socket.set_default_access_point(apo)
def quit():
camera.stop_finder()
app_lock.signal()
appuifw.app.exit_key_handler = quit
appuifw.app.title = u"InstaFlickr"
appuifw.app.menu = [(u"Take photo", start_viewfinder),
(u"New token", new_token),
(u"Access point", access_point),
(u"Quit", quit)]
appuifw.app.body = canvas = appuifw.Canvas()
canvas.clear((255, 255, 255))
show_text(u"Welcome to InstaFlickr")
load_token()
app_lock = e32.Ao_lock()
app_lock.wait()
没有合适的资源?快使用搜索试试~ 我知道了~
python一些实例
共89个文件
py:89个
5星 · 超过95%的资源 需积分: 9 82 下载量 180 浏览量
2009-06-27
22:46:23
上传
评论
收藏 36KB RAR 举报
温馨提示
python实例Python 是一种功能强大的解释性,交互性,面向对象(OO)的第四代计算机编程语言。她集成了模块、包、例外机制、动态造型以及非常高级的动态数据类型。它的语法表达优美易读。它具有很多优秀的脚本语言的特点:解释的,面向对象的,内建的高级数据结构,支持模块和包,支持多种平台,可扩展。而且它还支持交互式方式运行,图形方式运行。它的语法有很多与众不同的特性
资源推荐
资源详情
资源评论
收起资源包目录
PY实例.rar (89个子文件)
pys60
102-searchlistcomph.py 199B
065-sysinfo.py 62B
023-mp.py 143B
045-rwtext.py 413B
108-autoupdate.py 226B
066-httpclient.py 80B
064-contacts.py 471B
051-vocabu.py 3KB
067-logodownloader.py 277B
024-blockingmp.py 359B
037-zapper.py 3KB
095-instaflickr.py 6KB
011-firstapp.py 270B
048-localdb.py 492B
049-location.py 47B
036-camera.py 586B
009-twodialogs.py 110B
013-smsvoter.py 299B
047-filedict.py 202B
079-votingclient.py 1KB
060-pyserialpc.py 670B
028-bindkeycode.py 384B
044-readvideo.py 76B
106-wsintros.py 910B
061-btapplescript.py 445B
012-firstmenus.py 513B
087-mopymaps.py 3KB
073-jsonphotosender.py 712B
016-inboxsorter.py 1KB
010-firstfunction.py 152B
014-smsinbox.py 139B
062-gpsreader.py 563B
040-directory.py 102B
086-wsserver.py 1KB
004-multiquery.py 221B
124-mobileosc.py 3KB
063-telephone.py 91B
001-helloworld.py 121B
027-animalsounds.py 878B
080-jsongateway.py 2KB
043-readimage.py 67B
032-screenshot.py 104B
055-rfcommdisco.py 98B
034-viewfinder.py 354B
005-popupmenu.py 277B
109-plugin.py 502B
077-phoneip.py 127B
071-yahoowstest.py 346B
035-minicam.py 81B
003-notes.py 120B
082-instamese.py 4KB
090-eventfu.py 4KB
030-keydown.py 1KB
3dcube.py 3KB
057-btchat.py 2KB
104-distconst.py 73B
008-shoppinglist.py 509B
105-symtable.py 211B
041-fileio.py 120B
074-jsonphotoserver.py 516B
054-obexdisco.py 103B
050-gsmloca.py 2KB
031-dgraph.py 1KB
056-photosphone.py 948B
078-votingserver.py 2KB
103-inputsanita.py 124B
007-multilist.py 277B
033-movinggraph.py 1KB
068-photouploader.py 363B
046-dictfile.py 160B
022-tts.py 86B
070-tcpclient.py 207B
072-defaultap.py 123B
017-smsreceiver.py 330B
107-custommod.py 88B
029-keyevent.py 421B
018-smsgateway.py 558B
006-selectionlist.py 211B
015-inboxsearch.py 389B
075-httpserver.py 2KB
059-btclient.py 1KB
085-wsreqhandlers.py 457B
025-midi.py 144B
042-readsound.py 76B
019-hangman.py 2KB
069-testserver.py 368B
101-listcomph.py 39B
026-soundrec.py 598B
002-dialogs.py 230B
共 89 条
- 1
资源评论
- xiashilongflybird2014-06-04好资源,要是实例多点,注释多点就好了
- liushouyu19822012-11-05实例多一点会更好
- Javi2013-10-30实例有点少
- baoxiang6232012-06-14有点深,看不太懂。正在学习
zzyjupiter
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- C语言-leetcode题解之70-climbing-stairs.c
- C语言-leetcode题解之68-text-justification.c
- C语言-leetcode题解之66-plus-one.c
- C语言-leetcode题解之64-minimum-path-sum.c
- C语言-leetcode题解之63-unique-paths-ii.c
- C语言-leetcode题解之62-unique-paths.c
- C语言-leetcode题解之61-rotate-list.c
- C语言-leetcode题解之59-spiral-matrix-ii.c
- C语言-leetcode题解之58-length-of-last-word.c
- 计算机编程课程设计基础教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功