Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13512013 - Joshua Bezaleel Abednego #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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/
39 changes: 18 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
# 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 = 13512013
* Name = Joshua Bezaleel Abednego
* GithubID = joshuabezaleel

Each participnats should indicate clearly the following data:
* NIM = 1[35|82]+XXYYY
* Name = XXXXXXX
* GithubID = YYYY
##Requirements:
* NodeJS 4.0.0

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

How to Deploy
1. .....
2. .....
3. .....
4.
##How to Deploy
1. Go to the root folder of the project
2. Install all of the dependencies by executing the command `npm install` through command prompt.
3. Run the server by executing the command `node server.js`.
4. Server can be accessed through `localhost:80`.

How to Run
1. .....
2. .....
##How to Run
1. Send a request in a form of `GET /?parameters...`. The parameters are :
- `prodi` which indicates the study program code.
- `kode` which indicates the course code.
- `kelas` which indicates the class number.
2. Example of a complete request will be `GET /?prodi=135&kode=IF4050&kelas=01`.
3. It will return a value in a form of JSON containing the information of the course.
4. If there is error found, the error will be described in the JSON returned.

72 changes: 72 additions & 0 deletions REST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Service Adapter Daftar Peserta Kelas akademik.itb.ac.id

## Format Request

Method: GET

Parameter:

ps: Kode angka jurusan. Contoh: 135
kode: Kode mata kuliah. Contoh: IF4050
kelas: Kelas. Contoh: 01

Contoh request:

GET /?ps=135&kode=IF4050&kelas=01

## Format Response

Content-Type: application/json

Response saat berhasil:

Status Code: 200

Content:
{
"fakultas": "Sekolah Teknik Elektro dan Informatika",
"prodi": "Teknik Informatika",
"semester": "1",
"tahun": 2015,
"kode": "IF4050",
"mata_kuliah": "Pembangunan Perangkat Lunak Berorientasi Service",
"sks": "3",
"kelas": "01",
"dosen": "Adi Mulyanto",
"jumlah_peserta": "50",
"peserta": [
{
"nim": "13511018",
"nama": "Tito D Kesumo Siregar"
},
...
]
}

Response saat request tidak sesuai format:

Status Code: 400

Content:
{
"error": "Request tidak sesuai format"
}

Response saat kelas tidak ditemukan:

Status Code: 404

Content:
{
"error": "Tidak ditemukan kelas dengan kode IF4051"
}

Response saat terjadi kesalahan di server:

Status Code: 500

Content:
{
"error": "Terjadi kesalahan pada server"
}

12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name" : "IF4050-Service-Adapter",
"version" : "0.0.1",
"description" : "Scrape the DPK from six.akademik.itb.ac.id",
"main" : "server.js",
"author" : "Joshua Bezaleel Abednego",
"dependencies" : {
"express" : "latest",
"request" : "latest",
"cheerio" : "latest"
}
}
62 changes: 62 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var express = require('express');
var request = require('request');
var cheerio = require('cheerio');
var app = express();

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

var baseurl = 'https://six.akademik.itb.ac.id/publik/';
var prodiurl = 'daftarkelas.php?ps='+prodi+'&semester=1&tahun=2015&th_kur=2013';

request(baseurl + prodiurl, function(error, response, html){
if(!error && response.statusCode == 200){
var $ = cheerio.load(html);
$('li').each(function(){
if($(this).text().substring(0,6)===kode){
$(this).find('a').each(function(){
if($(this).text().substring(0,2)===kelas){
var href = $(this).attr('href');
request(baseurl+href, function(error,response,html){
$ = cheerio.load(html);
text = $('pre').text().split('\n');
var result = parsing(text);
res.status(200).json(result);
});
}
});
}
});
}

});
});

function parsing(text){
var result = {};
result.fakultas = text[0];
result.prodi = text[1].substr(17);
result.semester = text[2].substr(12,1);
result.tahun = "20" + text[2].substr(14);
result.kodematkul = text[4].substr(19,6);
result.namamatkul = text[4].substr(28).split(',')[0];
result.sks = text[4].substr(text[4].length-5,1);
result.kelas = text[5].substr(19,2);
result.dosen = text[5].substr(24);
result.jumpeserta = text[text.length-2].substr(16);
result.peserta = [];
//console.log(result.length-3);
for(var i = 10; i < text.length-3 ; i++){
result.peserta.push({
"nim" : text[i].substr(4,6),
"nama" : text[i].substr(15).trim()
});
}
return result;
};

app.listen('80');
console.log('Server is running');
exports = module.exports = app;