Udacity机器学习入门项目2:创建电影网站

entertainment_center.py

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


# 创建对象
import fractions

from udacity_test import media, fresh_tomatoes

movieShawshank = media.Movie("The Shawshank Redemption",
                             "https://upload.wikimedia.org/wikipedia/zh/a/af/Shawshank_Redemption_ver2.jpg",
                             "https://www.youtube.com/watch?v=spEPXHfKiCs")
movieGump = media.Movie("Forrest Gump", "https://upload.wikimedia.org/wikipedia/zh/a/ad/Forrestgumppost.jpg",
                        "https://www.youtube.com/watch?v=bLvqoHBptjg")
movieTruman = media.Movie("The Truman Show", "https://upload.wikimedia.org/wikipedia/zh/c/cd/Trumanshow.jpg",
                          "https://www.youtube.com/watch?v=oY_FcMif5bQ")

# 打印对象属性
movieShawshank.toString()
movieGump.toString()
movieTruman.toString()

# 把对象组成集合
movies = [movieShawshank, movieGump, movieTruman]

# 调用页面工具方法
fresh_tomatoes.open_movies_page(movies)


media.py

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

class Movie():
    """movie实体类"""

    def __init__(self, title, poster_image_url, trailer_url):
        """
        Input: title => 电影名称
               poster_image_url => 电影海报链接
               trailer_url => 电影播放youtube地址
        """
        self.title = title
        self.poster_image_url = poster_image_url
        self.trailer_url = trailer_url

    def toString(self):
        """打印对象的属性值"""
        print(self.title + ">>>" + self.poster_image_url + ">>>" + self.trailer_url)

fresh_tomatoes.py

import webbrowser
import os
import re

# -*- coding:utf-8 -*-
# Styles and scripting for the page
main_page_head = '''
<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 trailerSrc = $(this).attr('data-trailer_src')
            var sourceUrl = trailerSrc + '?autoplay=1&html5=1';
            $("#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 = '''
<!DOCTYPE html>
<html lang="en">
  <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">
            <img src="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_src="{trailer_src}" data-toggle="modal" data-target="#trailer" >
    <img src="{poster_image_url}" width="220" height="342">
    <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 youku or youtube ID from the url
        trailer_url = movie.trailer_url
        youku_id_match = re.search(r'(id_)(\w+=*)(\.html)', trailer_url)
        youtube_id_match = re.search(r'(?<=v=)[^&#]+', trailer_url)
        youtube_id_match = youtube_id_match or re.search(r'(?<=be/)[^&#]+', trailer_url)
        trailer_youku_id = youku_id_match.group(2) if youku_id_match else None
        trailer_youtube_id = youtube_id_match.group(0) if youtube_id_match else None
        if trailer_youku_id != None:
          trailer_src = 'http://player.youku.com/embed/' + trailer_youku_id
        else:
          trailer_src = 'http://youtube.com/embed/'+ trailer_youtube_id

        # 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_src=trailer_src
        )
    return content

def open_movies_page(movies):
  # Create or overwrite the output file
  output_file = open('fresh_tomatoes.html', 'w')

  # Replace the placeholder for the movie tiles with the actual dynamically 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
  url = os.path.abspath(output_file.name)
  webbrowser.open('file://' + url, new=2) # open in a new tab, if possible


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
本教程为官方授权出品伴随着大数据时代的到来,作为发掘数据规律的重要手段,机器学习已经受到了越来越多的关注。而作为机器学习算法在大数据上的典型应用,推荐系统已成为各行业互联网公司营销体系中不可或缺的一部分,而且已经带来了真实可见的收益。目前,推荐系统和机器学习已经成为各大公司的发力重点,众多知名公司(如亚马逊、netflix、facebook、阿里巴巴、京东、腾讯、新浪、头条等)都在着眼于将蕴含在庞大数据中的宝藏发掘出来,懂机器学习算法的大数据工程师也成为了新时代最紧缺的人才。精心打造出了机器学习与推荐系统课程,将机器学习理论与推荐系统项目实战并重,对机器学习和推荐系统基础知识做了系统的梳理和阐述,并通过电影推荐网站的具体项目进行了实战演练,为有志于增加大数据项目经验、扩展机器学习发展方向的工程师提供更好的学习平台。本课程主要分为两部分,机器学习和推荐系统基础,与电影推荐系统项目实战。第一部分主要是机器学习和推荐系统基础理论的讲解,涉及到各种重要概念和基础算法,并对一些算法用Python做了实现;第二部分以电影网站作为业务应用场景,介绍推荐系统的开发实战。其中包括了如统计推荐、基于LFM的离线推荐、基于模型的实时推荐、基于内容的推荐等多个模块的代码实现,并与各种工具进行整合互接,构成完整的项目应用。通过理论和实际的紧密结合,可以使学员对推荐系统这一大数据应用有充分的认识和理解,在项目实战中对大数据的相关工具和知识做系统的回顾,并且可以掌握基本算法,入门机器学习这一前沿领域,为未来发展提供更多的选择,打开通向算法工程师的大门。谁适合学:1. 有一定的 Java、Scala 基础,希望了解大数据应用方向的编程人员2. 有 Java、Scala 开发经验,了解大数据相关知识,希望增加项目经验的开发人员3. 有较好的数学基础,希望学习机器学习和推荐系统相关算法的求职人员

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值