Skip to content

Instantly share code, notes, and snippets.

@dalisoft
Last active October 21, 2020 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalisoft/ce1cc46dd2effe0562c2cf8313b20939 to your computer and use it in GitHub Desktop.
Save dalisoft/ce1cc46dd2effe0562c2cf8313b20939 to your computer and use it in GitHub Desktop.
Download course script
const https = require("https");
// For using in browser, remove `require` and `fetch` mockup
function fetch(url) {
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
res.setEncoding("utf8");
let response = "";
res.on("data", (data) => {
response += data;
});
res.on("end", () =>
resolve({
text: () => response,
json: () => JSON.parse(response),
})
);
})
.on("error", reject);
});
}
class TokenFetch {
// Class constants
HTML_PARSE_REGEX = /\/s\/([a-z0-9]+)\/(.*)\/(.*)\.mp4/;
FREE_COURSE_URL = "https://coursehunter.net/course/rubyonrails";
constructor(cronTimer = 3000) {
this.token = null;
this.taskTimer = null;
this.CRON_TIMER = cronTimer;
return this;
}
// Fetch request and expect response as HTML Response
fetch = async () => {
return fetch(this.FREE_COURSE_URL).then((res) => res.text());
};
// Parse `token` from HTML
parse = (html) => {
const [_, token] = html.match(this.HTML_PARSE_REGEX);
return token;
};
// Fetch and parse, expects response as Token (type:String)
get = () => {
return this.fetch().then(this.parse);
};
// Set tokens to class property to cache and re-use later
set = (token) => {
this.token = token;
return token;
};
// Asks token, does `get` and `set` token together
ask = () => {
return this.get().then(this.set);
};
cachedAsk = () => {
if (this.token) {
return this.token;
}
return this.ask();
};
// Creates cron task (fake cron with `setInterval`) and returns timer
cronTask = () => {
return setInterval(this.ask, this.CRON_TIMER);
};
// Creates and attachs task into class property
registerTask = () => {
this.taskTimer = this.cronTask();
return () => {
clearInterval(this.taskTimer);
this.taskTimer = null;
};
};
// Initialize all logics
init = async () => {
await this.ask();
return this.registerTask();
};
}
class DownloadCourse {
URL_MATCH_REGEX = /\/s\/([a-z0-9]+)\//;
constructor(link) {
this.link = link;
this.tokenClass = new TokenFetch();
}
setToken = (token) => {
this.tokenClass.set(token);
return this;
};
parseUrl = async (url) => {
const userToken = await this.tokenClass.cachedAsk();
return url.replace(this.URL_MATCH_REGEX, `/s/${userToken}/`);
};
}
class ArgsParse {
constructor() {
this.args = {};
for (let i = 0, len = process.argv.length; i < len; i += 2) {
const key = process.argv[i];
const value = process.argv[i + 1];
if (key.startsWith("--")) {
this.args[key.substr(2)] = value || null;
}
}
return this;
}
list = () => {
return this.args;
};
get = (key) => {
return this.args[key];
};
set = (key, value) => {
this.args[key] = value;
return this;
};
}
async function main() {
const args = new ArgsParse();
const dl = new DownloadCourse();
const token = args.get("token");
const url = args.get("url");
const help = args.get("help");
if (help !== undefined) {
console.log("coursehunter.net - Token normalizer script");
console.log("Made by @dalisoft with love for community");
console.log("(c) 2020 @dalisoft");
console.log("Licensed under MIT-License", "\n");
console.log("Commands:");
console.log("url [Required] - Download URL of course");
console.log(
"Example: --url https://dls1.coursehunter.net/s/8f0f260a050519d80207882a14121419/udemy-sg-docker-kubernetes.zip"
);
console.log(
"Returns: New URL for download course with modified token",
"\n"
);
console.log("token [Optional] - Set your token");
console.log("Example: --token 8f0f260a050519d80207882a14121419");
console.log("`url` and `token` commands should be used together", "\n");
return;
}
if (token) {
dl.setToken(token);
}
try {
if (url) {
const changedUrl = await dl.parseUrl(url);
console.log(changedUrl);
}
} catch (err) {
console.log(err);
console.log("Failed to get url");
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment