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

13512084 - Choirunnisa Fatima #36

Open
wants to merge 3 commits 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 @@
vendor/
26 changes: 11 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@
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
Each participants should indicate clearly the following data:
* NIM = 13512084
* Name = Choirunnisa Fatima
* GithubID = ichakid

Requreiments:
* ....
* ....
*
Requirements:
* Linux environment
* PHP >= 5.3.0
* Composer dependency manager

How to Deploy
1. .....
2. .....
3. .....
4.
1. Execute `composer install`

How to Run
1. .....
2. .....

1. Execute `sudo php -S 127.0.0.1:80` on this directory
2. Execute `GET` request to `http://127.0.0.1:80/?ps=135&kode=IF4050&kelas=01`
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"slim/slim": "^2.6",
"mmerian/phpcrawl": "^0.8.3"
}
}
97 changes: 97 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

157 changes: 157 additions & 0 deletions crawler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
require 'vendor/autoload.php';

// It may take a while to crawl a site ...
set_time_limit(10000);

// Extend PHPCrawler class and override the handleDocumentInfo()-method
class MyCrawler extends PHPCrawler {
public $kode;
public $kelas;
public $foundDoc = false;

function handleDocumentInfo($DocInfo) {
if (($DocInfo->received == true)
&& ($DocInfo->content_type == "text/html")){

// Check if we are already in the "leaf"
if (count($DocInfo->links_found) == 0) {
$url_parts = parse_url($DocInfo->url);
parse_str($url_parts['query'], $query);

if ($query['p']) {
if (checking($DocInfo->content, $this->kode,
$this->kelas)){
$this->foundDoc = $DocInfo->content;
return -1;
}
}
}
}
// flush();
}
}

//Check if the $doc contains $kode and $kelas
function checking($doc, $kode, $kelas) {
$separator = PHP_EOL;
$found = false;
$line = strtok($doc, $separator);
$line = strtok($separator);
$line = strtok($separator);
$line = strtok($separator);
var_dump($line);
if(strripos($line, $kode, 17) !== false){
$found = true;
}
$line = strtok($separator);
if(strripos($line, $kelas, 17) === false){
$found = false;
}
return $found;
}

class Finder {
public $ps;
public $kode;
public $kelas;
public $tahun = "2015";
public $semester = "1";
public $th_kur = "2013";
private $doc;

public function validate() {
return (!empty($this->ps) && !empty($this->kode) && !empty($this->kelas));
}

public function find() {
$crawler = new MyCrawler();

// Set paramater
$crawler->kode = $this->kode;
$crawler->kelas = $this->kelas;

// URL to crawl
$url = "https://six.akademik.itb.ac.id/publik/daftarkelas.php?ps="
. $this->ps . "&semester=" . $this->semester . "&tahun="
. $this->tahun . "&th_kur=" . $this->th_kur;
$crawler->setURL($url);

// Only receive content of files with content-type "text/html"
$crawler->addContentTypeReceiveRule("#text/html#");

// Ignore links to pictures, dont even request pictures
$crawler->addURLFilterRule("#\.(jpg|jpeg|gif|png)$# i");

// Don't let it back to the main page
$crawler->addURLFilterRule("#displayprodikelas.php# i");

// Thats enough, now here we go
$crawler->go();

if ($crawler->foundDoc !== false){
$this->doc = $crawler->foundDoc;
return true;
}
return false;
}

//Parse doc into array
public function parse() {
$separator = PHP_EOL;

$line = strtok($this->doc, $separator);
$fakultas = substr($line, 5);

$line = strtok($separator);
$prodi = preg_split('/\s:\s/', $line, 2)[1];

$line = strtok($separator);
$line = strtok($separator);
$mata_kuliah = preg_split('/\s:\s/', $line, 2)[1];

$matkul = array();
$splits = preg_split('#\s/\s#', $mata_kuliah, 2);
$matkul['kode'] = $splits[0];
preg_match('#(.*),\s(\d+)\sSKS#', $splits[1], $splits);
$matkul['sks'] = $splits[2];
$matkul['nama'] = $splits[1];

$line = strtok($separator);
$dosen = preg_split('/\s+/', $line, 6)[5];

$line = strtok($separator);
$line = strtok($separator);
$line = strtok($separator);
$line = strtok($separator);

$peserta = array();
while (($line !== false) && (substr($line, 0, 1) !== "-")) {
$mhs = array();
preg_match('#\d+\s+(\d+)\s+(.*)#', $line, $mhs);
$peserta[] = [
"nim" => $mhs[1],
"nama" => $mhs[2]
];
$line = strtok($separator);
}

$line = strtok($separator);
$jumlah_peserta = preg_split('/\s+=\s+/', $line)[1];

return [
"fakultas" => $fakultas,
"prodi" => $prodi,
"semester" => $this->semester,
"tahun" => $this->tahun,
"kode" => $matkul['kode'],
"mata_kuliah" => $matkul['nama'],
"sks" => $matkul['sks'],
"kelas" => $this->kelas,
"dosen" => $dosen,
"jumlah_peserta" => $jumlah_peserta,
"peserta" => $peserta,
];
}
}
?>
46 changes: 46 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
require 'vendor/autoload.php';
include("crawler.php");

// create new Slim instance
$app = new \Slim\Slim();
header("Content-Type: application/json");

// add route
$app->get("/", function () {
$app = \Slim\Slim::getInstance();
$finder = new Finder();
$finder->ps = $app->request()->get('ps');
$finder->kode = $app->request()->get('kode');
$finder->kelas = $app->request()->get('kelas');

if (!$finder->validate()){//wrong request format
$status = 400;
$body = [
"error" => "Request tidak sesuai format"
];
} else {//success request
if ($finder->find()) {
$status = 200;
try{
$body = $finder->parse();
} catch (Exception $e) {
$status = 500;
$body = [
"error" => "Terjadi kesalahan pada server"
];
}
} else {
$status = 404;
$body = [
"error" => "Tidak ditemukan kelas dengan kode " . $finder->kode
];
}
}

$app->response()->status($status);
echo json_encode($body);
});

// run the Slim app
$app->run();