Skip to content

Instantly share code, notes, and snippets.

@Infro
Created June 7, 2012 01:39
Show Gist options
  • Save Infro/2886021 to your computer and use it in GitHub Desktop.
Save Infro/2886021 to your computer and use it in GitHub Desktop.
Download Course RA's Class's Preview MP4 Videos
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Windows.Forms;
using System.Diagnostics;
using System.Web;
namespace ConsoleTestingStuff
{
public class DownloadPGMClass
{
String LecturePreviewUrl;
String LectureUrlParse;
Regex LectureUrlParseRegex;
String LecturePreviewVideoUrl;
String VideoUrlParse;
Regex VideoUrlParseRegex;
WebClient wc;
public static void main(string[] args)
{
if (args.Length != 1) return;
DownloadPGMClass x = new DownloadPGMClass();
x.InitializeMyStuff(args[0]);
x.StartDownload();
}
private void InitializeMyStuff(string ClassAbreviation)
{
LecturePreviewUrl = string.Format("https://class.coursera.org/{0}/lecture/preview", ClassAbreviation);
LectureUrlParse = string.Format(@"https://class\.coursera\.org/{0}/lecture/preview_view\?lecture_id=(\d+)", ClassAbreviation); ;
//"https://class\\.coursera\\.org/pgm/lecture/preview_view\\?lecture_id=(\\d+)";
LectureUrlParseRegex = new Regex(LectureUrlParse, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
LecturePreviewVideoUrl = string.Format("https://class.coursera.org/{0}/lecture/preview_view?lecture_id=", ClassAbreviation);
VideoUrlParse = "<source type=\"video/mp4\" src=\"(.*?)\"";
VideoUrlParseRegex = new Regex(VideoUrlParse, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
wc = new WebClient();
}
private void StartDownload()
{
String DefaultPath;
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
dlg.Description = "Select a folder";
if (dlg.ShowDialog() != DialogResult.OK) { MessageBox.Show("Download Canceled : No Folder Selected"); return; }
DefaultPath = dlg.SelectedPath;
}
//Why not just use a Dictionary? Because
List<KeyValuePair<string, string>> VideoList = new List<KeyValuePair<string, string>>();
String LectureLinks = wc.DownloadString(LecturePreviewUrl);
var myMatches = LectureUrlParseRegex.Matches(LectureLinks);
Debug.Assert(myMatches.Count >= 1);
foreach (Match m in myMatches)
{
String LectureID = m.Groups[1].Value;
String LectureHtml = wc.DownloadString(LecturePreviewVideoUrl + LectureID);
var LectureVideoMatch = VideoUrlParseRegex.Match(LectureHtml);
Debug.Assert(LectureVideoMatch.Success);
if (!LectureVideoMatch.Success)
continue;
String LectureVideoUrl = HttpUtility.UrlDecode(LectureVideoMatch.Groups[1].Value);
String FileName = LectureVideoUrl.Substring(LectureVideoUrl.LastIndexOf('/') + 1);
VideoList.Add(new KeyValuePair<string, string>(LectureVideoUrl, DefaultPath + "\\" + FileName));
}
foreach (KeyValuePair<string, string> VideoPathPair in VideoList)
{
wc.DownloadFile(VideoPathPair.Key, VideoPathPair.Value);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment