想必大家都知道sina推了一個叫Sina App Engine(SAE)的東西,就是模仿google的GAE啦.這個東西可以做免費的服務器用,SAE最近支持了python,作為一個業余的python愛好者,天上掉下的餡餅豈有不吃之理?
本屌的業余愛好就是看微博,糗百上的搞笑段子,一碰到經典搞笑的圖片就想保存下來留作日后慢慢欣賞。所以呢,我就在SAE上用python搭了一個小網站,上傳些平時存下來的搞笑圖片:http://yesyouknow.sinaapp.com.(基情提醒:不要把本屌的小網站搞癱了)可是這樣每次看到搞笑圖片的都先要保存到本地,然后還要找時間打包上傳,想想都煩的慌啊。
還好有強大的chrome,在簡單的看了chrome開發教程后(教程點我),本屌依葫蘆畫瓢做了一個擴展,可以一鍵上傳圖片到本屌的小網站。想試一試的基友們可以下載這個擴展:點我,打開下載后的文件只要直接將yesyouknow.crx文件拖拽到chrome瀏覽器窗口上就能安裝了.最后在網頁圖片上右擊便會彈出如下菜單:
看到那個”上傳到yesyouknow”的菜單了嘛,點了過后就會上傳到yesyouknow.sinaapp.com啦,刷新下網站就會看到剛剛上傳的圖片鳥.在yesyouknow網站上的圖片可以直接引用,也可以到SAE的管理后台批量下載,這樣是不是很方便了呢?
下面介紹實現方法,關於如何在SAE用python建站,可以直接去SAE首頁以及SAE python手冊查看,這里本屌就不羅嗦了.直接上代碼.
SAE Python支持web.py,所以開發方便了不少.
1. 網站首頁:index.py
這里很簡單就是從storage存儲中獲取圖片的鏈接列表,然后按時間排序每頁顯示4張圖片.
# -*- coding: utf-8 -*- import sae import web import string class Index: def GET(self): page_index=-1 user_data=web.input(page='0') page_index=string.atoi(user_data.page) st=sae.storage.Client() st_list=st.list('yesyouknow2') url_list=[]#只存放排序后的url,其它的都去掉 for d in st_list: url_list.append(st.url('yesyouknow2',d['name'])) url_list.sort() url_list.reverse() html_pic='' part_list=[] html_info="" if page_index>=len(st_list): page_index-=4 html_info='<div align="center">~~~~~~~~~這是最后一頁了~~~~~~~~~~</div>' elif page_index<0: page_index=0 html_info='<div align="center">~~~~~~~~~~這是第一頁~~~~~~~~~~~~~~</div>' if page_index+4>=len(url_list): part_list=url_list[page_index:] else: part_list=url_list[page_index:page_index+4] for puburl in part_list: #print part_list html_pic+='<div align="center"><img src="%s"/></div>'%(puburl.encode('utf-8')) html_space="<p> </p>" html_page=r'<div align="center"><a href="?page=%s">上一頁</a> <a href="?page=%s">下一頁</a></div>'%(str(page_index-4),str(page_index+4)) html_head=r'<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>嘿嘿</title></head><body>' html_end=r'</body></html>' html_count=r'<div align="center">當前共%s張圖片</div>'%(str(len(url_list))) form=r''' <div align="center">可以上傳單個圖片,也可以打包為zip文件批量上傳.文件大小控制在7M以內 <form action="http://yesyouknow.sinaapp.com/upload" method="post" enctype="multipart/form-data"> <input type="file" name="myfile" /> <input type="submit" /> </form> </div> ''' html=html_head html+=html_count html+=form html+=html_page html+=html_space html+=html_info html+=html_pic html+=html_space html+=html_page html+=html_end return html
2.接受上傳文件的代碼:upload.py
這里處理在網站首頁上傳的圖片,如果上傳的是zip文件,就在內存中解壓,然后將圖片挨個放進storange中
# -*- coding: utf-8 -*- import sae.storage import web import zipfile from datetime import datetime class Upload: def POST(self): web.header("Content-Type","text/html; charset=utf-8") x=web.input(myfile={}) if 'myfile' in x and x.myfile.filename!='': self.file_upload(x) web.seeother('/') def file_upload(self,x): filepath=x.myfile.filename.replace('\\','/') filename=filepath.split('/')[-1] filename=datetime.now().strftime("%Y%m%d%H%M%S%f")+'.'+filename.split('.')[-1]#將文件名修改為當前日期,方便后面排序 if ".zip" in filename: self.unzip_upload(x.myfile.file) else: st=sae.storage.Client() ob=sae.storage.Object(x.myfile.file.read()) sturl=st.put('yesyouknow2',filename,ob) def unzip_upload(self,zip_file): st=sae.storage.Client() z=zipfile.ZipFile(zip_file) namelist=z.namelist() for name in namelist: file=z.read(name) filename=datetime.now().strftime("%Y%m%d%H%M%S%f")+'.'+name.split('.')[-1]#日期加上文件后綴名 ob=sae.storage.Object(file) st.put('yesyouknow2',filename,ob)
3.接受chrome擴展發送過來的圖片鏈接:backupdate.py
#coding:utf-8 ##接收chrome插件上傳的圖片鏈接,將圖片下載存儲到storage中. import sae import web import urllib2 import sae.storage from datetime import datetime class Backupdate: def GET(self): web.header('Access-Control-Allow-Origin','*') user_data=web.input(src="no_exist") img_src=user_data.src img_data=urllib2.urlopen(img_src).read() filename=datetime.now().strftime("%Y%m%d%H%M%S%f")+'.'+img_src.split('.')[-1]#將文件名修改為當前日期,方便后面排序 st=sae.storage.Client() ob=sae.storage.Object(img_data) st.put('yesyouknow2',filename,ob) return "ok",img_src
4.至於chrome擴展的話就更簡單了,只用了一個函數用來發送當前的圖片鏈接給yesyouknow網站.
chrome.contextMenus.create方法就是創建菜單了,點擊菜單就會通過yesyouknowOnClick()來處理
//上傳你喜歡的圖片到yesyouknow.sinaapp.com function yesyouknowOnClick(info,tab){ console.log("hello yesyouknow"); console.log(info.srcUrl); xmlhttp=new XMLHttpRequest(); url="http://yesyouknow.sinaapp.com/backupdate?src="; url+=info.srcUrl; console.log(url) xmlhttp.open("GET",url,false); xmlhttp.send(null); } var title_yesyouknow=chrome.contextMenus.create({"title": "上傳到yesyouknow","contexts":["image"],"onclick":yesyouknowOnClick});
最后附上yesyouknow小站源碼:點我
祝大家玩得開心.