/** * Gets the weather from Yahoo!, which is more reliable. */ public function getYahooWeatherInfo() { echo "<pre>\n"; $manualSearch = new Collection(); foreach (Structure::get() as $structure) { if (strlen($structure->postal_code) > 0 && strlen($structure->country_code) > 0) { // find City in database? /** @var City $city */ $city = City::firstOrCreate(['postal_code' => $structure->postal_code, 'country_code' => $structure->country_code]); } else { // Fall back to Utrecht, the netherlands (central station area): $city = City::firstOrCreate(['postal_code' => '3511CE', 'country_code' => 'NL']); } $manualSearch->push($city); } $manualSearch = $manualSearch->unique(); // Yahoo weather is always per city. /** @var City $manualEntry */ foreach ($manualSearch as $city) { // function wont do stuff when its already filled. $this->_helper->getGeoFromYahoo($city); $weatherObject = $this->_helper->getYahooWeatherForCity($city); echo 'Now searching for city #' . $city->id . ', [postal code: ' . $city->postal_code . ', country code: ' . $city->country_code . ']' . "\n"; echo 'City has timezone ' . $city->time_zone . ' and woe-id ' . $city->woeid . "\n"; // try to get as many results from this as possible if (isset($weatherObject->query->results)) { $channel = $weatherObject->query->results->channel; // create report entry first: $report = new Report(); $report->city()->associate($city); $report->time = new Carbon(); $report->save(); // start saving data: // sunrise: $sysSunrise = new Carbon($channel->astronomy->sunrise, $city->time_zone); $sysSunrise->setTimezone('UTC'); $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'sys.sunrise'; $entry->value = $sysSunrise->format('U'); $entry->save(); // sunset: $sysSunset = new Carbon($channel->astronomy->sunset, $city->time_zone); $sysSunset->setTimezone('UTC'); $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'sys.sunset'; $entry->value = $sysSunset->format('U'); $entry->save(); // current temperature: $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'main.temp'; $entry->value = floatval($channel->item->condition->temp); $entry->save(); // current humidity: $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'main.humidity'; $entry->value = floatval($channel->atmosphere->humidity); $entry->save(); // current pressure: $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'main.pressure'; $entry->value = floatval($channel->atmosphere->pressure); $entry->save(); // max predicted for today. $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'main.temp_max'; $entry->value = floatval($channel->item->forecast[0]->high); $entry->save(); // min predicted for today. $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'main.temp_min'; $entry->value = floatval($channel->item->forecast[0]->low); $entry->save(); // wind speed $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'wind.speed'; $entry->value = floatval($channel->wind->speed); $entry->save(); // wind degrees $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'wind.deg'; $entry->value = floatval($channel->wind->direction); $entry->save(); // rain 1h? SKIPPED // weather.main $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'weather.main'; $entry->value = trim($channel->item->condition->text); $entry->save(); // weather.description $entry = new ReportValue(); $entry->report()->associate($report); $entry->name = 'weather.description'; $entry->value = trim($channel->item->description); $entry->save(); echo 'Saved ' . $report->reportValues()->count() . ' weather reports for city #' . $city->id . "\n\n"; } else { echo 'No results for city [' . $city->postal_code . ', ' . $city->country_code . ']' . "\n\n"; } } }
protected static function getRowInfo(&$rows, $params, $num, &$report) { $cols = array(); //expand columns $chart_rows = array(); foreach ($rows as $k => $row) { $vals = array(); if ($k === 0) { $i = 1; $unsorted = 1000; foreach ($row['values'] as $key => $value) { if (($temp = array_search($row['values'][$key]->i, $report->options['Charts'][$num]['columns'])) !== false) { $cols[$temp] = $key; } elseif (($temp = array_search($row['values'][$key]->key, $report->options['Charts'][$num]['columns'])) !== false) { $cols[$temp] = $key; } elseif ($report->options['Charts'][$num]['all']) { $cols[$unsorted] = $key; $unsorted++; } } ksort($cols); } foreach ($cols as $key) { if (isset($row['values'][$key]->chart_value) && is_array($row['values'][$key]->chart_value)) { foreach ($row['values'][$key]->chart_value as $ckey => $cval) { $temp = new ReportValue($row['values'][$key]->i, $ckey, trim($cval, '%$ ')); $temp->setValue($cval); $vals[] = $temp; } } else { $temp = new ReportValue($row['values'][$key]->i, $row['values'][$key]->key, $row['values'][$key]->original_value); $temp->setValue(trim($row['values'][$key]->getValue(), '%$ ')); $vals[] = $temp; } } $chart_rows[] = $vals; } //determine column types $types = array(); foreach ($chart_rows as $i => $row) { foreach ($row as $k => $v) { $type = self::determineDataType($v->original_value); //if the value is null, it doesn't influence the column type if (!$type) { $chart_rows[$i][$k]->setValue(null); continue; } elseif (!isset($types[$k])) { $types[$k] = $type; } elseif ($type === 'string') { $types[$k] = 'string'; } elseif ($types[$k] === 'date' && in_array($type, array('timeofday', 'datetime'))) { $types[$k] = 'datetime'; } elseif ($types[$k] === 'timeofday' && in_array($type, array('date', 'datetime'))) { $types[$k] = 'datetime'; } elseif ($types[$k] === 'date' && $type === 'number') { $types[$k] = 'number'; } } } $report->options['Charts'][$num]['datatypes'] = $types; //build chart rows $report->options['Charts'][$num]['Rows'] = array(); foreach ($chart_rows as $i => &$row) { $vals = array(); foreach ($row as $key => $val) { if (is_null($val->getValue())) { $val->datatype = 'null'; } elseif ($types[$key] === 'datetime') { $val->setValue(date('m/d/Y H:i:s', strtotime($val->getValue()))); $val->datatype = 'datetime'; } elseif ($types[$key] === 'timeofday') { $val->setValue(date('H:i:s', strtotime($val->getValue()))); $val->datatype = 'timeofday'; } elseif ($types[$key] === 'date') { $val->setValue(date('m/d/Y', strtotime($val->getValue()))); $val->datatype = 'date'; } elseif ($types[$key] === 'number') { $val->setValue(round(floatval(preg_replace('/[^-0-9\\.]*/', '', $val->getValue())), 6)); $val->datatype = 'number'; } else { $val->datatype = 'string'; } $vals[] = $val; } $report->options['Charts'][$num]['Rows'][] = array('values' => $vals, 'first' => !$report->options['Charts'][$num]['Rows']); } }
/** * @param \City $city * @param \stdClass $json * * @return \Report */ public function storeReport(\City $city, \stdClass $json) { $report = new \Report(); $report->city()->associate($city); $report->time = new Carbon(); $report->save(); // grab some values from the weather report, create report entries for them: $fields = \Config::get('marauder.weatherReportValues'); foreach ($fields as $section => $entries) { foreach ($entries as $fieldName) { if (isset($json->{$section}->{$fieldName})) { \ReportValue::create(['report_id' => $report->id, 'name' => $section . '.' . $fieldName, 'value' => $json->{$section}->{$fieldName}]); } } } // and some other values which aren't readily available: if (isset($json->weather[0])) { foreach ($fields['weather'] as $name) { \ReportValue::create(['report_id' => $report->id, 'name' => 'weather.' . $name, 'value' => $json->weather[0]->{$name}]); } } return $report; }