Skip to content

Instantly share code, notes, and snippets.

Created November 2, 2011 11:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mrspeaker/1333398 to your computer and use it in GitHub Desktop.
Save mrspeaker/1333398 to your computer and use it in GitHub Desktop.
AI Class playlist generator
These are the scripts I use to create the playlists for the AI Class (available here: It's not pretty, but it works. I put them up here due to popular demand.
The first stage is to grab the video list HTML from YouTube and extract/sort the videos. This is done with the Video_ID_scraper.html file (instructions there).
Next, paste the video info into the script and it generates the playlist. It relies on the GData APIs so you need a dev key etc.
Any questions to
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<script src="" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var unitName = "Unit 7",
allVids = [],
allVids.push({href:this.href, text:$(this).find("span").text()});
vids = allVids.filter(function(v){
return v.text.indexOf(unitName) > -1;
.map(function(v) {
return {
href: v.href,
text: v.text,
hash: v.text.slice(unitName.length).match(/([0-9]+[a-z]?)/g)[0]
var num1 = parseInt(a.hash, 10),
num2 = parseInt(b.hash, 10);
if(num1 === num2) {
var let1 = a.hash.slice((num1 + "").length) || "0",
let2 = b.hash.slice((num2 + "").length) || "0";
return let1 > let2 ? 1 : -1;
return num1 - num2;
// Print them out
var $vidbox = $("#vidbox").length > 0 ?
$("#vidbox") :
$("<div id='vidbox' />")
vids.forEach(function(v, i){
// Extract the vid id and convert to python format
var text = "\"" + v.href.slice(-11) + "\"" +
(i === vids.length - 1 ? "" : ",") +
" # " + v.text +
$vidbox.append("<span>" + text + "</span>");
if(vids.length == 0) {
$vidbox.append("<span>No videos for the search string '" + unitName + "' found.</span>");
<!-- Replace this "section" tag with the contents of the YouTube video list -->
<h1>AI Class playlist extractor</h1>
I use this jQuery/JavaScript script to extract a list of video Ids that can be pasted
into a python script which will create the youtube playlist. You have to paste some HTML
to scrape into this document (instructions below), then copy/paste the ID text to
the python script.
<h2>How to use</h2>
Change the variable "unitName" at the top of the JavaScript to the
unit (or homework) you want to extract (eg, "Unit 7").
Replace the body of this HTML document with the HTML from the
<a href="">knowitvideos</a> list.
To generate the list you need to click the "Load more" button until the videos
you are interested in are displayed in the scrolling window.
Then, you need to extract the HTML from the page: In Chrome you right-click
and "inspect element" - then find inspect the nodes until you find one that
has all of the videos as children elements (like, the "scrollbox-content" div).
In the inspector, choose "Copy as HTML" and paste the result into the body of this
HTML document (overwrite these stupid instructions)
Refresh the page - it should find any matches on your search string. These can then
be pasted into the python script.
import time
yt_service =
yt_service.ssl = True
yt_service = = ''
yt_service.password = 'password'
yt_service.client_id = 'Mr Speaker Playlist Importer'
yt_service.source = yt_service.client_id
yt_service.developer_key = 'Y0uR_GdaTa_D3V3l0pEr_K3y'
playlist = yt_service.AddPlaylist('AI Class Unit 7', 'AI Class unit 7 videos - including explanations')
ids = [
"pszEzBql4bw", # Unit 7 1 Introduction
"_VjyktjNMoM", # Unit 7 2 Propositional Logic
"eOp4UJl0ZIA", # Unit 7 2a Truth Tables
"4HWzU7RhfZE", # Unit 7 2b Answer
"ae_9TnTNPU0", # Unit 7 2c Question
"vGfOrh8ERXo", # Unit 7 2d Answer
"DDKhgqEWBps", # Unit 7 2e Question
"pxGxSt58kg0", # Unit 7 2f Answer
"nGZ2-EZnSh4", # Unit 7 2g Terminology
"WOYA5kQ_6gI", # Unit 7 2h Answer
"WQ7-B4H6-aE", # Unit 7 2i Propositional Logic Limitations
"hFzVZzMPy8Q", # Unit 7 3 First Order Logic
"TZ8iD-Rofk8", # Unit 7 3a Models
"Th_wM93aF94", # Unit 7 3b Syntax
"nkRYz5Omcr0", # Unit 7 3c Vacuum World
"JcQrAin3_V8", # Unit 7 3d Question
"vlbyPKhayiE", # Unit 7 3e Answer
"upyRNIh1LyE", # Unit 7 3f Question
"PY9d8qJ4BSY" # Unit 7 3g Answer
if isinstance(playlist,
print 'New playlist added:' +
playlist_id ="/")[-1]
playlist_uri = "" + playlist_id
for vid_id in ids:
print "Adding " + vid_id
playlistEntry = yt_service.AddPlaylistVideoEntryToPlaylist(playlist_uri, vid_id)
if isinstance(playlistEntry,
print 'Video added'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment