Skip to content

Instantly share code, notes, and snippets.

@RayyanNafees
Last active March 23, 2024 20:16
Show Gist options
  • Save RayyanNafees/6c0c24d66e6e73e33b7a9c5416af898e to your computer and use it in GitHub Desktop.
Save RayyanNafees/6c0c24d66e6e73e33b7a9c5416af898e to your computer and use it in GitHub Desktop.
Scrapes B.Tech, M.Tech, BE available branches at ZHCET with Deno
import cheerio from 'https://esm.sh/cheerio'
let mtech_major
let sub_branches = {}
const branches = []
const _NonEmptyObject = (obj) => !!Object.keys(obj).length
for (const i of ['btech', 'be', 'mtech']) {
const $ = await fetch(`https://ctengg.amu.ac.in/web/crs_struct.php?prog=${i}`)
.then((r) => r.text())
.then(cheerio.load)
branches[i] = []
$('select:first option').each((_, el) => {
const branch = $(el).text()
if (branch === 'Select Branch' || branch === 'First Year B.Tech.') return
if (!branch.includes('(')) return branches[i].push(branch)
const [major, minor] = branch
.slice(0, -1)
.split('(')
.map((i) => i.trim())
if (!mtech_major || mtech_major !== major) {
mtech_major = major
_NonEmptyObject(sub_branches) && branches[i].push(sub_branches)
sub_branches = {}
}
if (!sub_branches[major]) sub_branches[major] = []
sub_branches[major].push(minor)
})
}
await Deno.writeTextFile('branches.json', JSON.stringify(branches))
function dlify(branches) {
const html = []
for (const course in branches) {
html.push(`<dt>${course}</dt>`)
for (const branch of branches[course]) {
if (typeof branch === 'string') html.push(`\t<dd>${branch}</dd>`)
if (typeof branch === 'object') {
html.push(`<dd>${dlify(branch)}</dd>`)
}
}
}
return `<dl>\n${html.join('\n')}</dl>`
}
const dlHtmlString = dlify(branches)
await Deno.writeTextFile('branch-list.html', dlHtmlString)
console.log('branches', branches)
console.log('dl', dlHtmlString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment