Python練習題(三)--視訊展示網站
題目
來源:優達學城--程式設計基礎:python
內容:尋找自己喜歡的視訊,做成網站展示出來
效果演示:
原始碼
主要有三個.py檔案,放到一個資料夾中,執行 entertainment_center.py 即可。(需要python環境)
原始碼地址 : Python-Exercies
網站地址 : 視訊展示網站
其他
對優達原始碼做了改動,支援多種視訊格式,匯入codecs包,寫入html支援中文。
無法觀看的是嵌入youtube視訊(翻牆即可)。
附原始碼
entertainment_center.py
# -*- coding: utf-8 -*-
import media
import fresh_tomatoes
overwatch = media.Movie("Overwatch","守望者們迴歸",
"http://overwatch.nos.netease.com/2/media/Wallpapers/Genji_wallpaper/2048x2048.jpg",
"http://flv.bn.netease.com/videolib3/1506/26/cJtFW2189/HD/cJtFW2189-mobile.mp4")
school_of_rock = media.Movie("School of Rock"," Using rock music to learn",
"http://upload.wikimedia.org/wikipedia/en/thumb/1/11/School_of_Rock_Poster.jpg/220px-School_of_Rock_Poster.jpg",
"https://www.youtube.com/watch?v=3PsUJFEBC74")
spider_gay = media.Movie("美隊3 內戰","搞笑蜘蛛俠",
"http://p1.ifengimg.com/a/2016_19/ea0b92b08ab62d2_size47_w550_h275.jpg",
"https://www.youtube.com/watch?time_continue=16&v=Wa5yWjsjGoI")
overwatch2 = media.Movie("雙龍","雙龍",
"http://overwatch.nos.netease.com/2/media/videos/dragons-animated-short.jpg",
"http://flv.bn.netease.com/videolib3/1605/16/nTaMi2329/HD/nTaMi2329-mobile.mp4")
overwatch3 = media.Movie("虛幻爭霸","虛幻爭霸試玩",
"https://i.ytimg.com/vi/l-TAaE5raeU/hqdefault.jpg",
"https://www.youtube.com/watch?v=l-TAaE5raeU")
overwatch4 = media.Movie("LOL","盤點lol最強操作",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSLfycX_SZr4pT-UVddhr0FdcN67cX1rwFM21PPH3glYPETzGtT",
"http://r.plures.net/plu/tplayer/video-iath80mu.swf?vid=w0162pdtuoi&auto=1")
movies = [overwatch, school_of_rock, spider_gay, overwatch2, overwatch3, overwatch4]
fresh_tomatoes.open_movies_page(movies)
#print(media.Movie.__name__)
#print(media.Movie.__module__)
#print(media.Movie.__doc__)
fresh_tomatoes.py
# -*- coding: utf-8 -*-
import codecs
import webbrowser
import os
import re
# Styles and scripting for the page
main_page_head = '''
<!DOCTYPE html>s
<html lang="zh_CN">
<head>
<meta charset="utf-8">
<title>Fresh Tomatoes!</title>
<!-- Bootstrap 3 -->
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap-theme.min.css">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<style type="text/css" media="screen">
body {
padding-top: 80px;
}
#trailer .modal-dialog {
margin-top: 200px;
width: 640px;
height: 480px;
}
.hanging-close {
position: absolute;
top: -12px;
right: -12px;
z-index: 9001;
}
#trailer-video {
width: 100%;
height: 100%;
}
.movie-tile {
margin-bottom: 20px;
padding-top: 20px;
}
.movie-tile:hover {
background-color: #EEE;
cursor: pointer;
}
.scale-media {
padding-bottom: 56.25%;
position: relative;
}
.scale-media iframe {
border: none;
height: 100%;
position: absolute;
width: 100%;
left: 0;
top: 0;
background-color: white;
}
</style>
<script type="text/javascript" charset="utf-8">
// Pause the video when the modal is closed
$(document).on('click', '.hanging-close, .modal-backdrop, .modal', function (event) {
// Remove the src so the player itself gets removed, as this is the only
// reliable way to ensure the video stops playing in IE
$("#trailer-video-container").empty();
});
// Start playing the video whenever the trailer modal is opened
$(document).on('click', '.movie-tile', function (event) {
var trailerYouTubeId = $(this).attr('data-trailer-youtube-id');
var sourceUrl = trailerYouTubeId ;
$("#trailer-video-container").empty().append($("<iframe></iframe>", {
'id': 'trailer-video',
'type': 'text-html',
'src': sourceUrl,
'frameborder': 0
}));
});
// Animate in the movies when the page loads
$(document).ready(function () {
$('.movie-tile').hide().first().show("fast", function showNext() {
$(this).next("div").show("fast", showNext);
});
});
</script>
</head>
'''
# The main page layout and title bar
main_page_content = '''
<body>
<!-- Trailer Video Modal -->
<div class="modal" id="trailer">
<div class="modal-dialog">
<div class="modal-content">
<a href="#" class="hanging-close" data-dismiss="modal" aria-hidden="true">
![](https://lh5.ggpht.com/v4-628SilF0HtHuHdu5EzxD7WRqOrrTIDi_MhEG6_qkNtUK5Wg7KPkofp_VJoF7RS2LhxwEFCO1ICHZlc-o_=s0#w=24&h=24)
</a>
<div class="scale-media" id="trailer-video-container">
</div>
</div>
</div>
</div>
<!-- Main Page Content -->
<div class="container">
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Fresh Tomatoes Movie Trailers</a>
</div>
</div>
</div>
</div>
<div class="container">
{movie_tiles}
</div>
</body>
</html>
'''
# A single movie entry html template
movie_tile_content = '''
<div class="col-md-6 col-lg-4 movie-tile text-center" data-trailer-youtube-id="{trailer_youtube_id}" data-toggle="modal" data-target="#trailer">
![]({poster_image_url})
<h2>{movie_title}</h2>
</div>
'''
def create_movie_tiles_content(movies):
# The HTML content for this section of the page
content = ''
for movie in movies:
# Extract the youtube ID from the url
youtube_id_match = re.search(
r'(?<=v=)[^&#]+', movie.trailer_url)
if youtube_id_match:
youtube_id_match = 'http://www.youtube.com/embed/' + youtube_id_match.group(0) + '?autoplay=1&html5=1';
else :
youtube_id_match = movie.trailer_url
trailer_youtube_id = youtube_id_match
# Append the tile for the movie with its content filled in
content += movie_tile_content.format(
movie_title=movie.title,
poster_image_url=movie.poster_image_url,
trailer_youtube_id=trailer_youtube_id
)
return content
def open_movies_page(movies):
# Create or overwrite the output file
output_file = codecs.open('fresh_tomatoes.html', 'w',encoding='utf-8')
# Replace the movie tiles placeholder generated content
rendered_content = main_page_content.format(
movie_tiles=create_movie_tiles_content(movies))
# Output the file
output_file.write(main_page_head + rendered_content)
output_file.close()
# open the output file in the browser (in a new tab, if possible)
url = os.path.abspath(output_file.name)
webbrowser.open('file://' + url, new=2)
media.py
import webbrowser
class Movie():
"""This class provides a way to store movie related information """
VALID_RATINGS = ["G", "PG", "PG-13", "R"]
def __init__(self, movie_title, movie_storyline, poster_image, trailer):
self.title = movie_title
self.storyline = movie_storyline
self.poster_image_url = poster_image
self.trailer_url = trailer
def show_trailer(self):
webbrowser.open(self.trailer_url)
相關文章
- 創業札記(三)- 向色情視訊網站學習創業網站
- 大笑三聲爆笑視訊笑話網站網站
- python 100題練習記錄(三)Python
- 新手練習:Python練習題目Python
- Python 練習題Python
- 慕課網Python入門練習題---Python
- python練習題解析Python
- python爬蟲練習--爬取虎牙主播原畫視訊Python爬蟲
- 知乎高贊!有沒有適合新手練習 Python 的做題類網站?Python網站
- Python基礎練習題Python
- 五、python的練習題Python
- Python函式練習題Python函式
- python相關練習題Python
- Python爬蟲訓練:爬取酷燃網視訊資料Python爬蟲
- 推薦4個國外線上視訊下載網站,YouTube、vimeo、推特視訊網站!網站
- 網站返回頂部功能視訊網站
- 技術類視訊教程網站網站
- 值得模範的視訊網站網站
- 使用 Python 下載 B 站視訊Python
- python練習冊-第0000題Python
- 直播原始碼網站,訊息圖示在收到訊息時展示訊息條數原始碼網站
- 完全免費的《從零開始學Swift》同步練習題解析視訊Swift
- 短視訊app開發,三種圖片並排展示的方式APP
- 14個程式碼展示網站網站
- 國外視訊網站再記錄網站
- Python-100 練習題 02Python
- python物件導向練習題01Python物件
- python練習冊-第0002題Python
- python006 列表練習題Python
- Python練習題篇(列表、字典、元祖)Python
- 9道python基礎練習題Python
- 網路資料庫練習題資料庫
- 常用Python學習網站Python學習網站
- Opencv第三章練習題答案OpenCV
- (視訊) 《快速建立網站》3.4 網站改版3分鐘搞定 - WordPress主題安裝和備份網站
- 19個練習黑客技術的線上網站黑客網站
- android學習視訊(實戰專案演練)Android
- Scala學習(三)練習