Skip to content

Instantly share code, notes, and snippets.

@nixmaniack
Forked from mrspeaker/README
Created November 30, 2011 20:07
Show Gist options
  • Save nixmaniack/1410592 to your computer and use it in GitHub Desktop.
Save nixmaniack/1410592 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: http://www.mrspeaker.net/2011/10/26/ai-class-playlists/). 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 YouTube_Playlist_Uploader.py script and it generates the playlist. It relies on the GData APIs so you need a dev key etc.
Any questions to [email protected]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
$(function(){
var unitName = "Unit 7",
allVids = [],
vids;
$(".playnav-item-title").each(function(i,s){
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]
}
})
.sort(function(a,b){
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' />")
.css("font-family","monospace")
.prependTo("body");
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 +
"<br/>";
$vidbox.append("<span>" + text + "</span>");
});
if(vids.length == 0) {
$vidbox.append("<span>No videos for the search string '" + unitName + "' found.</span>");
}
})
</script>
</head>
<body>
<!-- Replace this "section" tag with the contents of the YouTube video list -->
<section>
<h1>AI Class playlist extractor</h1>
<p>
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.
</p>
<h2>How to use</h2>
<ol>
<li>
Change the variable "unitName" at the top of the JavaScript to the
unit (or homework) you want to extract (eg, "Unit 7").
</li>
<li>
Replace the body of this HTML document with the HTML from the
<a href="http://www.youtube.com/user/knowitvideos#g/u">knowitvideos</a> list.
<ol>
<li>
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.
</li>
<li>
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).
</li>
<li>
In the inspector, choose "Copy as HTML" and paste the result into the body of this
HTML document (overwrite these stupid instructions)
</li>
</ol>
</li>
<li>
Refresh the page - it should find any matches on your search string. These can then
be pasted into the python script.
</li>
</ol>
</section>
</body>
</html>
import gdata.youtube
import gdata.youtube.service
import time
yt_service = gdata.youtube.service.YouTubeService()
yt_service.ssl = True
yt_service = gdata.youtube.service.YouTubeService()
yt_service.email = 'your@youtube_email.com'
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'
yt_service.ProgrammaticLogin()
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, gdata.youtube.YouTubePlaylistEntry):
print 'New playlist added:' + playlist.id.text
playlist_id = playlist.id.text.split("/")[-1]
playlist_uri = "http://gdata.youtube.com/feeds/api/playlists/" + playlist_id
for vid_id in ids:
print "Adding " + vid_id
playlistEntry = yt_service.AddPlaylistVideoEntryToPlaylist(playlist_uri, vid_id)
if isinstance(playlistEntry, gdata.youtube.YouTubePlaylistVideoEntry):
print 'Video added'
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment