使用web.py在BAE建立自已的电影网站
2019獨角獸企業重金招聘Python工程師標準>>>
最近在網上看了一篇文章使用web.py在BAE上建立電影網站,http://www.51bigfool.com/%E6%88%91%E6%98%AF%E5%A6%82%E4%BD%95%E7%94%A8bae%E5%92%8Cweb-py%E6%89%93%E9%80%A0%E8%B1%86%E7%93%A3%E7%94%B5%E5%BD%B1top100%E7%9A%84.html 我自已也在此基礎上做了一些改進,也在一點點的熟悉使用web.py這個框架,可以看一下我弄了一半的應用 http://mypythontest.duapp.com/
準備 BAE web.py
1. 在BAE上建立一個python的應用(快速創建即可),選擇Iframe
之后進入云引擎將環境類型設置為python
先簡單看一下目錄結構如下
│? index.py
│? model.py
│
├─static
│? └─images
│????????? lg_movie_a12_2.png
│
└─templates
??? │? add.html
??? │? base.html
??? │? index.html
??? │? play.html
??? │? show.html
??? │? top250style.css
新建一個Mysql,進入phpmyadmin導入所需的表
?
CREATE?TABLE?DoubanTop250(id?INT?AUTO_INCREMENT?,moviename?TEXT,score?FLOAT,url?TEXT,PRIMARY?KEY?(?id?) )我先按照原來的教程添加了相應的代碼
model.py
#?_*_?coding:utf-8?_*_ import?web import?web.db from?bae.core?import?constdbname?=?"數據庫名字"db?=?web.database(dbn='mysql',host=const.MYSQL_HOST,port=int(const.MYSQL_PORT),user=const.MYSQL_USER,passwd=const.MYSQL_PASS,db=dbname )def?additem(name,?rating,?address):return?db.insert('DoubanTop250',?moviename=name,?score=rating,?url=address)def?get_items():return?db.select('DoubanTop250',?order='id')def?get_item(id):return?db.select('DoubanTop250',?where='id=$id',?vars=locals())[0]index.py
#_*_?coding:utf-8?_*_ import?web import?model import?os import?reurls?=?("/",?"Index","/play/(\d+)",?"Play" )t_globals?=?{'datestr':?web.datestr }app_root?=?os.path.dirname(__file__) template_root?=?os.path.join(app_root,?'templates/') render?=?web.template.render(template_root,?base='base',?globals=t_globals)class?Index:def?GET(self):items?=?model.get_items() #???????return?render.test(items)return?render.index(items)class?Play:def?POST(self,?id):id?=?int(id)item?=?model.get_item(id)return?render.play(item)app?=?web.application(urls,?globals()).wsgifunc() from?bae.core.wsgi?import?WSGIApplication application?=?WSGIApplication(app)base.html
$def?with?(page) <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html??xmlns="http://www.w3.org/1999/xhtml"><head><meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/><title>?楊彥星的自娛自樂</title><link?type="text/css"?rel="stylesheet"?href="/templates/top250style.css"?/></head><body><div?id="header"><a?target="_blank"?href="http://movie.douban.com/chart"?title="豆瓣電影排行旁"><img?src="/static/images/lg_movie_a12_2.png"?></a></div><div?id="main"><h1>?這是瞎寫的我會亂說嗎|<a?target="_blank"?href?=?"/addmovie/">添加電影</a></h1>$:page</div><div?id="footer"><div?id="copyright"><p>本站純屬自娛自樂與學習python之用,電影啥的全是網上收集</p><a?href?=?"/">返回首頁</a><p><em>Proudly?powered?by?Web.py</em></p></div></div></body> </html>index.html
$def?with(items)<table><tr><th>排名</th><th>影片名</th><th>得分</th><th>百度影音播放</th></tr>$for?item?in?items:<tr><td?id="tdstyle">$item.id</td><td?id="tdstyle">$item.moviename</td><td?id="tdstyle">$item.score</td><td?id="tdstyle"><form?action="/play/$item.id"?method="post"><input?type="submit"?value="播放"?/></form></td></tr></table>play.html
$def?with(item) <h2>$item.id?$item.moviename</h2> <blockquote><object?classid="clsid:02E2D748-67F8-48B4-8AB4-0A085374BB99"?width="600"?height="400"?id="BaiduPlayer"?name="BaiduPlayer"?onError=if(window.confirm('請您先安裝百度影音軟件,然后刷新本頁才可以正常播放.')){window.open('http://player.baidu.com')}else{self.location='http://player.baidu.com'}><PARAM?NAME='URL'?VALUE=$item.url><PARAM?NAME='Autoplay'?VALUE='1'></object><a?title="返回首頁"?href="http://kanimdb.duapp.com">?返回首頁?</a> </blockquote>top250style.css
body{background-image:?url(tw.jpg);background-position:right;background-color:?#fff;margin-left:15%;margin-right:15%;border:?2px?dotted?gray;padding:?20px?20px?20px?20px;font-family:?sans-serif; }h1?{font-family:?sans-serif;font-weight:?bolder;;color:?green;border-bottom:?2px?solid?gray;text-align:?center; }table?{width:?500px;text-align:?center;margin-top:?30px;margin-left:?20%;margin-right:?20%;border:?thin?solid?black;border-collapse:?collapse; }th,?#tdstyle?{border:?thin?dotted?gray;padding:?5px;background-color:?silver; } th?{background-color:?#fcba7a; }#main?{padding-top:?20px;padding-bottom:?20px; }#bdplaer?{padding-left:?10px;padding-right:?10px; }#header?{text-align:?center; }#footer?{padding:?10px,?10px,?10px,?10px;text-align:?center; }#copyright?{padding:?10px,?10px,?10px,?10px;font-size:?small; }修改app.conf
handlers:-?url?:?/static/images/(.*)script:?/static/images/$1-?url?:?/templates/(.*)script:?/templates/$1-?url?:?/(.*)script:?index.py-?expire?:?.jpg?modify?10?years-?expire?:?.swf?modify?10?years-?expire?:?.png?modify?10?years-?expire?:?.gif?modify?10?years-?expire?:?.JPG?modify?10?years-?expire?:?.ico?modify?10?years好了,現在這個應用基本上就可以看了,不過現在請忘了以上的代碼。因為我們要為其添加一些自已的想要的東西
首先我們要添加“添加影片”功能
修改urls配置
urls?=?("/","Index","/play/(\d+)","Play","/add/","Add","/addmovie/","addmovie" )這里的addmovie 其實只是想render到一個模板中
添加class addmovie實現
class?addmovie:def?GET(self):return?render.add()添加add.html
<form?action="/add/"?method="post"> 電影名:<input?type="text"?name?=?"name"><br> 得分:<input?type="text"?name?=?"rating"><br> 播放地址:<input?type="text"?name?=?"address"><br> <input?type="submit"?value="add"> </form>很簡單的表單,我前端不會,只會寫簡單的html
這里name值可以隨便寫,有意義或者你自已認識即可,反正后面的python代碼會做處理
這里將movie的信息post到/add/中
轉為urls里已經配置了
"/add/","Add",
下面來寫class Add實現
class?Add:def?POST(self):movieinfo?=?web.data()#model.additem(movieinfo)#return?render.show(movieinfo)postlist?=?movieinfo.split('&')???movie?=?[]for?i?in?postlist:movie.append(i.split('=')[1])model.additem(urllib.unquote(movie[0]),movie[1],urllib.unquote(movie[2]))web.seeother("/")這里的代碼寫的太ugly了,但是可以滿足需要,如果有更好的實現可以和我說下
先將post來的數據使用&分割,將會得到name=xxx,rating=xxx,address=xxx的列表
接著來獲取相應的值,也就是再將列表中的各個項再用‘=’進行分割,然后將值append到一個新的列表中
然后將新的列表中的各個值進行unquote,要不你插入到數據庫中的將不是中文等,而是quote過的數據
插件結束后跳轉到首頁 web.seeother(“/”)
好了,現在我們完成了添加電影的操作
2.完成后你現在就去找些電影和百度影音地址來添加看看,但是我發現一個新的問題,百度影音在非IE瀏覽器上是不好用的,這還了得……
在網上找方法,有一個通過 javacsript先判斷瀏覽器的appname來決定使用哪個,http://pcbbs.baidu.com/thread-62871-1-1.html
這個是我試過了,可以實現在chrome與FF下調用百度影音,但是我要傳的$item.url不能傳入javascrip中,傳入后就是$item.url,在網上查找說可以要寫兩個$,我也試過了,但是還是不能播放,于是我決定放棄IE用戶,讓所有播放都走非IE的方法中,愛咋地咋地。
百度這點做的就不如qvod,人家官方都出了在非IE瀏覽器上調用的方法,而百度還在使用IE,真是不思進取,我也決定在網頁上調用qvod播放接口
那么新的問題又來了,程序怎么知道你是要用qvod還是百度來播放?其實很簡單,你在添加播放地址的時候,qvod的地址是以qvod開始的,百度影音是以bdhd開始的,只要判斷一下$item.url是以哪個開頭的就可以了
但是網上更多的還是thunder,edk等協議的影視資源,這些無論是百度還是qvod都無法播放,于是我想到了現在很流行的云點播,迅雷的云點播可以播放,但是非白金會員只能播放一段時間,隨便找了幾個提供在線云點播的,有彈出式廣告的占大多數,于是找了一個沒有廣告的,okdvd,其接口調用也挺簡單的
<iframe scrolling="no" frameborder="0" name="win_vod" id="win_vod" border="0" style="width: 950px; height: 550px" src="http://www.okdvd.com/api.php#!url=后面加上播放地址"></iframe>
所以最終修改我的play.html為
$def?with(item) <h2>$item.id?$item.moviename</h2> <blockquote> <div?align?=?"center"> $if?item.url.startswith('qvod'):<object?classid="clsid:F3D0D36F-23F8-4682-A195-74C92B03D4AF"?width="860"?height="460"id="QvodPlayer"?name="QvodPlayer"?onerror="document.getElementById('QvodPlayer').style.display='none';document.getElementById('iframe_down').style.display='';document.getElementById('iframe_down').src='http://error2.qvod.com/error4.htm';"><param?name='Showcontrol'?value='2'><param?name='URL'?value=$item.url><param?name='Autoplay'?value='1'><embed?id="QvodPlayer2"?name="QvodPlayer2"?width="860"?height="460"?URL=$item.url?type='application/qvod-plugin'?Autoplay='1'?Showcontrol='1'?></embed></object>???? $elif?item.url.startswith('bdhd'):<object?id="BaiduPlayer"?name="BaiduPlayer"?type="application/player-activex"?width="860"?height="460"?progid="Xbdyy.PlayCtrl.1"?param_URL=$item.url?param_OnPlay="onPlay"?param_OnPause="onPause"?param_OnFirstBufferingStart="onFirstBufferingStart"?param_OnFirstBufferingEnd="onFirstBufferingEnd"?param_OnPlayBufferingStart="onPlayBufferingStart"?param_OnPlayBufferingEnd="onPlayBufferingEnd"?param_OnComplete="onComplete"?param_Autoplay="1"></object> $else:<iframe?scrolling="no"?frameborder="0"?name="win_vod"?id="win_vod"?border="0"?style="width:?860px;?height:?460px"?src="http://www.okdvd.com/okapi.php#!url=$item.url"></iframe> </div> </blockquote>這里有一個問題困擾了我很長時間,原來這里的模板如果有python代碼的話也是要遵循其縮進原則的,如$if 下面的<object>如果和$if同級,那么也會報錯,BAE提供錯誤日志,可以很快的查看到哪里出錯了
3.首頁上播放按鈕現在還都是“播放”字樣,我想讓它顯示是百度還是qvod播放,其實方法和上面一樣,判斷$item.url的startswith()方法,另外我又有了新的需求,因為網上有些電影的資源實在不好找到百度或者qvod與thunder或者edk資源,但是優酷或者土豆上有,那我可以讓其跳轉到相應的網址上播放
所以最終的index.html修改成這樣
$def?with(items)<table><tr><th>排名</th><th>影片名</th><th>得分</th><th>播放</th></tr>$for?item?in?items:<tr><td?id="tdstyle">$item.id</td><td?id="tdstyle">$item.moviename</td><td?id="tdstyle">$item.score</td><td?id="tdstyle">$if?item.url.startswith("http"):??????????????????<form?action="$item.url"?method="get"?target="_blank"><input?type="submit"?value="新頁面播放"?/></form>$elif?item.url.startswith('bdhd'):<form?action="/play/$item.id"?method="post"><input?type="submit"?value="百度影音播放"?/></form>$elif?item.url.startswith('qvod'):<form?action="/play/$item.id"?method="post"><input?type="submit"?value="qvod播放"?/></form>$else:<form?action="/play/$item.id"?method="post"><input?type="submit"?value="云點播播放"?/></form></td></tr></table>而最終的index.html修改成這樣
#-*-?coding:utf-8?-*- import?web import?model import?os import?re import?urlliburls?=?("/","Index","/play/(\d+)","Play","/add/","Add","/addmovie/","addmovie" )t_globals?=?{'datestr':?web.datestr }app_root?=?os.path.dirname(__file__) template_root?=?os.path.join(app_root,?'templates/') render?=?web.template.render(template_root,?base='base',?globals=t_globals)class?Index:def?GET(self):items?=?model.get_items() #???????return?render.test(items)return?render.index(items)class?Play:def?POST(self,?id):id?=?int(id)item?=?model.get_item(id)return?render.play(item)class?Add:def?POST(self):movieinfo?=?web.data()#model.additem(movieinfo)#return?render.show(movieinfo)postlist?=?movieinfo.split('&')???movie?=?[]for?i?in?postlist:movie.append(i.split('=')[1])model.additem(urllib.unquote(movie[0]),movie[1],urllib.unquote(movie[2]))web.seeother("/")class?addmovie:def?GET(self):return?render.add()app?=?web.application(urls,?globals()).wsgifunc() from?bae.core.wsgi?import?WSGIApplication application?=?WSGIApplication(app)?
4.遺留問題,現在沒有權限限制,誰都可以添加,也沒有修改與刪除選項,所以后面我將添加相應的權限管理,另外總不能自已都去手工添加電影,考慮使用爬蟲來自動添加電影
5.獻給熱愛的電影的同胞們,向亞倫·斯沃茨 致敬
? 向湯唯致敬
轉載于:https://my.oschina.net/yangyanxing/blog/173295
總結
以上是生活随笔為你收集整理的使用web.py在BAE建立自已的电影网站的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 随机优化中的样本均值近似方法
- 下一篇: html5 统计图 等值线,等值线及图表