Skip to content
Snippets Groups Projects
Select Git revision
  • 8071e7322dfc6ece51c717f58d2814a1fea66443
  • master default protected
2 results

parse.php

Blame
  • parse.php 3.37 KiB
    <?php
    
    /**
     * PHP Version 7
     * Melakukan crawling dan parsing data berita.
     * @category Berita
     * @package  Komunitas-PMO
     * @author   Putu Arya Pradipta <13515017@std.stei.itb.ac.id>
     * @license  http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
     * @link     http://pear.php.net/package/komunitas-pmo
     */
    
    require 'dbConnect.php';
    require 'sendNotif.php';
    
    $url = 'http://pmo.itb.ac.id/feed/';
    // header('Content-Type: text/html; charset=utf-8');
    
    $rss = new DOMDocument('1.0', 'UTF-8');
    $rss->load($url);
    $feed = array();
    
    $rssLastBuild =  $rss->getElementsByTagName('lastBuildDate')->item(0)->nodeValue;
    // cek apakah last build lebih baru
    $lastBuild = file_get_contents("lastNewsUpdate.txt");
    if (strtotime($lastBuild) < strtotime($rssLastBuild)) {
        foreach ($rss->getElementsByTagName('item') as $node) {
            // ambil judul
            $judul = htmlspecialchars(
                $node->getElementsByTagName('title')->item(0)->nodeValue
            );
            
            // cek apakah judul ada di dalam database
            $sql = "SELECT * FROM `berita` WHERE `judul` like '%" . $judul . "%'";
            $stmt = $conn->query($sql);
            $numRows = $stmt->num_rows;
            $stmt->close();
            if ($numRows == 0) {
                $stmt2 = $conn->prepare(
                    "INSERT INTO berita(judul, tgl, konten, foto) VALUES (?,?,?,?)"
                );
                echo "<br> <h1> " . $judul . "</h1>";
                $tgl = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;
                $tgl = date("Y-m-d", strtotime($tgl));
                
                $konten = $node->getElementsByTagName('encoded')->item(0)->nodeValue;
                
                $dom = new DOMDocument();
    
                // suppress error
                libxml_use_internal_errors(true);
                $dom->loadHTML('<?xml encoding="utf-8" ?>' . $konten);
                libxml_clear_errors();
                
                $imgarray = $dom->getElementsByTagName('img');
                
                $allData = '';
                // ambil gambar pertama untuk thumbnail
                // $thumbnail = null;
                // if ($imgarray->length > 0) {
                //   $thumbnail = $imgarray->item(0)->getAttribute("src");
                //   print_r($thumbnail);
                // }
                $arrayimage = array();
                while ($imgarray->length > 0) {
                    $img = $imgarray->item(0);
                    // $img->setAttribute("style","display: block; width: 90%");
                    array_push($arrayimage, $img->getAttribute("src"));
                    // $allData .= $img->ownerDocument->saveHTML($img);
                    $img->parentNode->removeChild($img);
                }
                $arrayimage = json_encode($arrayimage);
                // $parray = $dom->getElementsByTagName('p');
                // while ($parray->length > 0) {
                //   $p = $parray->item(0);
                //   // $p->setAttribute("style","display: block; width: 90%");
                //   $allData .= $p->ownerDocument->saveHTML($p);
                //   $p->parentNode->removeChild($p);
                // }
                $allData .= $dom->saveHTML();
                print_r($allData);
                $stmt2->bind_param("ssss", $judul, $tgl, $allData, $arrayimage);
                $stmt2->execute();
                $stmt2->close();
            }
        }
        // update
        file_put_contents("lastNewsUpdate.txt", $rssLastBuild);
        \pmotraining\FirebaseNotif::sendNotif("Ada berita baru", "Klik untuk membuka", 1, 0);
    }
    // closing
    $conn->close();