Парсер БК

Темы, не касающиеся фреймворка, но относящиеся к программированию в целом.
Ответить
Vladbara705
Сообщения: 10
Зарегистрирован: 2019.02.23, 22:55

Парсер БК

Сообщение Vladbara705 » 2019.04.05, 09:34

Набросал небольшой парсер.
Как его можно улучшить с точки зрения правильного ООП,solid и автотестов?

Код: Выделить всё

<?php
namespace app\components;
ini_set('max_execution_time', 3000);
class Events 
{
    private $parsing_games;
    private $gamesResult;
    private $eventsResult;
    function __construct(ParsingInterface $parsing_games) // берет названия вида матча с сайте
    {
        $this->parsing_games = $parsing_games;
    }
    public function parsing()
    {
        $gamesResult = $this->parsing_games->reciving_games();
        return $gamesResult;
    }
}
class Url {
    private $url;
    private $array;
    private $games;
    private $opts;
    private $context;
    public function recieveBody($url)
    {
        $opts = array('http'=>array('Accept' => "*/*",
                                    'Accept-Encoding' => "gzip, deflate, br",
                                    'Accept-Language' => "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
                                    'Cache-Control' => "no-cache",
                                    'Connection' => "keep-alive",
                                    'Host' => "www.olimp.bet",
                                    'Pragma' => "no-cache",
                                    'Referer' => "https://www.olimp.bet/service-worker.js",
                                    'method' => "GET",
                                    'header' => "User-Agent: Mozilla/6.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36\r\n"));
        $context = stream_context_create($opts);
        $array = file_get_contents($url,false,$context);
        $games = json_decode($array,true);
        return $games;
    }
}
interface ParsingInterface
{
    public function reciving_games();
}
class parsingOlimp implements ParsingInterface
{
    private $url;
    private $games;
    private $events;
    private $champ;
    private $gamesResult;
    private $champResult;
    private $eventsResult;
    private $kfResult;
    private $value;
    private $value2;
    private $value3;
    private $value4;
    function __construct(Url $url = null)
    {
        $url = new Url();
        $this->url = $url;
    }
    public function reciving_games() // берет названия вида матча с сайте
    {
        $games = $this->url->recieveBody('https://ruolimp.ru/apicps/line/sport/list?lang=0&from=0');
        foreach ($games as $key => $value)
        {
            $img = Img::img($value["name"]);
            $champ = $this->url->recieveBody('https://ruolimp.ru/apicps/line/sport/champs?lang=0&id='.$games[$key]["id"]);
            foreach ($champ as $key2 => $value2)
            {
                $events = $this->url->recieveBody('https://ruolimp.ru/apicps/line/champ/matches?lang=0&id='.$value2["id"]);
                foreach ($events as $key3 => $value3)
                {
                    if ($value3["outcomes"] == null)
                    {
                        continue 2;
                    }
                    $kfResult1 = array();
                    $eventsResult[] = ["keyChamp" => $value2["id"], "keyEvents" => $value3["matchInfo"]["id"], "nameEvents" => $value3["matchInfo"]["name"]];
                    foreach ($value3["outcomes"] as $key4 => $value4)
                    {
                        if ($value4["shortName"] === 'П1' or $value4["shortName"] === 'П2' or $value4["shortName"] === 'Х')
                        {
                            $kfResult1 = array_merge($kfResult1, array("keyEvents" => $value3["matchInfo"]["id"],$value4["shortName"] => $value4["value"]));
                        }
                    }
                    $kfResult[] = $kfResult1;
                }
                $champResult[] = ["keyGames" => $value2["sportId"],"keyChamp" => $value2["id"],"nameChamp" => $value2["name"]];
            }
            $gamesResult[] =["keyGames" => $value["id"], "nameGames" => $value["name"], "img" => $img, "champ" => $champResult, "events" => $eventsResult, "kf" => $kfResult];
            sleep(2);
        }
        return json_encode($gamesResult);
    }
}
class Img
{
    public function img($name)
    {
        switch ($name)
        {
            case 'Футбол':
                return ('img/Soccer-Ball-icon.png');
                break;
            case 'Хоккей':
                return ('img/no-translate-detected_318-112087.png');
                break;
            case 'Волейбол':
                return ('img/184940.png');
                break;
            case 'Водное поло':
                return ('img/184940.png');
                break;
            case 'Теннис':
                return ('img/no-translate-detected_318-143555.png');
                break;
            case 'Шахматы':
                return ('img/white-centaur.png');
                break;
            case 'Гольф':
                return ('img/33846.png');
                break;
            case 'Футзал':
                return ('img/soccer-ball-outlined-sportive-object_icon-icons.com_54090.png');
                break;
            case 'Пляжный футбол':
                return ('img/soccer-ball-outlined-sportive-object_icon-icons.com_54090.png');
                break;
            case 'Настольный теннис':
                return ('img/no-translate-detected_318-117825.png');
                break;
            case 'Австралийский футбол':
                return ('img/ball-of-american-football-outlined-sportive-symbol_icon-icons.com_54114.png');
                break;
            case 'Американский футбол':
                return ('img/ball-of-american-football-outlined-sportive-symbol_icon-icons.com_54114.png');
                break;
            case 'Регби':
                return ('img/ball-of-american-football-outlined-sportive-symbol_icon-icons.com_54114.png');
                break;
            case 'Бадминтон':
                return ('img/no-translate-detected_318-78190.png');
                break;
            case 'Бейсбол':
                return ('img/no-translate-detected_318-71430.png');
                break;
            case 'Крикет':
                return ('img/no-translate-detected_318-71430.png');
                break;
            case 'Бильярд':
                return ('img/46506.png');
                break;
            case 'Хоккей на траве':
                return ('img/depositphotos_53701335-stock-illustration-two-black-silhouettes-sticks-for.png');
                break;
            case 'Флорбол':
                return ('img/depositphotos_53701335-stock-illustration-two-black-silhouettes-sticks-for.png');
                break;
            case 'Формула 1':
                return ('img/310px-Formula_1_logo.png');
                break;
            case 'Баскетбол':
                return ('img/basket_ball_100x100_4farby_detska.png');
                break;
            case 'Хоккей с мячом':
                return ('img/72524.png');
                break;
            case 'Гандбол':
                return ('img/dc2e47484f389361b9803eb7d6f2.png');
                break;
            case 'Дартс':
                return ('img/IMG_5037.png');
                break;
            case 'Киберспорт':
                return ('img/0005303-chem-kibersport-otlichaetsya-ot-fentezi-sporta.png');
                break;
            case 'Борьба':
                return ('img/depositphotos_80503464-stock-illustration-freestyle-wrestling-retro.png');
                break;
            case 'Бокс':
                return ('img/depositphotos_43070683-stock-illustration-boxing-gloves.png');
                break;
        }
        return 'img/no-translate-detected_318-139902.png';
    }
} 

Ответить