Last active
August 30, 2017 06:19
-
-
Save zengyu714/458ac2c653aaf0c487782bc7c5cd30bd to your computer and use it in GitHub Desktop.
Udacity - Full Stack Web Developer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
from tqdm import tqdm | |
def check_profanity(filename='./notes.txt'): | |
bad_words = [] | |
with open(filename, 'r') as notes: | |
for line in notes: | |
# See whether read words well | |
for word in tqdm(line.split()): | |
# Check profanity | |
connection = urllib.urlopen("http://www.wdylike.appspot.com/?q=" + word) | |
result_web = connection.read() | |
connection.close() | |
if 'true' in result_web: | |
bad_words += word | |
print result_web | |
if len(bad_words): | |
print "Look at " + bad_words + "..." | |
else: | |
print "Everything is OK!" | |
check_profanity() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Codes from Udacity Courses - Progamming Foundations with Python | |
import webbrowser | |
import os | |
import re | |
# 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 trailerYouTubeId = $(this).attr('data-trailer-youtube-id') | |
var sourceUrl = 'http://www.youtube.com/embed/' + trailerYouTubeId + '?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-youtube-id="{trailer_youtube_id}" 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 youtube ID from the url | |
youtube_id_match = re.search(r'(?<=v=)[^&#]+', movie.trailer_youtube_url) | |
youtube_id_match = youtube_id_match or re.search(r'(?<=be/)[^&#]+', movie.trailer_youtube_url) | |
trailer_youtube_id = youtube_id_match.group(0) if youtube_id_match else None | |
# 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 = 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import webbrowser | |
class Movie(): | |
def __init__(self, movie_title, movie_storyline, poster_image_url, trailer_youtube_url): | |
self.title = movie_title | |
self.storyline = movie_storyline | |
self.poster_image_url = poster_image_url | |
self.trailer_youtube_url = trailer_youtube_url | |
def show_trailer(self): | |
webbrowser.open(self.trailer_youtube) | |
# test | |
# ---------------------------------------------------------------------------------------- | |
import fresh_tomatoes | |
dunkirk = Movie('Dunkirk', | |
'Allied soldiers from Belgium, the British Empire and France are surrounded by the German army and evacuated during a fierce battle in World War II.', | |
'https://images-na.ssl-images-amazon.com/images/M/MV5BN2YyZjQ0NTEtNzU5MS00NGZkLTg0MTEtYzJmMWY3MWRhZjM2XkEyXkFqcGdeQXVyMDA4NzMyOA@@._V1_SY1000_CR0,0,674,1000_AL_.jpg', | |
'https://www.youtube.com/watch?v=XRtZUkAR2u4') | |
shining = Movie('Shining', | |
'A family heads to an isolated hotel for the winter where an evil and spiritual presence influences the father into violence, while his psychic son sees horrific forebodings from the past and of the future.', | |
'https://images-na.ssl-images-amazon.com/images/M/MV5BZWFlYmY2MGEtZjVkYS00YzU4LTg0YjQtYzY1ZGE3NTA5NGQxXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_.jpg', | |
'https://www.youtube.com/watch?v=5Cb3ik6zP2I') | |
blue = Movie('Blue Is The Warmest Color', | |
'Adèle\'s life is changed when she meets Emma, a young woman with blue hair, who will allow her to discover desire and to assert herself as a woman and as an adult. In front of others, Adèle grows, seeks herself, loses herself, and ultimately finds herself through love and loss.', | |
'https://images-na.ssl-images-amazon.com/images/M/MV5BMmYwZmVkZGItMjMyOS00OTkxLTg0MDEtZTM2Yzk0ZWEyNTQzXkEyXkFqcGdeQXVyMzM4MjM0Nzg@._V1_SY1000_CR0,0,696,1000_AL_.jpg', | |
'https://www.youtube.com/watch?v=7PcgYoBUtlo') | |
star_war_4 = Movie('Star Wars: Episode IV - A New Hope', | |
'Luke Skywalker joins forces with a Jedi Knight, a cocky pilot, a Wookiee, and two droids to save the galaxy from the Empire\'s world-destroying battle-station, while also attempting to rescue Princess Leia from the evil Darth Vader.', | |
'https://images-na.ssl-images-amazon.com/images/M/MV5BYTUwNTdiMzMtNThmNS00ODUzLThlMDMtMTM5Y2JkNWJjOGQ2XkEyXkFqcGdeQXVyNzQ1ODk3MTQ@._V1_SY1000_CR0,0,664,1000_AL_.jpg', | |
'https://www.youtube.com/watch?v=nywPf1p-BBY') | |
cube = Movie('Cube', | |
'Six complete strangers of widely varying personality characteristics are involuntarily placed in an endless maze containing deadly traps.', | |
'https://images-na.ssl-images-amazon.com/images/M/MV5BZTIyZGM3NDItMTNmNS00Yzc4LTg2MzItOWY4MTE1NDlmZDIyXkEyXkFqcGdeQXVyMTAwMzUyOTc@._V1_.jpg', | |
'https://www.youtube.com/watch?v=YAWSkYqqkMA') | |
arrival = Movie('Arrival', | |
'When twelve mysterious spacecraft appear around the world, linguistics professor Louise Banks is tasked with interpreting the language of the apparent alien visitors.', | |
'https://images-na.ssl-images-amazon.com/images/M/MV5BMTExMzU0ODcxNDheQTJeQWpwZ15BbWU4MDE1OTI4MzAy._V1_SY1000_CR0,0,640,1000_AL_.jpg', | |
'https://www.youtube.com/watch?v=tFMo3UJ4B4g') | |
movies = [cube, dunkirk, blue, shining, arrival, star_war_4] | |
fresh_tomatoes.open_movies_page(movies) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment