Untitled

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

namespace Crawler;

use Symfony\Component\DomCrawler\Crawler;

class Vyvymanga extends CrawlerCore
{
    public $referer = "https://vyvymanga.com/";
    public $proxy = false;

    protected string $BASE_URL = "https://vyvymanga.net";

    function list($page = 1)
    {
        $URL = sprintf("https://vyvymanga.net/search?sort=updated_at&page=%s", $page);

        $data = $this->curl($URL);

        return (new Crawler($data))->filter(".comic-item a")->each(function (Crawler $node) {
            $url = $node->attr('href');
            if (strpos($url, 'http') !== false) {
                return $url;
            }

            return $this->BASE_URL . $url;
        });
    }

    function info($url)
    {
        $html = $this->curl($url);
        $crawler = new Crawler($html);

        if ($crawler->filter('h1')->count() <= 0) {
            return [];
        }

        $data['name'] = $crawler->filter("h1")->text();
        $data['cover'] = $crawler->filter('.img-manga')->attr('src');

        $title = explode_by("<h1 class=\"title\">", '<hr>', $html);
        if (!empty($title)) {
            $other_name = explode_by('<p>', '</p>', $title);
            if (!empty($other_name)) {
                $data['other_name'] = trim($other_name);
            }
        }


        $data['status'] = $crawler->filter('.text-ongoing')->count() > 0 ? 'on-going' : 'completed';

        $theloai = explode_by('<span class="pre-title">Genres</span>', '</p>', $html);

        $getText = (function (Crawler $node) {
            return trim($node->text());
        });

        $data['taxonomy']['genres'] = (new Crawler($theloai))->filter('a')->each($getText);

        if (in_array('Uncategorized', $data['taxonomy']['genres'])) {
            unset($data['taxonomy']['genres'][array_search('Uncategorized', $data['taxonomy']['genres'])]);
        }

        if (in_array('Web comic', $data['taxonomy']['genres'])) {
            $data['taxonomy']['genres'][array_search('Web comic', $data['taxonomy']['genres'])] = 'Webtoons';
        }

        foreach ($data['taxonomy']['genres'] as $theloai) {

            if($this->blacklist_category($theloai)){
                return null;
            }
        }

        $data['description'] = $crawler->filter(".summary .content")->text();

        $crawler->filter('.list .list-group-item *')->each(function (Crawler $crawler) {
            foreach ($crawler as $node) {
                $node->parentNode->removeChild($node);
            }
        });

        $data['list_chapter'] = $crawler->filter(".list .list-group-item")->each(function (Crawler $node) {
            return [
                'url' => $node->attr('href'),
                'name' => $node->attr('id') ? preg_replace('/chapter-/', 'Chapter ', $node->attr('id')) : $node->text(),
            ];
        });
        $data['list_chapter'] = array_reverse($data['list_chapter']);

        return $data;
    }

    function content($url)
    {
        $html = $this->curl($url);
        $crawler = new Crawler($html);


        $getIMG = (function (Crawler $node) {
            return $node->attr('data-src');
        });

        return [
            'type' => 'image',
            'content' => $crawler->filter("#carousel .carousel-item img.w-100")->each($getIMG)
        ];
    }

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

        $black_list = [
            'Smut',
            'Yaoi'
        ];

        if(in_array($key, $black_list)){
            return true;
        }

        return false;
    }


    function getTranslate($url, $bunny = "")
    {
        $url = htmlspecialchars_decode($url);

        if (!str_contains($url, '_x_tr_sl')) {
            $parse_url = parse_url($url);
            $host = str_replace('.', '-', $parse_url['host']);
            $path = $parse_url['path'] ?? "/";
            $query = isset($parse_url['query']) ? $parse_url['query'] . '&' : '';

            $url = sprintf(
                "https://%s.translate.goog%s?%s_x_tr_sl=en&_x_tr_tl=vi&_x_tr_hl=en&_x_tr_pto=op,wapp",
                $host,
                $path,
                $query
            );
        }

        if (!empty($bunny)) {
            $host = parse_url($url, PHP_URL_HOST);
            $url = str_replace($host, $bunny, $url);
        }

        return $url;
    }
}