Udemy courses are a great way to learn a new skill, and there are a lot of courses on Udemy available free of charge. Unfortunately, there’s not an easy way to add all of the freely available Udemy courses to your Udemy library. To fix that I decided to make a Node.js console application that could query the Udemy API for a list of free courses and iterate over that list using a browser automation library like Nightmare.js. To get started I first had to request an API key from Udemy.com.

Then I all I had to do was use Axios to make a get request to the Udemy API. Axios is a promise-based HTTP client that can run in the browser or in a Node.js script. To install it, all I had to do was a yarn command to install Axios. Then I simply had to create a module to get the list of courses. I didn’t want courses in any language other than English, but in order to make this script reusable I pulled out the variable that determines language into a module property so that anyone could easily change the language they are querying for. Unfortunately, the Udemy API only allows you to get 100 course objects at a time, so I couldn’t make just one get request. In order to get around this limitation, I added a recursive function call to the promise resolution callback. That way, when we get the first 100 items, we can immediately make another get request for the next page. You can see the module below:

After I got the courses I had to find a way to script out the steps necessary to enroll in each of the courses. Originally, I tried Nightwatch.js, a test runner that uses Selenium to automate browser functions. However, Nightwatch was a little bit error prone, and I didn’t like the necessity of assertions and Selenium in my initial script.¬†Nightmare.js, however, is a generic browser automation tool that uses Electron as its browser instead of a need for a web driver from Selenium. It also uses Promises instead of callback functions to handle asynchronous operations. All I had to do to get Nightmare installed was a yarn add command for nightmare. The initial install might take awhile, though, because nightmare depends on electron and it has to build a fresh copy. The module I created to run automation can be found below:

This would have been great if it worked, but I ran into some captchas that caused this particular script not to run. There’s also the problem of automation that relies on network resources being inherently error prone. I think it’s possible to make this particular script run the way I want it to, but it would require a lot more control over Udemy than I have currently. I’m going to open up my repository and pull requests are welcome, but I don’t know how much farther I’m going to be able to go with this script. Here’s the repository.

Side note on commander.js:

Commander is a NPM package that parses command line arguments, options, and switches for you. I really like it’s simplicity and the -h flag that is automatically built based on your configuration of the initial program variable. This allows you to delegate command line argument parsing to something that does it really well. I put a few switches into the index file of this script, but I didn’t implement them at all. I really like how easy commander makes it to do what would normally require a lot of custom code. I know that commander.js is based on Ruby’s commander gem, but I’m really glad that concepts like command line parsing have real value across programming languages.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: