Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
6.0 kB
1
Indexable
Never
<?php

namespace Crawler;

use Symfony\Component\DomCrawler\Crawler;
use Curl\Curl;

class Leermanga
{
    public $referer = 'https://leermanga.net';
    public $proxy = false;  
    public $curl;

    function __construct()
    {
        $this->curl = new Curl();
        $this->curl->setReferrer($this->referer);
    }

    function list($page)
    {

        $url = "https://leermanga.net/biblioteca?page=$page";

        $curl_respone = $this->curl($url);
        if ($curl_respone) {
            $crawler = new Crawler($curl_respone);

            $data = $crawler->filter('.page-item-detail .manga_biblioteca h3.h5 a')->each(function (Crawler $node) {
                return trim($node->attr('href'));
            });
        }
        //print_r($data);
        return $data;
    }

    function info($url)
    {
        $total_curl = 0;
        $curl_respone = $this->curl($url);
        // $is_curl = true;
        // print_r($curl_respone);
        exit;

        if ($curl_respone) {
            $crawler = new Crawler($curl_respone);
            $manga = $crawler->filter('.wrap');
            if ($manga->count() <= 0) {
                return die('Có lỗi khi lấy dữ liệu!');
            }

            $outerHtml = $manga->eq(0)->outerHtml();


            $data['name'] = $manga->filter('.post-title h1')->text();


            // $data['other_name'] = remove_html(explode_by("Alternative:\n</h5>\n</div>", '</div>', $outerHtml));
            // $data['other_name'] = $data['other_name'] === 'NA' ? NULL : $data['other_name'];

            $data['other_name'] = [];

            if($crawler->filter('meta[property="og:image"]')->count() > 0){
                $data['cover'] = $crawler->filter('meta[property="og:image"]')->eq(0)->attr('content');
            } else {
                $data['cover'] = $crawler->filter('.summary_image img')->attr('src');
            }

            $data['description'] = $crawler->filter('.c-page__content .description-summary p')->text();
            
            // $data['status'] = remove_html(explode_by("Status\n</h5>\n</div>", '</div>', $outerHtml));
            $data['status'] = slugGenerator($data['status']) == 'OnGoing' ? 'on-going' : 'on-going';

            $data['type'] = $crawler->filter('.post-title span.manga-title-info')->eq(0)->text();
            $data['type'] = slugGenerator($data['type']) == 'Manhwa' ? 'manga' : 'manhwa';

            $data['taxonomy']['source'][] = $url;

            $data['taxonomy']['authors'] =[];

            // $data['taxonomy']['authors'] = $manga->filter('.author-content a')->each(function (Crawler $node) {
            //     $authors =  trim($node->text());

            //     if($authors === 'Zinmanga'){
            //         return [];
            //     }

            //     return $authors;
            // });

            $data['taxonomy']['genres'] = $manga->filter('.post-content_item .summary-content a.tags_manga .fa-tag')->each(function (Crawler $node) {
                return $this->change_category($node->text());
            });
            
            $data['taxonomy']['translation'] = [];

            // $data['taxonomy']['translation'] = $manga->filter('.artist-content a')->each(function (Crawler $node) {
            //     return trim($node->text());
            // });

            $data['list_chapter'] = $crawler->filter('.listing-chapters_wrap ul li ul li a')->each(function (Crawler $node) {
                if($node->count() <= 0){
                    return null;
                }

                $node = $node->filter("a")->eq(0);
                
                $chapter['name'] = mb_convert_case(trim($node->text()), MB_CASE_TITLE, "UTF-8");

                if ((preg_match('#(chapter|chương|chap|capítulo)(.[\d.]+)#is', $chapter['name'], $name)  || preg_match('#([C]+)([\d.]+)#is', $chapter['name'], $name)) && !empty($name[2])) {
                    $chapter['name'] = 'Capítulo '. trim($name[2]);
                } elseif(is_numeric($chapter['name'])){
                    $chapter['name'] = 'Capítulo '. trim($chapter['name']);
                }

                $chapter['url'] = trim($node->attr('href'));

                return $chapter;
            });

            $data['list_chapter'] = array_reverse(array_filter($data['list_chapter']));
            return $data;
        }


    }

    function content($url)
    {
        $curl_respone = $this->curl($url);


        if ($curl_respone) {
            $crawler = new Crawler($curl_respone);
            $chapter['type'] = 'image';
            $chapter['content'] = $crawler->filter('.reading-content img')->each(function (Crawler $node) {
                //return $this->AddProxy(trim($node->attr('data-src')));
                return trim($node->attr('data-src'));
            });

            return $chapter;
        }

        return exit("lỗi");
    }

    // function AddProxy($url)
    // {
    //     $url = trim($url);
    //     $url = "https://thcomic.com/proxy/index.php?url=" . base64_encode($url) . "&proxysite=zinmanga";
    //     return $url;
    // }

    function change_category($key){
        $key = trim($key);

        return mb_convert_case(
            [
                'Webtoons' => 'Webtoon',
                'Truyện 18+' => null
            ]
            [$key] ?? $key, MB_CASE_TITLE, "UTF-8");
    }

    function curl($url){
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_REFERER, $this->referer);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        $resp = curl_exec($curl);
        curl_close($curl);

        return $resp;
    }
}