diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a725465 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ \ No newline at end of file diff --git a/README.md b/README.md index 9322a3b..8439b8a 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..187060a --- /dev/null +++ b/composer.json @@ -0,0 +1,6 @@ +{ + "require": { + "slim/slim": "^2.6", + "mmerian/phpcrawl": "^0.8.3" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..787716c --- /dev/null +++ b/composer.lock @@ -0,0 +1,97 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "25351eac65c898337319a1d0aa1f13d5", + "packages": [ + { + "name": "mmerian/phpcrawl", + "version": "0.8.3", + "source": { + "type": "git", + "url": "https://github.com/mmerian/phpcrawl.git", + "reference": "d9c84beeaaa666b5910a6005db689a74cb6894b0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mmerian/phpcrawl/zipball/d9c84beeaaa666b5910a6005db689a74cb6894b0", + "reference": "d9c84beeaaa666b5910a6005db689a74cb6894b0", + "shasum": "" + }, + "type": "library", + "autoload": { + "classmap": [ + "libs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-v2" + ], + "authors": [ + { + "name": "Uwe Hunfeld", + "email": "phpcrawl@cuab.de" + } + ], + "description": "PHPCrawl is a webcrawler/webspider-library written in PHP. It supports filters, limiters, cookie-handling, robots.txt-handling, multiprocessing and much more.", + "time": "2015-01-31 11:00:11" + }, + { + "name": "slim/slim", + "version": "2.6.2", + "source": { + "type": "git", + "url": "https://github.com/slimphp/Slim.git", + "reference": "20a02782f76830b67ae56a5c08eb1f563c351a37" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/slimphp/Slim/zipball/20a02782f76830b67ae56a5c08eb1f563c351a37", + "reference": "20a02782f76830b67ae56a5c08eb1f563c351a37", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "suggest": { + "ext-mcrypt": "Required for HTTP cookie encryption" + }, + "type": "library", + "autoload": { + "psr-0": { + "Slim": "." + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Josh Lockhart", + "email": "info@joshlockhart.com", + "homepage": "http://www.joshlockhart.com/" + } + ], + "description": "Slim Framework, a PHP micro framework", + "homepage": "http://github.com/codeguy/Slim", + "keywords": [ + "microframework", + "rest", + "router" + ], + "time": "2015-03-08 18:41:17" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/crawler.php b/crawler.php new file mode 100644 index 0000000..fa26dbd --- /dev/null +++ b/crawler.php @@ -0,0 +1,157 @@ +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, + ]; + } +} +?> diff --git a/index.php b/index.php new file mode 100644 index 0000000..86a904e --- /dev/null +++ b/index.php @@ -0,0 +1,46 @@ +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(); \ No newline at end of file