Created
December 1, 2022 03:02
-
-
Save andronex/6a4ee50aba9de11e07ab91fcdcc99eae to your computer and use it in GitHub Desktop.
Парсер цветов NCS с сайта https://colorscheme.ru/ncs-colors.html для MODX из Console с сохранением в свою кастомную таблицу
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$modx->addPackage('colorsProducts', MODX_CORE_PATH . 'components/colorsProducts/model/'); | |
for($idx=1;$idx <= 20;$idx++){ | |
$url = 'https://colorscheme.ru/ncs-colors-'.$idx.'.html'; | |
$options = array( | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HEADER => false, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17", | |
CURLOPT_AUTOREFERER => true, | |
CURLOPT_CONNECTTIMEOUT => 120, | |
CURLOPT_TIMEOUT => 120, | |
CURLOPT_MAXREDIRS => 10, | |
); | |
$ch = curl_init( $url ); | |
curl_setopt_array( $ch, $options ); | |
$content = curl_exec( $ch ); | |
curl_close( $ch ); | |
$doc = new DOMDocument(); | |
$doc->loadHTML($content); | |
$xpath = new DOMXpath($doc); | |
$elements = $xpath->query("//tbody[@id='default']/tr"); | |
foreach($elements as $tr){ | |
$idx_td = 0; | |
$ncs_rgb = []; | |
$ncs_hex = $ncs_name = ''; | |
foreach($tr->childNodes as $td){ | |
$is_continue = false; | |
$idx_td++; | |
if($idx_td == 2){ | |
$ncs_name = trim($td->nodeValue); | |
} | |
if($idx_td == 7){ | |
$ncs_hex = trim(str_replace('#', '', $td->nodeValue)); | |
} | |
if($idx_td == 8 || $idx_td == 9 || $idx_td == 10){ | |
$ncs_rgb[] = trim($td->nodeValue); | |
} | |
if(count($ncs_rgb) == 3){ | |
$ncs_rgb_text = implode(', ', $ncs_rgb); | |
} | |
} | |
if(!$modx->getObject('ColoringProducts', ['name' => $ncs_name])){ | |
$newColor = $modx->newObject('ColoringProducts'); | |
$newColor->set('name', $ncs_name); | |
$newColor->set('type', 'ncs'); | |
$newColor->set('rgb', $ncs_rgb_text); | |
$newColor->set('hex', $ncs_hex); | |
$newColor->set('price', 500); | |
$newColor->set('price_kraski', 900); | |
$newColor->set('price_gruntovki', 650); | |
$newColor->save(); | |
} | |
// print_r($ncs_rgb_text); | |
// print_r($ncs_hex); | |
// print_r($ncs_name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment