Skip to content

Instantly share code, notes, and snippets.

@orsenthil
Forked from bburky/coursera.sh
Last active June 30, 2016 17:32
Show Gist options
  • Save orsenthil/7600ef43c852841331c9d16e23fefd43 to your computer and use it in GitHub Desktop.
Save orsenthil/7600ef43c852841331c9d16e23fefd43 to your computer and use it in GitHub Desktop.
Automatically configure a VM download Coursera courses
#!/bin/bash
# Automatically configure a VM download Coursera courses.
# This script works as a user data file for use with a cloud VM.
# This script will resume downloading if the VM is restarted.
# This script works with Debian jessie (or possibly Ubuntu with systemd).
# You must enroll in each course and accept the Honor of Code of each course
# before you can download them.
# Usage:
# 1. Enter your Coursera login credentials in this script
# 2. Select the Coursera courses you want to download in this script by
# uncommenting them below, and add any additional courses you want to
# download that are not listed
# 3. Create a digital ocean droplet
# 4. Select Debian 8.5 x64
# 5. Choose a large enough droplet
# 6. Check "User Data" and paste this entire script into it
# 7. Select your ssh key under "Add your SSH keys"
# 8. Periodically ssh into the VM as root and check journalctl to see if it
# has finished downloading
# 9. Copy the downloaded courses from ~coursera/courses or upload them to
# your cloud backup location of choice
# If coursera-dl stops because of an error you can run the command
# `systemctl start coursera.service` to restart downloading or simply reboot
# the VM.
# You can add additional courses to download later by creating directories under
# ~/coursera/courses and restart downloading.
# Enter your Coursera username and password to download courses
USERNAME="user@example.com"
PASSWORD="hunter2"
# Uncomment or add additional courses you want to download
COURSES="\
4dimensions-004
ac-005
ai-001
aiplan-003
algebra-002
algo-003
algo-005
algo-009
algo2-003
algorithms-001
algs4partI-010
algs4partI-012
algs4partII-007
americandisease-001
analyze-003
androidapps101-002
androidcapstone-001
androidpart1-014
androidpart2-013
aofa-006
aoo-002
apcalc-002
apcalcpart1-001
apstat-002
art-001
artificialvision-001
assetpricing-003
astrotech-003
audio-002
automata-004
basicwriting-009
behaviorprinciples-002
bibrain-003
bigdata-004
bigdataanalytics-001
bigdataschool-001
bitcointech-002
braintargeted-001
career-002
cariesmanagement-003
cdt208-002
childliteracy-001
circaclock-002
circuits-005
citizenjournalism-001
clinicaltrials-001
comics-002
comnetworks-003
comparch-003
compartsaudio-001
compartsprocessing-001
compartsvisual-001
compfinance-009
compilers-003
compilers-004
compmethods-005
compneuro-003
compphoto-001
compvision-002
configuringworld-004
cooperativelearning-001
corethink-003
cplusplus4c-002
crypto-010
cs101-selfservice
datasci-002
db
dementiacare-002
democraticdev-002
design-006
designtech4learning-001
digitalstorytelling-004
dino101-006
dsalgo-001
dsp-006
earlychildhood-001
earth-002
eefun-001
eefunlab-001
einstein-001
emergenceoflife-002
energy-001
eqp-004
ethique-001
experiments-001
extrasolarplanets-001
fdubigdata-001
financialmarketszh-002
finiteelementmethods-002
fog-001
functionalanalysis-002
gametheory-003
gametheory-005
gametheory2-003
genomescience-002
ggp-003
ggp-004
globalization-002
globalorder-003
globalsportsbusiness-002
gtcomp-002
healthcaresafety-002
healthimaging-001
healthleadership-001
hetero-004
hetero-005
humanresearch-001
hwswinterface-002
ictinprimary-004
immigration-002
inforiskman-007
informationtheory-003
infosec-005
inquirytechniques-001
interorg-003
introbiology-004
intrologic-005
intrologic-009
intropsych-002
introstats-003
lcl-001
linalg-001
linearopt-002
linearprogramming-002
logic1-002
logic2-002
ltto-003
m2o2c2-001
machlearning-001
maps-003
marriageandmovies-002
mathematicalmethods-005
mathletics-003
mathphil-003
maththink-004
maththink-008
matrix-002
meatweeat-002
medievalspain-002
metaliteracy-001
ml-003
ml-005
mmds-002
mmds-003
mobilecloudsecurity-006
modelsystems-002
modelthinking-006
money-004
money2-004
moralities-002
musichistoryperforms-002
nanotech-001
networks-003
neuraldata-001
neuralnets-2012-001
neurobio-002
neuroethics-002
newtechtools-003
nlangp-001
nlp-002
nlpintro-001
ntuecom-001
ntufsc-004
ntumltwo-002
oldglobe-002
optimization-003
organalysis-003
os-001
pdstatistics-001
pgm-003
philsci-002
phys1-005
physics1-002
pkuic-002
pkupop-002
plantknows-003
poisonings-002
posacommunication-006
posaconcurrency-006
posasoftware-001
practicalethics-001
precalculus-004
prob-001
probability-002
proglang-002
proglang-003
programming1-002
programming2-001
psy-003
reactive-002
recsys-001
religionandtolerance-002
scicomp-003
scicontentsurvey-001
scientificcomp-007
sciframework-001
scistudentinquiry-001
sciwrite-2012-001
sdn1-001
security
sh-001
shaping1landscape-004
shaping2paths-004
sjtufirstaid-001
sjtuma153-001
sleep-005
sna-004
sna-2012-001
socialentrepeneur-002
spatialcomputing-001
staticsofobjects-002
statreasoning-004
stats1-003
succeedincollege-003
sustaininprac-002
techethics-001
tinkering-002
totalleadership-003
ucsfdiabetes-002
uffinancialplanning-002
understandingterror-003
unethicaldecision-002
ushealthcare-002
uvg-002
virology2-001
vlsicad-005
votingfairdiv-002
warandpeace-002
warhol-002
webapplications-003
webgl-001
writing2-002
"
apt-get update
apt-get upgrade -y
apt-get install -y python3 python3-pip
pip3 install coursera
useradd coursera -m -s /bin/bash
if [ -f /root/.ssh/authorized_keys ]; then
# Copy any authorized ssh keys added to the VM
mkdir ~coursera/.ssh/
cp /root/.ssh/authorized_keys ~coursera/.ssh/
fi
# Create the .netrc file
echo "machine coursera-dl login $USERNAME password $PASSWORD" > ~coursera/.netrc
chmod 600 ~coursera/.netrc
# Remove commented out courses and whitespace from courses list
COURSES=$(echo -n "$COURSES" | sed -e '/^#/ d' -e 's/\s//g')
mkdir ~coursera/courses
for course in $COURSES; do
mkdir ~coursera/courses/"$course"
done
chown -R coursera:coursera ~coursera
# Set up a service to download Coursera courses on every boot
cat >/etc/systemd/system/coursera.service <<EOF
[Unit]
Description=Download Coursera courses
Requires=network.target
[Service]
User=coursera
ExecStart=/bin/sh -c "cd ~/courses && coursera-dl --netrc --resume *"
[Install]
WantedBy=default.target
EOF
chmod 664 /etc/systemd/system/coursera.service
systemctl daemon-reload
systemctl enable coursera.service
systemctl start coursera.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment