Skip to content

Instantly share code, notes, and snippets.

@bburky
Last active June 30, 2016 17:30
Show Gist options
  • Save bburky/40317e6375b1262b1a1fc31072acf496 to your computer and use it in GitHub Desktop.
Save bburky/40317e6375b1262b1a1fc31072acf496 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="[email protected]"
PASSWORD="hunter2"
# Uncomment or add additional courses you want to download
COURSES="\
# algo-003
# algo-005
# algo-009
# algo2-003
# algs4partI-010
# algs4partII-007
# analyze-003
# audio-002
# cariesmanagement-003
# comparch-003
# compilers-003
# compilers-004
# cplusplus4c-002
# crypto-010
# eefun-001
# eqp-004
# experiments-001
# gametheory-003
# ggp-003
# hetero-004
# hwswinterface-002
# intrologic-005
# linearopt-002
# machlearning-001
# maththink-004
# matrix-002
# ml-003
# ml-005
# mmds-002
# modelthinking-006
# neuralnets-2012-001
# organalysis-003
# pgm-003
# proglang-002
# recsys-001
# scicomp-003
# sna-2012-001
# spatialcomputing-001
"
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