Guest User

fresh_tomatoes.py

a guest
Dec 15th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.82 KB | None | 0 0
  1. import webbrowser
  2. import os
  3. import re
  4.  
  5.  
  6. # Styles and scripting for the page
  7. main_page_head = '''
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11.    <meta charset="utf-8">
  12.    <title>My Movies Trailer Project</title>
  13.    <!-- Bootstrap 3 -->
  14.    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
  15.    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap-theme.min.css">
  16.    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  17.    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
  18.    <style type="text/css" media="screen">
  19.        body {
  20.            padding-top: 80px;
  21.        }
  22.        #trailer .modal-dialog {
  23.            margin-top: 200px;
  24.            width: 640px;
  25.            height: 480px;
  26.        }
  27.        .hanging-close {
  28.            position: absolute;
  29.            top: -12px;
  30.            right: -12px;
  31.            z-index: 9001;
  32.        }
  33.        #trailer-video {
  34.            width: 100%;
  35.            height: 100%;
  36.        }
  37.        .movie-tile {
  38.            margin-bottom: 20px;
  39.            padding-top: 20px;
  40.        }
  41.        .movie-tile:hover {
  42.            background-color: #EEE;
  43.            cursor: pointer;
  44.        }
  45.        .scale-media {
  46.            padding-bottom: 56.25%;
  47.            position: relative;
  48.        }
  49.        .scale-media iframe {
  50.            border: none;
  51.            height: 100%;
  52.            position: absolute;
  53.            width: 100%;
  54.            left: 0;
  55.            top: 0;
  56.            background-color: white;
  57.        }
  58.    </style>
  59.    <script type="text/javascript" charset="utf-8">
  60.        // Pause the video when the modal is closed
  61.        $(document).on('click', '.hanging-close, .modal-backdrop, .modal', function (event) {
  62.            // Remove the src so the player itself gets removed, as this is the only
  63.            // reliable way to ensure the video stops playing in IE
  64.            $("#trailer-video-container").empty();
  65.        });
  66.        // Start playing the video whenever the trailer modal is opened
  67.        $(document).on('click', '.movie-tile', function (event) {
  68.            var trailerYouTubeId = $(this).attr('data-trailer-youtube-id')
  69.            var sourceUrl = 'http://www.youtube.com/embed/' + trailerYouTubeId + '?autoplay=1&html5=1';
  70.            $("#trailer-video-container").empty().append($("<iframe></iframe>", {
  71.              'id': 'trailer-video',
  72.              'type': 'text-html',
  73.              'src': sourceUrl,
  74.              'frameborder': 0
  75.            }));
  76.        });
  77.        // Animate in the movies when the page loads
  78.        $(document).ready(function () {
  79.          $('.movie-tile').hide().first().show("fast", function showNext() {
  80.            $(this).next("div").show("fast", showNext);
  81.          });
  82.        });
  83.    </script>
  84. </head>
  85. '''
  86.  
  87.  
  88. # The main page layout and title bar
  89. main_page_content = '''
  90.  <body>
  91.    <!-- Trailer Video Modal -->
  92.    <div class="modal" id="trailer">
  93.      <div class="modal-dialog">
  94.        <div class="modal-content">
  95.          <a href="#" class="hanging-close" data-dismiss="modal" aria-hidden="true">
  96.            <img src="https://lh5.ggpht.com/v4-628SilF0HtHuHdu5EzxD7WRqOrrTIDi_MhEG6_qkNtUK5Wg7KPkofp_VJoF7RS2LhxwEFCO1ICHZlc-o_=s0#w=24&h=24"/>
  97.          </a>
  98.          <div class="scale-media" id="trailer-video-container">
  99.          </div>
  100.        </div>
  101.      </div>
  102.    </div>
  103.  
  104.    <!-- Main Page Content -->
  105.  
  106.    <div class="container">
  107.      <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  108.        <div class="container">
  109.          <div class="navbar-header">
  110.            <a class="navbar-brand" href="#">Udacity Movie Trailer Project</a>
  111.          </div>
  112.        </div>
  113.      </div>
  114.    </div>
  115.    <div class="container">
  116.      {movie_tiles}
  117.    </div>
  118.  </body>
  119. </html>
  120. '''
  121.  
  122.  
  123. # A single movie entry html template
  124. movie_tile_content = '''
  125. <div class="row">
  126.    <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">
  127.        <img src="{poster_image_url}" width="220" height="342">
  128.    </div>
  129.    <div class="col-md-6 col-lg-4 text-center">
  130.        <h3>{movie_title}</h3>
  131.        <h5>{movie_storyline}</h5>
  132.        <h6>Artists : {movie_artist}</h6>
  133.    </div>
  134. </div>
  135. '''
  136.  
  137.  
  138. def create_movie_tiles_content(movies):
  139.     # The HTML content for this section of the page
  140.     content = ''
  141.     for movie in movies:
  142.         # Extract the youtube ID from the url
  143.         youtube_id_match = re.search(
  144.             r'(?<=v=)[^&#]+', movie.trailer_youtube_url)
  145.         youtube_id_match = youtube_id_match or re.search(
  146.             r'(?<=be/)[^&#]+', movie.trailer_youtube_url)
  147.         trailer_youtube_id = (youtube_id_match.group(0) if youtube_id_match
  148.                               else None)
  149.  
  150.         # Append the tile for the movie with its content filled in
  151.         content += movie_tile_content.format(
  152.             movie_title=movie.title,
  153.             poster_image_url=movie.poster_image_url,
  154.             trailer_youtube_id=trailer_youtube_id,
  155.             movie_storyline = movie.storyline,
  156.             movie_artist = movie.artist
  157.         )
  158.     return content
  159.  
  160.  
  161. def open_movies_page(movies):
  162.     # Create or overwrite the output file
  163.     output_file = open('fresh_tomatoes.html', 'w')
  164.  
  165.     # Replace the movie tiles placeholder generated content
  166.     rendered_content = main_page_content.format(
  167.         movie_tiles=create_movie_tiles_content(movies))
  168.  
  169.     # Output the file
  170.     output_file.write(main_page_head + rendered_content)
  171.     output_file.close()
  172.  
  173.     # open the output file in the browser (in a new tab, if possible)
  174.     url = os.path.abspath(output_file.name)
  175.     webbrowser.open('file://' + url, new=2)
Advertisement
Add Comment
Please, Sign In to add comment