/** * retrieve the content */ public function run() { // api call $cache = new class_cache('yr.no_' . substr(strrchr($this->location, '/'), 1) . '.xml'); if ($cache->hit()) { $content = $cache->read(); } else { $url = 'http://www.yr.no/place/' . rawurlencode($this->location) . '/forecast.xml'; $content = file_get_contents($url); } if (substr($content, 0, 5) == '<?xml') { // write cache $cache->write($content); $xml = simplexml_load_string($content); $this->debug($xml); // today $this->data['city'] = (string) $xml->location->name; // forecast $i = 0; foreach ($xml->forecast->tabular->time as $day) { if (config_lang == 'de') { $windspeed = ' mit ' . round((string) $day->windSpeed->attributes()->mps * 3.6, 1) . ' km/h'; } elseif (config_lang == 'nl') { $windspeed = ' met ' . round((string) $day->windSpeed->attributes()->mps * 3.6, 1) . ' km/u'; } else { $windspeed = ' at ' . round((string) $day->windSpeed->attributes()->mps * 2.24, 1) . ' MPH'; } if ($i == 0) { $this->data['current']['date'] = (string) $day->attributes()->from; $this->data['current']['conditions'] = translate((string) $day->symbol->attributes()->name, 'yr.no'); $this->data['current']['wind'] = translate((string) $day->windSpeed->attributes()->name . ' from ' . (string) $day->windDirection->attributes()->code, 'yr.no') . $windspeed; $this->data['current']['icon'] = $this->icon((string) $day->symbol->attributes()->number, $this->icon_sm); $this->data['current']['temp'] = (double) $day->temperature->attributes()->value . '°C'; $this->data['current']['more'] = (int) $day->pressure->attributes()->value . ' hPa'; $i++; } if ($i < 5 and $day->attributes()->period == 2) { $this->data['forecast'][$i]['date'] = (string) $day->attributes()->from; $this->data['forecast'][$i]['conditions'] = translate((string) $day->symbol->attributes()->name, 'yr.no'); $this->data['forecast'][$i]['wind'] = translate((string) $day->windSpeed->attributes()->name . ' from ' . (string) $day->windDirection->attributes()->code, 'yr.no') . $windspeed; $this->data['forecast'][$i]['icon'] = $this->icon((string) $day->symbol->attributes()->number); $this->data['forecast'][$i]['temp'] = (double) $day->temperature->attributes()->value . '°C'; $this->data['forecast'][$i]['more'] = (int) $day->pressure->attributes()->value . ' hPa'; $i++; } } } else { $this->error('Weather: yr.no', 'Read request failed!'); } }
/** * retrieve the content */ public function run() { // api call $cache = new class_cache('rss_' . strtolower($this->url)); if ($cache->hit()) { $xml = simplexml_load_string($cache->read()); } else { $xml = simplexml_load_string($cache->write(file_get_contents('http://' . $this->url))); } if ($xml) { $i = 1; // the entries foreach ($xml->channel->item as $item) { $item = (array) $item; // media? if (isset($item['enclosure']) && ((string) $item['enclosure']->attributes()->type == 'image/jpeg' || (string) $item['enclosure']->attributes()->type == 'image/jpg')) { unset($item['image']); $item['image']['url'] = (string) $item['enclosure']->attributes()->url; } unset($item['enclosure']); // description $item["description"] = (string) $item["description"]; $this->data['entry'][] = $item; if ($i++ >= $this->limit) { break; } } // the channel $channel = (array) $xml->channel; if (isset($channel['image'])) { $channel['image'] = array('url' => (string) $channel['image']->url, 'title' => (string) $channel['image']->title, 'link' => (string) $channel['image']->link); } unset($channel['item']); $this->data['channel'] = $channel; } else { $this->error('RSS', 'Read request failed \'' . $this->url . '\''); } }
/** * retrieve the content */ public function run() { // api call $cache = new class_cache('wunderground_' . $this->location . '.json'); if ($cache->hit()) { $content = $cache->read(); } else { $url = 'http://api.wunderground.com/api/' . config_weather_key . '/conditions/forecast/lang:' . trans('wunderground', 'lang') . '/q/' . $this->location . '.json'; $content = file_get_contents($url); } $parsed_json = json_decode($content); if ($parsed_json->{'forecast'}) { // write cache $cache->write($content); $this->debug($parsed_json); // today $this->data['city'] = (string) $parsed_json->{'current_observation'}->{'display_location'}->{'city'}; if (substr(trans('format', 'temp'), -1, 1) != "F") { $this->data['current']['temp'] = transunit('temp', (double) $parsed_json->{'current_observation'}->{'temp_c'}); } else { $this->data['current']['temp'] = transunit('temp', (double) $parsed_json->{'current_observation'}->{'temp_f'}); } $this->data['current']['conditions'] = (string) $parsed_json->{'current_observation'}->{'weather'}; $this->data['current']['icon'] = $this->icon((string) $parsed_json->{'current_observation'}->{'icon'}, $this->icon_sm); // get the good values by countries in km/h or mph for the wind and gusts if (trim(strrchr(trans('format', 'speed'), ' ')) != 'mph') { $wind_speed = transunit('speed', (double) $parsed_json->{'current_observation'}->{'wind_kph'}); $wind_gust = transunit('speed', (double) $parsed_json->{'current_observation'}->{'wind_gust_kph'}); } else { $wind_speed = transunit('speed', (double) $parsed_json->{'current_observation'}->{'wind_mph'}); $wind_gust = transunit('speed', (double) $parsed_json->{'current_observation'}->{'wind_gust_mph'}); } $wind_direction = translate((string) $parsed_json->{'current_observation'}->{'wind_dir'}, 'wunderground'); // in french, there is a difference when the word stars with a vowel // de (from) becomes d' can be use if a language has the same exception if (config_lang == 'fr') { if (substr($wind_direction, 0, 1) == 'O' || substr($wind_direction, 0, 1) == 'E') { $from = translate('from_ew', 'wunderground'); } else { $from = translate('from_ns', 'wunderground'); } } else { $from = translate('from', 'wunderground'); } // when the wind has no fix direction from is blank if ((string) $parsed_json->{'current_observation'}->{'wind_dir'} == 'Variable') { $from = ''; } $this->data['current']['wind'] = translate('wind', 'wunderground') . " " . $from . " " . $wind_direction . ", " . translate('wind_speed', 'wunderground') . " " . $wind_speed; if ($wind_gust > 0) { $this->data['current']['wind'] .= ", " . translate('wind_gust', 'wunderground') . " " . $wind_gust; } $this->data['current']['more'] = translate('humidity', 'wunderground') . " " . (string) $parsed_json->{'current_observation'}->{'relative_humidity'}; // forecast $i = 0; foreach ($parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'} as $day) { $this->data['forecast'][$i]['date'] = (string) $day->{'date'}->{'year'} . '-' . (string) $day->{'date'}->{'month'} . '-' . (string) $day->{'date'}->{'day'}; $this->data['forecast'][$i]['conditions'] = (string) $day->{'conditions'}; $this->data['forecast'][$i]['icon'] = $this->icon((string) $day->{'icon'}); if (substr(trans('format', 'temp'), -1, 1) != "F") { $this->data['forecast'][$i]['temp'] = (double) $day->{'low'}->{'celsius'} . '°/' . (double) $day->{'high'}->{'celsius'} . '°'; } else { $this->data['forecast'][$i]['temp'] = (double) $day->{'low'}->{'fahrenheit'} . '°/' . (double) $day->{'high'}->{'fahrenheit'} . '°'; } $i++; } } else { $add = $parsed_json->{'response'}->{'error'}->{'description'}; $this->error('Weather: wounderground.com', 'Read request failed' . ($add ? ': ' . $add : '') . '!'); } }
public function run() { $cache = new class_cache('calendar_google.json'); // check for cached data if ($cache->hit(3600)) { $cache_content = json_decode($cache->read(), true); if ($cache_content['accessTokenExpiry'] < time()) { // if token is not valid anymore reload everything cached unset($cache_content); } } if (!isset($cache_content['accessToken'])) { $token_url = 'https://accounts.google.com/o/oauth2/token'; $post_data = array('client_secret' => self::CLIENT_SECRET, 'grant_type' => 'refresh_token', 'refresh_token' => self::REFRESH_TOKEN, 'client_id' => self::CLIENT_ID); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $token_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); $token_object = json_decode($result); $cache_content['accessToken'] = $token_object->{'access_token'}; $cache_content['accessTokenExpiry'] = time() + $token_object->{'expires_in'} - 60; } //--------------------------------------------------------------------- // so now we have an access-token, let's use it to access the calendar $context = stream_context_create(array('http' => array('method' => "GET", 'header' => "Authorization: OAuth " . $cache_content['accessToken']))); // first let's retrieve the colors and cache them if (!isset($cache_content['calendarColors'])) { $resturl = 'https://www.googleapis.com/calendar/v3/colors'; $content = @file_get_contents($resturl, false, $context); if ($content !== false) { $result = json_decode($content, true); $cache_content['calendarColors'] = $result["event"]; //var_dump($cache_content['calendarColors']); } } // then retrieve the users calendars available and cache them, too // do only if more than one calendar specified if (count($this->calendar_ids) > 1 && !isset($cache_content['calendarList'])) { $resturl = 'https://www.googleapis.com/calendar/v3/users/me/calendarList'; $content = @file_get_contents($resturl, false, $context); if ($content !== false) { $result = json_decode($content); $calendarList = array(); foreach ($result->{'items'} as $entry) { $cal = array(); $cal["description"] = $entry->{'summary'}; $cal["backgroundColor"] = $entry->{'backgroundColor'}; $calendarList[$entry->{'id'}] = $cal; } $cache_content['calendarList'] = $calendarList; //var_dump($cache_content['calendarList']); } } // retrieve the calendar entries from each calendar $events = array(); foreach ($this->calendar_ids as $calendarid) { $resturl = 'https://www.googleapis.com/calendar/v3/calendars/' . urlencode($calendarid) . '/events?maxResults=' . $this->count . '&q=-%22%40visu+no%22&singleEvents=true&orderBy=startTime&timeMin=' . urlencode(date('c')); $content = @file_get_contents($resturl, false, $context); $this->debug($content); // for debugging purposes only // echo $content; // echo "###########"; if ($content !== false) { $result = json_decode($content, true); foreach ($result["items"] as $entry) { $startstamp = (string) $entry["start"]["dateTime"]; $endstamp = (string) $entry["end"]["dateTime"]; if ($startstamp == '') { $startstamp = (string) $entry["start"]["date"]; } if ($endstamp == '') { $endstamp = (string) $entry["end"]["date"]; } $start = strtotime($startstamp); $end = strtotime($endstamp); $color = ''; if ((string) $entry["colorId"] != '') { $color = $cache_content['calendarColors'][(string) $entry["colorId"]]['background']; } else { if (count($this->calendar_ids) > 1) { $color = $cache_content['calendarList'][$calendarid]['backgroundColor']; } } $events[$start] = array('start' => date('y-m-d', $start) . ' ' . date('H:i:s', $start), 'end' => date('y-m-d', $end) . ' ' . date('H:i:s', $end), 'title' => (string) $entry["summary"], 'content' => (string) $entry["description"], 'where' => (string) $entry["location"], 'color' => $color, 'link' => (string) $entry["htmlLink"]); } } else { $this->error('Calendar: Google', 'Calendar ' . $calendarid . ' read request failed!'); } } // finally re-order the events comming potentially from different calendars $i = 1; ksort($events); foreach ($events as $event) { $event['pos'] = $i++; $this->data[] = $event; if ($i > $this->count) { break; } } // at the end write back cache $cache->write(json_encode($cache_content)); }