/** * Test initialization and helpers. * * @author Petr Bugyík * @package Grido\Tests */ if (@(!(include __DIR__ . '/../vendor/autoload.php'))) { echo 'Install Nette Tester using `composer update --dev`'; exit(1); } // configure environment Tester\Environment::setup(); date_default_timezone_set('Europe/Prague'); Tracy\Debugger::$maxDepth = 5; Tracy\Debugger::$maxLen = 500; // create temporary directory define('TEMP_DIR', __DIR__ . '/tmp/' . getmypid()); @mkdir(dirname(TEMP_DIR)); // @ - directory may already exist Tester\Helpers::purge(TEMP_DIR); ini_set('session.save_path', TEMP_DIR); function id($val) { return $val; } function before(\Closure $function = NULL) { static $val; if (!func_num_args()) { return $val ? $val() : NULL;
public function actionPocasi($id, $mesto, $rec = false) { // api.plbot.lury.cz/pocasi/dnes?kraj=kraj if ($mesto === NULL) { $mesto = "praha"; } else { $mesto = \Nette\Utils\Strings::webalize($mesto); } $return = array('data' => array()); $cached = $this->cache->load($this->name . $this->action . $mesto . date('d.m.Y') . $id); if ($cached !== NULL) { if ($rec === FALSE) { $this->sendResponse(new \Nette\Application\Responses\JsonResponse($cached, "application/json;charset=utf-8")); return; } else { return $cached; } } $response = $this->tools->callCurlRequest(sprintf(static::URL_POCASI, $mesto)); switch ($id) { case 'dnes': preg_match('#<span id="title-loc">(.*?)</span>#i', $response, $title); preg_match('#<div id="predpoved-dnes".+<div class="info">\\s*<p>\\s*([^<]+)</p>(.+)id="predpoved-zitra"#i', $response, $r); preg_match('#<span class="date">(.*?)</span>#i', $r[0], $date); preg_match('#temp.*?value">([^<]*).*?sup">([^<]*)</span>.*?<span class=#i', $r[2], $r2); preg_match_all('#temp">([\\d-]+).*?sup">([^<]*)</span>.*?dayTime">\\s*([^<]*)\\s*</span>\\s*</div>#im', $r[2], $r3); \Tracy\Debugger::$maxLen = 10000; $return['data']['datum'] = "{$date[1]}"; $return['data']['predpoved'] = html_entity_decode($r[1]); $return['data']['nyni'] = html_entity_decode("{$r2[1]}{$r2[2]}"); $return['data']['rano'] = '?? °C'; $return['data']['odpoledne'] = '?? °C'; $return['data']['vecer'] = '?? °C'; $return['data']['noc'] = '?? °C'; foreach ($r3[3] as $key => $doba) { switch ($doba) { case 'Ráno': $return['data']['rano'] = html_entity_decode("{$r3[1][$key]}{$r3[2][$key]}"); break; case 'Odpoledne': $return['data']['odpoledne'] = html_entity_decode("{$r3[1][$key]}{$r3[2][$key]}"); break; case 'Večer': $return['data']['vecer'] = html_entity_decode("{$r3[1][$key]}{$r3[2][$key]}"); break; case 'V Noci': $return['data']['noc'] = html_entity_decode("{$r3[1][$key]}{$r3[2][$key]}"); break; } } $return['data']['pro'] = "Pro {$title[1]}"; $this->cache->save($this->name . $this->action . $mesto . date('d.m.Y') . $id, $return, array(\Nette\Caching\Cache::EXPIRE => "+1 day")); break; case 'zitra': preg_match('#<span id="title-loc">(.*?)</span>#i', $response, $title); preg_match('#<div id="predpoved-zitra".+<div class="info">\\s*<p>\\s*([^<]+)</p>(.+)id="predpoved-pozitri"#i', $response, $r); preg_match('#<span class="date">(.*?)</span>#i', $r[0], $date); preg_match('#atDay.*?temp.*?value">([-\\d]+).*?sup">([^<]+)</span>.*?atNight.*?temp.*?value">([-\\d]+).*?sup">([^<]+)</span>#i', $r[0], $r2); \Tracy\Debugger::$maxLen = 10000; $return['data']['datum'] = "{$date[1]}"; $return['data']['predpoved'] = html_entity_decode($r[1]); $return['data']['den'] = html_entity_decode("{$r2[1]}{$r2[2]}"); $return['data']['noc'] = html_entity_decode("{$r2[3]}{$r2[4]}"); $return['data']['pro'] = "Pro {$title[1]}"; $this->cache->save($this->name . $this->action . $mesto . date('d.m.Y') . $id, $return, array(\Nette\Caching\Cache::EXPIRE => "+1 day")); break; case 'pozitri': preg_match('#<span id="title-loc">(.*?)</span>#i', $response, $title); preg_match('#<div id="predpoved-pozitri".*?<div class="info">\\s*<p>\\s*([^<]+)</p>(.*?)id="predpoved-(.*?)"#i', $response, $r); preg_match('#<span class="date">(.*?)</span>#i', $r[0], $date); //preg_match( '#atDay.*?temp.*?value">(\d+).*?sup">([^<]+)</span>.*?atNight.*?temp.*?value">(\d+).*?sup">([^<]+)</span>#i', $r[0], $r2 ); preg_match('#atDay.*?temp.*?value">([-\\d]+).*?sup">([^<]+)</span>.*?atNight.*?temp.*?value">([-\\d]+).*?sup">([^<]+)</span>#i', $r[0], $r2); \Tracy\Debugger::$maxLen = 10000; $return['data']['datum'] = "{$date[1]}"; $return['data']['predpoved'] = html_entity_decode($r[1]); $return['data']['den'] = html_entity_decode("{$r2[1]}{$r2[2]}"); $return['data']['noc'] = html_entity_decode("{$r2[3]}{$r2[4]}"); $return['data']['pro'] = "Pro {$title[1]}"; $this->cache->save($this->name . $this->action . $mesto . date('d.m.Y') . $id, $return, array(\Nette\Caching\Cache::EXPIRE => "+1 day")); break; default: $id = NULL; $dnes = $this->actionPocasi("dnes", $mesto, true); $zitra = $this->actionPocasi("zitra", $mesto, true); $pozitri = $this->actionPocasi("pozitri", $mesto, true); $return['data'] = array('dnes' => $dnes, 'zitra' => $zitra, 'pozitri' => $pozitri); $this->cache->save($this->name . $this->action . $mesto . date('d.m.Y') . $id, $return, array(\Nette\Caching\Cache::EXPIRE => "+1 day")); break; } if ($rec === false) { $this->sendResponse(new \Nette\Application\Responses\JsonResponse($return, "application/json;charset=utf-8")); } else { return $return['data']; } }
<?php require __DIR__ . '/../vendor/autoload.php'; $configurator = new Nette\Configurator(); // Enable Nette Debugger for error visualisation & logging //$configurator->setDebugMode(TRUE); $configurator->enableDebugger(__DIR__ . '/../log'); Tracy\Debugger::$maxLen = 2000; // Enable RobotLoader - this will load all classes automatically $configurator->setTempDirectory(__DIR__ . '/../temp'); $configurator->createRobotLoader()->addDirectory(__DIR__)->register(); // Create Dependency Injection container from config.neon file $configurator->addConfig(__DIR__ . '/config/config.neon'); if (file_exists(__DIR__ . '/config/config.local.neon')) { $configurator->addConfig(__DIR__ . '/config/config.local.neon'); } $container = $configurator->createContainer(); return $container;