From cc7a9964aa6adb1b4436dad06b656ab366dc1baf Mon Sep 17 00:00:00 2001 From: christangga Date: Mon, 21 Sep 2015 14:02:39 +0700 Subject: [PATCH 1/3] final v1.0 --- .gitignore | 2 + README.md | 24 +++++------- index.js | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 21 ++++++++++ 4 files changed, 141 insertions(+), 15 deletions(-) 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..9303c34 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +npm-debug.log \ No newline at end of file diff --git a/README.md b/README.md index 9322a3b..2978242 100644 --- a/README.md +++ b/README.md @@ -6,22 +6,16 @@ 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 = 13512019 + * Name = Christ Angga Saputra + * GithubID = christangga -Requreiments: - * .... - * .... - * +Requirements: + * NodeJS How to Deploy - 1. ..... - 2. ..... - 3. ..... - 4. - + 1. npm update + How to Run - 1. ..... - 2. ..... - + 1. Open terminal, type 'npm start' + 2. Your web service is ready at http://localhost:8000 diff --git a/index.js b/index.js new file mode 100644 index 0000000..5147de2 --- /dev/null +++ b/index.js @@ -0,0 +1,109 @@ +var express = require('express'); +var fs = require('fs'); +var request = require('request'); +var cheerio = require('cheerio'); +var async = require('async'); +var app = express(); + +app.use(function(req, res, next) { + if (!req.query.ps || !req.query.kode || !req.query.kelas) { + res.type('application/json'); + res.status(400).json({'error': 'Request tidak sesuai format'}); + } else { + next(); + } +}); + +app.get('/', function(req, res) { + var base_url = 'https://six.akademik.itb.ac.id/publik/'; + + var ps = req.query.ps; + var code = req.query.kode.toUpperCase(); + var class_no = req.query.kelas; + + async.waterfall([ + function(callback) { + var classlist_url = 'daftarkelas.php?ps=' + ps + '&semester=1&tahun=2015&th_kur=2013'; + + request(base_url + classlist_url, function(error, response, html) { + var dpk_url = ''; + var err = null; + + if (!error) { + var $ = cheerio.load(html); + + $('ol li').filter(function(i, el) { + var text = $(this).text(); + return (text.substring(0, text.indexOf(' ')) == code); + }).children('ul').children('li').each(function(i, el) { + var text = $(this).children('a').first().html(); + if (text == class_no) { + dpk_url = $(this).children('a').first().attr('href'); + } + }); + + if (!dpk_url) { + err = 404; + } + } else { + err = 500; + } + + callback(err, base_url + dpk_url); + }); + }, + function(dpk_url, callback) { + request(dpk_url, function(error, response, html) { + var json = {}; + var err = null; + + if (!error) { + var $ = cheerio.load(html); + + var data = $('pre').text().replace('"','').split('\n'); + json['fakultas'] = data[0]; + json['prodi'] = data[1].substring(data[1].indexOf(':')+2); + json['semester'] = data[2].substring(data[2].indexOf(':')+2,data[2].indexOf('/')); + json['tahun'] = parseInt('20' + data[2].substring(data[2].indexOf('/')+1)); + json['kode'] = data[4].substring(data[4].indexOf(':')+2,data[4].lastIndexOf('/')-1); + json['mata_kuliah'] = data[4].substring(data[4].lastIndexOf('/')+2,data[4].indexOf(',')); + json['sks'] = data[4].substr(data[4].indexOf(',')+2,1); + json['kelas'] = data[5].substring(data[5].indexOf(':')+2,data[5].lastIndexOf('/')-1); + json['dosen'] = data[5].substring(data[5].lastIndexOf('/')+2); + json['jumlah_peserta'] = data[data.length-2].substring(data[data.length-2].indexOf('=')+2); + json['peserta'] = []; + + for (var i=10; i Date: Mon, 21 Sep 2015 14:03:38 +0700 Subject: [PATCH 2/3] update Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2978242..3f71da6 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Requirements: * NodeJS How to Deploy - 1. npm update + 1. Open terminal, type 'npm update' How to Run 1. Open terminal, type 'npm start' From 203f55b47de3410cf7209bf13562e66fc676bfbe Mon Sep 17 00:00:00 2001 From: christangga Date: Mon, 21 Sep 2015 14:11:20 +0700 Subject: [PATCH 3/3] update Readme 2 --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3f71da6..aec4767 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 = 13512019 * Name = Christ Angga Saputra * GithubID = christangga @@ -14,8 +8,14 @@ Requirements: * NodeJS How to Deploy - 1. Open terminal, type 'npm update' + 1. Install all dependencies with `npm update` + 2. Run the server with `npm start`. + 2. Your web service is ready at `http://localhost:8000` How to Run - 1. Open terminal, type 'npm start' - 2. Your web service is ready at http://localhost:8000 + 1. Available endpoint is: + - `[GET] /?ps=XXX&kode=YYYYYY&kelas=ZZ` + - `ps` is major code + - `kode` is course code + - `kelas` is class number code + - Return value will be in `json`