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..aec4767 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,21 @@ # 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 + * NIM = 13512019 + * Name = Christ Angga Saputra + * GithubID = christangga -Each participnats should indicate clearly the following data: - * NIM = 1[35|82]+XXYYY - * Name = XXXXXXX - * GithubID = YYYY - -Requreiments: - * .... - * .... - * +Requirements: + * NodeJS How to Deploy - 1. ..... - 2. ..... - 3. ..... - 4. - + 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. ..... - 2. ..... - + 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` 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