From 14fc8c4a6f075d5a217f1a0d2db373a249f657e1 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 2 May 2014 03:04:25 -0700 Subject: [PATCH] Example code for Episode 15 --- episode14/README.txt | 3 + episode14/coding101.py | 30 +++++----- episode15/.gitignore | 1 + episode15/README.txt | 3 + episode15/codingYouTube101.py | 104 ++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 15 deletions(-) create mode 100644 episode14/README.txt create mode 100644 episode15/.gitignore create mode 100644 episode15/README.txt create mode 100644 episode15/codingYouTube101.py diff --git a/episode14/README.txt b/episode14/README.txt new file mode 100644 index 0000000..6d1f9de --- /dev/null +++ b/episode14/README.txt @@ -0,0 +1,3 @@ +Example for TWiT Coding 101 - Episode 14 + +Displays a list of the Coding 101 episodes (from a file), upon selection of a specific episode displays its title, air date and description. \ No newline at end of file diff --git a/episode14/coding101.py b/episode14/coding101.py index c05e628..c5a9a5c 100644 --- a/episode14/coding101.py +++ b/episode14/coding101.py @@ -10,10 +10,10 @@ import textwrap # Print opening title -print "================================ CODING 101 ================================" +print '================================ CODING 101 ================================' # Open the file -episodeFile = open("coding101.txt", "r") +episodeFile = open('coding101.txt', 'r') # Read the lines into a list linesFromFile = episodeFile.readlines() # Close the file @@ -25,25 +25,25 @@ while run: # Print an empty line print # Print the episode list - print "Choose an episode:" + print 'Choose an episode:' print # Loop through the episodes list. count = 0 for line in linesFromFile: - # Increment the episode number. + # Increment the episode number count += 1 # Split the line using tab as the delimiter # The format is: DateEpisode TitleDescription - episode = line.split("\t") + episode = line.split('\t') # Print the episode number & title. # The episode number is converted into a str, and right justified - print "\t{0}. {1}".format(str(count).rjust(2), episode[1]) + print '\t{0}. {1}'.format(str(count).rjust(2), episode[1]) print # Ask for the episode number - choice = raw_input("Enter 0-" + str(count) + " (or ENTER to quit): ") + choice = raw_input('Enter 1-' + str(count) + ' (or ENTER to quit): ') # Did you press enter? if not choice: @@ -52,7 +52,7 @@ while run: # Validate the selected episode number. It must be a number... elif choice.isdigit(): - # Convert the selection to an integer. + # Convert the selection to an integer selection = int(choice) # It must also be between 1 and the total number of episodes @@ -60,14 +60,14 @@ while run: if 1 <= selection <= count: print print - print "============================================================================" - # Get and split the line for the episode, the list is zero-based. + print '============================================================================' + # Get and split the line for the episode, the list is zero-based episode = linesFromFile[selection - 1].split('\t') - # Print the episode, title - print "Episode #" + choice + ": \"" + episode[1] + '" aired on ' + episode[0] + # Print the episode title and aired date + print 'Episode #' + choice + ": \"" + episode[1] + '" aired on ' + episode[0] print - # Print the description, using textwrap to make it look pretty. + # Print the description, using textwrap to make it look pretty print textwrap.fill(episode[2], width=76) - print "============================================================================" + print '============================================================================' print - raw_input("Press ENTER to continue...") + raw_input('Press ENTER to continue...') diff --git a/episode15/.gitignore b/episode15/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/episode15/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/episode15/README.txt b/episode15/README.txt new file mode 100644 index 0000000..b942784 --- /dev/null +++ b/episode15/README.txt @@ -0,0 +1,3 @@ +Example for TWiT Coding 101 - Episode 15 + +Displays a list of the Coding 101 episodes (fetched from YouTube), upon selection of a specific episode displays its title, description and URL. \ No newline at end of file diff --git a/episode15/codingYouTube101.py b/episode15/codingYouTube101.py new file mode 100644 index 0000000..8ddb739 --- /dev/null +++ b/episode15/codingYouTube101.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python + +# Coding YouTube 101 +# +# Written by Erik C. Thauvin (erik@thauvin.net) +# http://erik.thauvin.net/ +# http://github.com/ethauvin/coding101/ +# May 1, 2014 + +# To warp long lines +import textwrap +# For making http requests +import urllib2 +# For decoding YouTube's API responses +import json + +# Your YouTube API key from https://developers.google.com/youtube/registering_an_application +youTubeApiKey = '' + +# Print opening title +print '============================= CODING YOUTUBE 101 =============================' +# Print an empty line +print + +# Ask for the API key if none is specified above +while not youTubeApiKey.isalnum(): + youTubeApiKey = raw_input('Enter your YouTube API Key: ') + # Did you press enter? + if not youTubeApiKey: + # Stop the program execution. Bye-Bye! + exit(0) + +# Search for Coding 101 videos using the channel ID and our API key +youTubeResponse = urllib2.urlopen( + 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCSxIcr2rZZcoU7rSGXaEQag&maxResults=20&order=date&key=' + youTubeApiKey) + +# Load the list of videos returned by YouTube +videos = json.load(youTubeResponse) + +# Declare the episodes list +episodes = [] + +# Loop through the videos to fill in the episodes list +for video in videos['items']: + # Ensure that it is a video + if video['id']['kind'] == 'youtube#video': + # Store the title, description and link of the video in our list, separated by tabs + # The format is: TitleDescriptionURL + episodes.append( + video['snippet']['title'] + '\t' + video['snippet']['description'] + '\t' + + 'http://youtube.com/watch?v=' + video['id']['videoId']) + +# Let's go. The program will execute until run = False +run = True +while run: + print + print 'Choose an episode:' + print + + # Loop through and print the episodes list. + count = 0 + for episode in episodes: + # Increment the loop count, same as: count = count + 1 + count += 1 + # Split the episode info using tab as the delimiter + episodeInfo = episode.split('\t') + # Print the loop count & title + # The count is converted into a str, and right justified + print ' {0}. {1}'.format(str(count).rjust(2), episodeInfo[0]) + + print + + # Ask for the episode number + choice = raw_input('Enter 1-' + str(count) + ' (or ENTER to quit): ') + + # Did you press enter? + if not choice: + # Stop the program execution. Bye-Bye! + run = False + + # Validate the selected episode number. It must be a number... + elif choice.isdigit(): + # Convert the selection to an integer + selection = int(choice) + + # It must also be between 1 and the total number of episodes + # Could be written as: selection >= 1 and selection <= count + if 1 <= selection <= count: + print + print + print '==============================================================================' + # Split the selected episode info using tab as the delimiter, the list is zero-based + episodeInfo = episodes[selection - 1].split('\t') + # Print the episode title + print episodeInfo[0] + print + # Print the description, using textwrap to make it look pretty + print textwrap.fill(episodeInfo[1], width=76) + print + # Print the URL + print episodeInfo[2] + print '==============================================================================' + print + raw_input('Press ENTER to continue...')