Example code for Episode 15

This commit is contained in:
Erik C. Thauvin 2014-05-02 03:04:25 -07:00
parent 9e72575265
commit 14fc8c4a6f
5 changed files with 126 additions and 15 deletions

3
episode14/README.txt Normal file
View file

@ -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.

View file

@ -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: Date<TAB>Episode Title<TAB>Description
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...')

1
episode15/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.idea

3
episode15/README.txt Normal file
View file

@ -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.

View file

@ -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: Title<TAB>Description<TAB>URL
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...')