variable_get('tribune_meteo_name', "Meteo"), ); if (variable_get('tribune_meteo_authentified', FALSE)) { $answer['login'] = $answer['info']; } if (preg_match(':^/'. t('weather') .' (.+)$:', $post['message'], $matches)) { $city = $matches[1]; $answer['message'] = _tribune_meteo_get_weather_info($city); $answer['message'] = tribune_filters_print_clock($post) ." ". $answer['message']; return array($answer); } elseif (preg_match(':^/'. t('forecast') .'? (.+)$:', $post['message'], $matches)) { $city = $matches[1]; $answer['message'] = _tribune_meteo_get_weather_forecast($city); $answer['message'] = tribune_filters_print_clock($post) ." ". $answer['message']; return array($answer); } } function tribune_meteo_help() { $user = array_shift(user_load_self(array())); $help = 'Type "/weather city" for weather information, "/forecast city" for a forecast.'; return $help; } function _tribune_meteo_get_weather_info($city) { if (preg_match("/[A-Z]{4}[0-9]{4}/", $city)) { return _tribune_meteo_get_yahoo_weather_info(strtoupper($city)); } else { return _tribune_meteo_get_google_weather_info($city); } } function _tribune_meteo_get_weather_forecast($city) { if (preg_match("/[A-Z]{4}[0-9]{4}/", $city)) { return _tribune_meteo_get_yahoo_weather_forecast(strtoupper($city)); } else { return _tribune_meteo_get_google_weather_forecast($city); } } function _tribune_meteo_get_google_feed($city) { $lang = $language['language']; $units = variable_get('tribune_meteo_imperial', FALSE) ? 'US' : 'SI'; $url = "http://www.google.co.uk/ig/api?hl=". $lang ."&weather=". urlencode($city); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); try { $xml = new SimpleXMLElement(utf8_encode($result), LIBXML_NOERROR | LIBXML_NOWARNING); if ($xml->xpath("/xml_api_reply/weather/problem_cause")) { return array('error' => t("Unable to find weather data for @city", array('@city' => $city))); } return array('error' => FALSE, 'xml' => $xml, 'lang' => $lang, 'units' => $units); } catch (Exception $e) { return array('error' => $e->getMessage()); } } function _tribune_meteo_get_google_weather_info($city) { $result = _tribune_meteo_get_google_feed($city); if ($result['error']) { $info = $result['error']; } else { $xml = $result['xml']; $to_units = strtolower($result['units']); $node = $xml->xpath("/xml_api_reply/weather/forecast_information/unit_system"); $unit_system = $node[0]['data']; switch ($to_units) { case "us": $temp_unit = "F"; $speed_unit = "mph"; break; default: $to_units = "si"; case "si": $temp_unit = "C"; $speed_unit = "km/h"; break; } $node = $xml->xpath("/xml_api_reply/weather/forecast_information/city"); $city = $node[0]['data']; $node = $xml->xpath("/xml_api_reply/weather/current_conditions/wind_condition"); $wind = $node[0]['data']; if ($to_units == "us") { $node = $xml->xpath("/xml_api_reply/weather/current_conditions/temp_f"); } else { $node = $xml->xpath("/xml_api_reply/weather/current_conditions/temp_c"); } $temp = $node[0]['data']; $node = $xml->xpath("/xml_api_reply/weather/current_conditions/condition"); $desc = $node[0]['data']; $node = $xml->xpath("/xml_api_reply/weather/current_conditions/humidity"); $humidity = preg_replace('/^[^0-9]*([0-9]*)%[^0-9]*$/', '$1', $node[0]['data']); $info = t("Weather in @city: @desc, @temp °@temp_unit, humidity: @humidity%", array('@city' => $city, '@desc' => $desc, '@temp' => $temp, '@temp_unit' => $temp_unit, '@humidity' => $humidity)); } return $info; } function _tribune_meteo_get_google_weather_forecast($city) { $result = _tribune_meteo_get_google_feed($city); if ($result['error']) { return $result['error']; } else { $xml = $result['xml']; $to_units = strtolower($result['units']); $node = $xml->xpath("/xml_api_reply/weather/forecast_information/unit_system"); $unit_system = strtolower($node[0]['data']); switch ($to_units) { case "us": $temp_unit = "F"; $speed_unit = "mph"; break; default: $to_units = "si"; case "si": $temp_unit = "C"; $speed_unit = "km/h"; break; } $node = $xml->xpath("/xml_api_reply/weather/forecast_information/city"); $city = $node[0]['data']; $conditions = array(); $node = $xml->xpath("/xml_api_reply/weather/forecast_conditions"); foreach ($node as $forecast) { $date = ""; $high = "?"; $low = "?"; $desc = "?"; foreach ($forecast->children() as $child) { $value = $child['data']; switch ($child->getName()) { case 'high': $high = tribune_meteo_temp($value, $unit_system, $to_units); break; case 'low': $low = tribune_meteo_temp($value, $unit_system, $to_units); break; case 'day_of_week': $date = $value; break; case 'condition': $desc = $value; break; } } if ($date) { $conditions[] = array( 'date' => $date, 'high' => $high, 'low' => $low, 'desc' => $desc, ); } } $args = array(); $info = "Forecast for @city ― "; $args['@city'] = $city; $args['@temp_unit'] = $temp_unit; foreach ($conditions as $id => $cond) { $info .= "@date". $id .": @low". $id ."»@high". $id ." °@temp_unit (@cond". $id ."), "; $args["@date". $id] = $cond['date']; $args["@low". $id] = $cond['low']; $args["@high". $id] = $cond['high']; $args["@cond". $id] = $cond['desc']; } return t(substr($info, 0, -2), $args); } } function _tribune_meteo_get_yahoo_weather_info($city) { $info = ""; $url = "http://weather.yahooapis.com/forecastrss?u=c&p=". $city; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); $xml = new SimpleXMLElement($result); $speed_unit = ""; $temp_unit = ""; $node = $xml->xpath("/rss/channel/yweather:units"); foreach ($node[0]->attributes() as $attr => $value) { switch ($attr) { case 'temperature': $temp_unit = $value; break; case 'speed': $speed_unit = $value; break; } } $city = "?"; $node = $xml->xpath("/rss/channel/yweather:location"); foreach ($node[0]->attributes() as $attr => $value) { switch ($attr) { case 'city': $city = $value; break; } } $wind = "?"; $node = $xml->xpath("/rss/channel/yweather:wind"); foreach ($node[0]->attributes() as $attr => $value) { switch ($attr) { case 'speed': $wind = $value; break; } } $temp = "?"; $desc = "?"; $node = $xml->xpath("/rss/channel/item/yweather:condition"); foreach ($node[0]->attributes() as $attr => $value) { switch ($attr) { case 'temp': $temp = $value; break; case 'text': $desc = $value; break; } } $info = t("Weather in @city: @desc, @temp °@temp_unit", array('@city' => $city, '@desc' => $desc, '@temp' => $temp, '@temp_unit' => $temp_unit)); return $info; } function _tribune_meteo_get_yahoo_weather_forecast($city) { $info = ""; $url = "http://weather.yahooapis.com/forecastrss?u=c&p=". urlencode($city); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); $xml = new SimpleXMLElement($result); $speed_unit = ""; $temp_unit = ""; $node = $xml->xpath("/rss/channel/yweather:units"); foreach ($node[0]->attributes() as $attr => $value) { switch ($attr) { case 'temperature': $temp_unit = $value; break; case 'speed': $speed_unit = $value; break; } } $city = "?"; $node = $xml->xpath("/rss/channel/yweather:location"); foreach ($node[0]->attributes() as $attr => $value) { switch ($attr) { case 'city': $city = $value; break; } } $conditions = array(); $node = $xml->xpath("/rss/channel/item/yweather:forecast"); foreach ($node as $forecast) { $date = ""; $high = "?"; $low = "?"; $desc = "?"; foreach ($forecast->attributes() as $attr => $value) { switch ($attr) { case 'high': $high = $value; break; case 'low': $low = $value; break; case 'date': $date = $value; break; case 'text': $desc = $value; break; } } if ($date) { $timestamp = strtotime($date); $conditions[$timestamp] = array( 'date' => date('d/m/Y', $timestamp), 'high' => $high, 'low' => $low, 'desc' => $desc, ); } } $args = array(); $info = "Forecast for @city ― "; $args['@city'] = $city; $args['@temp_unit'] = $temp_unit; foreach ($conditions as $timestamp => $cond) { $info .= "@date$timestamp: @low${timestamp}»@high$timestamp °@temp_unit, "; $args["@date". $timestamp] = $cond['date']; $args["@low". $timestamp] = $cond['low']; $args["@high". $timestamp] = $cond['high']; } return t(substr($info, 0, -2), $args); } function tribune_meteo_f2c($fahrenheit) { return round(($fahrenheit-32)*(5/9)); } function tribune_meteo_c2f($celsius) { return round($celsius*(5/9)+32); } function tribune_meteo_temp($value, $from, $to) { $from = strtolower($from); $to = strtolower($to); if ($from != $to) { switch ($to) { case 'si': return tribune_meteo_f2c($value); case 'us': return tribune_meteo_c2f($value); default: return $value; } } else { return $value; } } function tribune_meteo_settings() { $form = array(); $form['tribune_meteo_name'] = array( '#type' => "textfield", '#title' => t("Display name"), '#default_value' => variable_get('tribune_meteo_name', "Meteo"), ); $form['tribune_meteo_authentified'] = array( '#type' => "checkbox", '#title' => t("Appear to be authentified"), '#default_value' => variable_get('tribune_meteo_authentified', FALSE), '#description' => t("Whether this filter will appear to be 'anonymous' or 'authentified'. Since the filter does not correspond to a real user, posting as authentified may result in 'nickname collisions'."), ); $form['tribune_meteo_imperial'] = array( '#type' => "checkbox", '#title' => t("Use imperial system"), '#default_value' => variable_get('tribune_meteo_imperial', FALSE), '#description' => t("If this is enabled, the imperial system will be used, otherwise SI units will be shown."), ); return system_settings_form($form); }