From 7c971e265408e0fe6b6f44361049ba7757e191f2 Mon Sep 17 00:00:00 2001 From: afik Date: Sat, 19 Sep 2015 16:21:05 +0700 Subject: [PATCH 1/6] initial commit --- .gitignore | 1 + index.js | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 15 ++++++ 3 files changed, 155 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0eda99b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node-modules/ diff --git a/index.js b/index.js new file mode 100644 index 0000000..6758702 --- /dev/null +++ b/index.js @@ -0,0 +1,139 @@ +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.error("kelas ga ketemu"); + res.status(404).send("Tidak ditemukan kelas dengan kode : " + kd); + } 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.error("error get dpk"); + } else { + + jsonArr.fakultas = arr[1]; + jsonArr.prodi = arr[3]; + jsonArr.semester = arr[4]; + jsonArr.tahun = 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; + + // var temp1 = ""; + // for (var i = 1; i< arr.length; ++i) { + // temp1 += i + ": " + arr[i] + "
"; + // } + + res.status(200).send(JSON.stringify(jsonArr)); + } + } else { + console.log("error downloading2"); + } + }); + } + } else { + console.error("error downloading1"); + } + }); + } else { + res.status(400).send("Request tidak sesuai format"); + } + +}); + +app.use(function(err, req, res, next) { + console.error(err.stack); + res.status(500).send('Terjadi kesalahan pada server'); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..ee18b0b --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "six-adapter", + "version": "1.0.0", + "description": "", + "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" + } +} From 30ceeb7f3b259b337e284dfc852bad5ddf507a5c Mon Sep 17 00:00:00 2001 From: afik Date: Sat, 19 Sep 2015 16:26:37 +0700 Subject: [PATCH 2/6] edit package.json --- .gitignore | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 0eda99b..ffaaf1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -node-modules/ +node-modules/* diff --git a/package.json b/package.json index ee18b0b..8a85b3c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "six-adapter", "version": "1.0.0", - "description": "", + "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" From 5f139a3a590c543096763a861ff556f1fdc77077 Mon Sep 17 00:00:00 2001 From: afik Date: Sat, 19 Sep 2015 16:33:09 +0700 Subject: [PATCH 3/6] edit Readme --- README.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9322a3b..388c166 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,13 @@ 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. ..... @@ -22,6 +20,10 @@ How to Deploy 4. How to Run - 1. ..... - 2. ..... + 1. npm install + 2. run node index.js + 3. call endpoint at `host/?ps=X&kode=Y&kelas=Z` + X = kode program studi (ex.135) + Y = kode mata kuliah (ex.IF4050) + Z = no kelas (ex.01) From d460ddb2fee1a6ed52e2fa6db4210b763545a7c2 Mon Sep 17 00:00:00 2001 From: afik Date: Sat, 19 Sep 2015 21:06:10 +0700 Subject: [PATCH 4/6] add error --- index.js | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 6758702..593f905 100644 --- a/index.js +++ b/index.js @@ -29,8 +29,7 @@ var download = function (url, callback) { 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); + console.log('App listening at http://%s:%s', host, port); }); app.get('/', function (req, res) { @@ -66,8 +65,10 @@ app.get('/', function (req, res) { } if (!ada) { - console.error("kelas ga ketemu"); - res.status(404).send("Tidak ditemukan kelas dengan kode : " + kd); + + console.log("kelas tidak ketemu"); + + res.status(404).send("Tidak ditemukan kelas dengan kode " + kd); } else { class_url = base_url + class_links[(kls*2)-2]; console.log(class_url); @@ -85,7 +86,10 @@ app.get('/', function (req, res) { arr2 = data.match(regPesertaKelas); if (arr.length == 0 || arr2.length == 0) { - console.error("error get dpk"); + console.log("error downloading1"); + var err = new Error(); + err.status = 500; + throw err; } else { jsonArr.fakultas = arr[1]; @@ -111,29 +115,36 @@ app.get('/', function (req, res) { jsonArr.peserta = peserta; - // var temp1 = ""; - // for (var i = 1; i< arr.length; ++i) { - // temp1 += i + ": " + arr[i] + "
"; - // } - res.status(200).send(JSON.stringify(jsonArr)); } } else { console.log("error downloading2"); + var err = new Error(); + err.status = 500; + throw err; } }); } } else { - console.error("error downloading1"); + console.log("error downloading1"); + var err = new Error(); + err.status = 500; + throw err; } }); } else { - res.status(400).send("Request tidak sesuai format"); + var err = new Error(); + err.status = 400; + throw err; } }); -app.use(function(err, req, res, next) { - console.error(err.stack); - res.status(500).send('Terjadi kesalahan pada server'); +app.use(function(err, req, res) { + if (err.status == 400) { + res.status(400).send("Request tidak sesuai format"); + } else { + console.error(err.stack); + res.status(500).send('Terjadi kesalahan pada server'); + } }); \ No newline at end of file From b76082bb894eeca4f15209a1ba7d916dca3a076d Mon Sep 17 00:00:00 2001 From: afik Date: Sat, 19 Sep 2015 23:19:08 +0700 Subject: [PATCH 5/6] edit tahun --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 593f905..4cdd4d2 100644 --- a/index.js +++ b/index.js @@ -95,7 +95,7 @@ app.get('/', function (req, res) { jsonArr.fakultas = arr[1]; jsonArr.prodi = arr[3]; jsonArr.semester = arr[4]; - jsonArr.tahun = arr[5]; + jsonArr.tahun = "20" + arr[5]; jsonArr.kode = arr[6]; jsonArr.mata_kuliah = arr[7]; jsonArr.sks = arr[8]; From db42b7e80a143e9e392af81966cfdae12e351e70 Mon Sep 17 00:00:00 2001 From: afik Date: Sun, 20 Sep 2015 14:21:24 +0700 Subject: [PATCH 6/6] edit readme & error response --- README.md | 23 +++++++---------------- index.js | 47 ++++++++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 388c166..f0fe8b5 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,5 @@ # 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 = 13512077 * Name = Khoirunnisa Afifah * GithubID = afik @@ -14,16 +8,13 @@ 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. npm install - 2. run node index.js - 3. call endpoint at `host/?ps=X&kode=Y&kelas=Z` - X = kode program studi (ex.135) - Y = kode mata kuliah (ex.IF4050) - Z = no kelas (ex.01) + 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) diff --git a/index.js b/index.js index 4cdd4d2..971e25c 100644 --- a/index.js +++ b/index.js @@ -67,8 +67,10 @@ app.get('/', function (req, res) { if (!ada) { console.log("kelas tidak ketemu"); - - res.status(404).send("Tidak ditemukan kelas dengan kode " + kd); + 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); @@ -87,9 +89,10 @@ app.get('/', function (req, res) { if (arr.length == 0 || arr2.length == 0) { console.log("error downloading1"); - var err = new Error(); - err.status = 500; - throw err; + var error = { + error : "Terjadi kesalahan pada server" + }; + res.status(500).send(JSON.stringify(error)); } else { jsonArr.fakultas = arr[1]; @@ -119,32 +122,34 @@ app.get('/', function (req, res) { } } else { console.log("error downloading2"); - var err = new Error(); - err.status = 500; - throw err; - } + var error = { + error : "Terjadi kesalahan pada server" + }; + res.status(500).send(JSON.stringify(error)); + } }); } } else { console.log("error downloading1"); - var err = new Error(); - err.status = 500; - throw err; + var error = { + error : "Terjadi kesalahan pada server" + }; + res.status(500).send(JSON.stringify(error)); } }); } else { - var err = new Error(); - err.status = 400; - throw err; + var error = { + error : "Request tidak sesuai format" + }; + res.status(400).send(JSON.stringify(error)); } }); app.use(function(err, req, res) { - if (err.status == 400) { - res.status(400).send("Request tidak sesuai format"); - } else { - console.error(err.stack); - res.status(500).send('Terjadi kesalahan pada server'); - } + console.error(err.stack); + var error = { + error : "Terjadi kesalahan pada server" + }; + res.status(500).send(JSON.stringify(error)); }); \ No newline at end of file