Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node-modules/*
31 changes: 12 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
# README for Service Adapter Assignment on IF4050 2015

##Instruction
1. Fork this repository https://github.com/if-itb/if4050-2015-ServiceAdapter.git
2. Work on your fork --> commit --> push [as many as you want]
3. [When you are done OR the deadline] create pull request

Each participnats should indicate clearly the following data:
* NIM = 1[35|82]+XXYYY
* Name = XXXXXXX
* GithubID = YYYY
* NIM = 13512077
* Name = Khoirunnisa Afifah
* GithubID = afik

Requreiments:
* ....
* ....
*

* nodejs & npm

How to Deploy
1. .....
2. .....
3. .....
4.
1. Fork this repository
2. Install dependency : `npm install`
3. Run server : `node index.js`

How to Run
1. .....
2. .....
1. Make request at `localhost:3000/?ps=X&kode=Y&kelas=Z`
- X = study program code (ex.135)
- Y = course code (ex.IF4050)
- Z = class number (ex.01)

155 changes: 155 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
var express = require('express');
var https = require("https");
var cheerio = require("cheerio");

var app = express();

var base_url = "https://six.akademik.itb.ac.id/publik/";
var sem = 1;
var th = 2015;
var kur = 2013;
var regPesertaKelas = /([0-9]{8})(.*)/g;
var regAll = /(^(.*)$)\n.+Studi\s+:\s+(.*$)\n.+:\s(\d)\/(\d+)\n+.+:\s+(\w+)\s\/\s(.*),\s(\d).+\n.+:\s(\d+)\s\/\s(.*)(.|\n)*-\n.+=\s(.*)/m;
var regNama = /\s{3}(.*$)/gm;

var download = function (url, callback) {
https.get(url, function(res) {
var data = "";
res.on('data', function(chunk) {
data += chunk;
});
res.on("end", function() {
callback(data);
});
}).on("error", function() {
callback(null);
});
}

var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});

app.get('/', function (req, res) {
var ps = req.query.ps;
var kd = req.query.kode;
var kls = req.query.kelas;

if ((ps != null) && (kd != null) && (kls != null)) {
var url = base_url + "daftarkelas.php?ps=" + ps + "&semester=" + sem + "&tahun=" + th + "&th_kur=" + kur;

download(url, function(data) {
if (data) {
var $ = cheerio.load(data, {
ignoreWhitespace: false,
xmlMode: false,
lowerCaseTags: false});
var ada = false;
var class_links = [];
$("ol > li").each(function(i, e) {
var mk = $(e).text();
if (mk.indexOf(kd) > -1) {
ada = true;

$(e).find("li>a").each(function(j, elem) {
class_links[j] = $(elem).attr("href");
});
}
});

if (kls > class_links.length/2) {
ada = false;
console.log("kelas ga ada");
}

if (!ada) {

console.log("kelas tidak ketemu");
var err = {
error : "Tidak ditemukan kelas dengan kode " + kd
};
res.status(404).send(JSON.stringify(err));
} else {
class_url = base_url + class_links[(kls*2)-2];
console.log(class_url);

var arr = [];
var arr2 = [];
var jsonArr = { };

download(class_url, function(data) {
if (data) {
$ = cheerio.load(data);
data = $("pre").text();

arr = data.match(regAll);
arr2 = data.match(regPesertaKelas);

if (arr.length == 0 || arr2.length == 0) {
console.log("error downloading1");
var error = {
error : "Terjadi kesalahan pada server"
};
res.status(500).send(JSON.stringify(error));
} else {

jsonArr.fakultas = arr[1];
jsonArr.prodi = arr[3];
jsonArr.semester = arr[4];
jsonArr.tahun = "20" + arr[5];
jsonArr.kode = arr[6];
jsonArr.mata_kuliah = arr[7];
jsonArr.sks = arr[8];
jsonArr.kelas = arr[9];
jsonArr.dosen = arr[10];
jsonArr.jumlah_peserta = arr[12];

var peserta = [];

for (var i = 0; i< arr2.length; ++i) {
var item = arr2[i];
peserta.push ({
"nim" : item.split(/\s{3}/)[0],
"nama" : item.split(/\s{3}/)[1]
});
}

jsonArr.peserta = peserta;

res.status(200).send(JSON.stringify(jsonArr));
}
} else {
console.log("error downloading2");
var error = {
error : "Terjadi kesalahan pada server"
};
res.status(500).send(JSON.stringify(error));
}
});
}
} else {
console.log("error downloading1");
var error = {
error : "Terjadi kesalahan pada server"
};
res.status(500).send(JSON.stringify(error));
}
});
} else {
var error = {
error : "Request tidak sesuai format"
};
res.status(400).send(JSON.stringify(error));
}

});

app.use(function(err, req, res) {
console.error(err.stack);
var error = {
error : "Terjadi kesalahan pada server"
};
res.status(500).send(JSON.stringify(error));
});
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "six-adapter",
"version": "1.0.0",
"description": "adapter to get current semester's DPK at six.akademik.itb.ac.id",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "afik",
"license": "ISC",
"dependencies": {
"cheerio": "^0.19.0",
"express": "^4.13.3"
}
}