[LARAVEL] scrap jadwal sholat kemenag - fourslickz/notes GitHub Wiki

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Cookie\FileCookieJar;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use DOMDocument;
use DOMXPath;

class DownloadJadwalSholatKemenag extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'download:jadwal-sholat-kemenag';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Download Jadwal Sholat dari website kemenag';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        // get cookie from kemenag
        $kemenagCookie = $this->getKemenagCookie();

        // get jadwal sholat
        $getJadwalSholat = $this->getJadwalSholat();

        $this->info($getJadwalSholat);

    }

    private function getKemenagCookie()
    {
        // Kemenag main page
        $url = 'https://bimasislam.kemenag.go.id/jadwalshalat';

        $client = new Client();
        $cookieJar = new FileCookieJar(storage_path('cookies.txt'), true);
        $response = $client->get($url, ['cookies' => $cookieJar]);

        return $response;
    }

    private function getJadwalSholat()
    {
        $domain = 'bimasislam.kemenag.go.id';
        $url = 'https://bimasislam.kemenag.go.id/ajax/getShalatbln';

        $filePath = storage_path('cookies.txt');
        $fileContents = File::get($filePath);
        $cookies = json_decode($fileContents, true);

        $client = new Client();
        //$cookieJar = CookieJar::fromArray($cookies, $domain);
        $cookieJar = new FileCookieJar(storage_path('cookies.txt'), true);
        $data = [
            'x'     => 'c74d97b01eae257e44aa9d5bade97baf',  // prov_id
            'y'     => '077e29b11be80ab57e1a2ecabb7da330',  // kabko_id
            'bln'   => '3',
            'thn'   => '2024',
        ];

        $response = $client->post($url, [
            'cookies'       => $cookieJar,
            'form_params'   => $data,
        ]);

        $body = $response->getBody()->getContents();

        $this->info($body);
    }
    private function getProvince()
    {
        $url = 'https://bimasislam.kemenag.go.id/jadwalshalat';

        $client = new Client();
        $response = $client->get($url);
        $htmlString = (string) $response->getBody();

        libxml_use_internal_errors(true);

        $doc = new DOMDocument();
        $doc->loadHTML($htmlString);
        $xpath = new DOMXPath($doc);

        //$selectProvince = $xpath->query("//select[@id='search_prov']");
        $selectProvince = $xpath->query("//select");
        if ($selectProvince->length > 0) {
            foreach ($selectProvince as $select) {
                $options = $xpath->query('.//option', $select);
                foreach ($options as $option) {
                    $this->line('Value: ' . $option->getAttribute('value'));
                }
            }
        }

    }
}