Udacity机器学习入门 创建电影项目 P2

media.py

# -*- coding:utf-8 -*-

class Movie():
    """movie实体类"""
    def __init__(self, movie_name, movie_image_url, movie_play_url):
        """
            movie_name:电影名称
            movie_poster_url:电影海报链接
            movie_play_url:电影播放youtube地址
        """
        self.movie_name = movie_name
        self.movie_image_url = movie_image_url
        self.movie_play_url = movie_play_url
    def __str__(self):
        return '电影名:%s \n电影海报:%s \n播放地址: %s' % (self.movie_name, self.movie_image_url, self.movie_play_url)

fresh_tomatoes.py

# -*- coding:utf-8 -*-
import webbrowser
import os
import re
# page head contains js && styles
html_page_head = """
 
     
    Fresh Tomatoes! 
     
     
     
     
     
     
     
 
"""

# html page
html_page_content = """
 
 
   
     

     
    
{movie_container}
""" # single movie page template html_single_movie_content = """

{movie_name}

""" def get_youtube_id(movie): play_url = movie.movie_play_url # match movie id from youtube url id_match = re.search(r'(?<=v=)[^&#]+', play_url) id_match = id_match or re.search(r'(?<=be/)[^&#]+', play_url) youtube_id = id_match.group(0) if id_match else None return youtube_id def generate_movie_content(movies): content = '' for movie in movies: youtube_url = 'http://youtube.com/embed/' + get_youtube_id(movie) content += html_single_movie_content.format(movie_name=movie.movie_name, movie_image_url=movie.movie_image_url, youtube_src=youtube_url) return content def open_movies_page(movies): # create html page output_file = open('fresh_tomatoes.html', 'w') rendered_content = html_page_content.format(movie_container=generate_movie_content(movies)) output_file.write(html_page_head + rendered_content) output_file.close() url = os.path.abspath(output_file.name) webbrowser.open('file://' + url, new=2)

entertainment_center.py

# -*- coding:utf-8 -*-

import media
import fresh_tomatoes

# 初始化model数据
movieOne = media.Movie("Star Wars Battlefront II",
                       "https://i.ytimg.com/vi/WypeQtYC3Ms/hqdefault.jpg?sqp=-oaymwEXCNACELwBSFryq4qpAwkIARUAAIZCGAE=&rs=AOn4CLDO0EXdWiHVheduqbfizpQaN6nEmw",
                       "https://youtu.be/WypeQtYC3Ms")
movieTwo = media.Movie("Logan",
                       "https://i.ytimg.com/vi/p0Xlr3La_6I/hqdefault.jpg?sqp=-oaymwEXCNACELwBSFryq4qpAwkIARUAAIZCGAE=&rs=AOn4CLDtokXAqjWUDZmA3jClTR2eRY07gw",
                       "https://youtu.be/zW-wrIlpv7E")
movieThree = media.Movie("injustice god among us",
                         "https://i.ytimg.com/vi/KTJ9MOumqxk/hqdefault.jpg?sqp=-oaymwEXCNACELwBSFryq4qpAwkIARUAAIZCGAE=&rs=AOn4CLAOb8zzQPtUHirCwYgPYuoSu2vR1A",
                         "https://youtu.be/KTJ9MOumqxk")

# print(movieOne)
# print(movieTwo)
# print(movieThree)

# 将model数据加进数组
movies = [movieOne, movieTwo, movieThree]

# 执行页面生成函数
fresh_tomatoes.open_movies_page(movies)

你可能感兴趣的:(机器学习入门)