From 8dd16c5f3459db55275cbf3f825684e78888b6df Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Mon, 21 Nov 2016 23:04:33 +0100 Subject: [PATCH] =?UTF-8?q?Ajoute=20le=20module=20m=C3=A9t=C3=A9o=20WorldW?= =?UTF-8?q?eatherOnline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pour #1 --- .../worldweatheronline/Worldweatheronline.php | 38 +++++ .../css/worldweatheronline.css | 9 + .../includes/WorldweatheronlineApi.php | 160 ++++++++++++++++++ .../js/worldweatheronline.js | 30 ++++ 4 files changed, 237 insertions(+) create mode 100644 modules/worldweatheronline/Worldweatheronline.php create mode 100644 modules/worldweatheronline/css/worldweatheronline.css create mode 100644 modules/worldweatheronline/includes/WorldweatheronlineApi.php create mode 100644 modules/worldweatheronline/js/worldweatheronline.js diff --git a/modules/worldweatheronline/Worldweatheronline.php b/modules/worldweatheronline/Worldweatheronline.php new file mode 100644 index 0000000..15fdcab --- /dev/null +++ b/modules/worldweatheronline/Worldweatheronline.php @@ -0,0 +1,38 @@ +getParam('city'); + $future_weather = []; + include $this->pathToModule.'includes/WorldweatheronlineApi.php'; + $api = new WorldweatheronlineApi($ville); + if ($api->isFound()) { + $forecast = $api->getForecast(); + $current = $api->getCurrent(); + echo ' + '; + echo '
getParam("y").';"> + '.$ville.'
+
+ '.'Actuellement : ' . $current['temp_c'] . '°C - ' . $current['condition'].'
+ '.implode('
', array_map(function($f) { + return sprintf('%s : %s°C | %s°C - %s', $f['day_of_week'], $f['low'], $f['high'], $f['condition']); + }, $forecast)).' +
'; + } + } +} \ No newline at end of file diff --git a/modules/worldweatheronline/css/worldweatheronline.css b/modules/worldweatheronline/css/worldweatheronline.css new file mode 100644 index 0000000..0ab292f --- /dev/null +++ b/modules/worldweatheronline/css/worldweatheronline.css @@ -0,0 +1,9 @@ + #worldweatheronline {position:absolute; top:10px; left: 100px;padding:5px;-webkit-border-radius: 5px; + -moz-border-radius: 5px;border: 1px solid rgba(0,0,0,0.3); + -webkit-box-shadow: 0px 3px 3px rgba(0, 0, 0, 0.15); + -moz-box-shadow: 0px 3px 3px rgba(0, 0, 0, 0.15);} + #worldweatheronline .weatherpic img { + float: left; width: 53px; height: 53px; margin-right:10px; + } + + #city-submit{cursor:pointer;} \ No newline at end of file diff --git a/modules/worldweatheronline/includes/WorldweatheronlineApi.php b/modules/worldweatheronline/includes/WorldweatheronlineApi.php new file mode 100644 index 0000000..7c79298 --- /dev/null +++ b/modules/worldweatheronline/includes/WorldweatheronlineApi.php @@ -0,0 +1,160 @@ +city_code = $city_code; + $this->prefix_images = 'http://' . $this->domain; + //$this->url = 'http://'.$this->domain.'/ig/api?weather='.urlencode($this->city_code).'&hl='.$lang; + $this->url = 'http://api.worldweatheronline.com/free/v1/weather.ashx?q=' . $this->city_code . '&format=xml&num_of_days=3&key=' . self::KEY; + + $getContentCode = $this->getContent($this->url); + + if ($getContentCode == 200) { + + $content = utf8_encode($this->response); + + $xml = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA); + + if (!isset($xml->error)) { + //if(!isset($xml->weather->problem_cause)) { + + //$this->city = (string)$xml->weather->forecast_information->city->attributes()->data; + $this->city = (string)$xml->request->query; + + $this->current_conditions['condition'] = (string)$xml->current_condition->weatherDesc; + $this->current_conditions['temp_f'] = (string)$xml->current_condition->temp_F; + $this->current_conditions['temp_c'] = (string)$xml->current_condition->temp_C; + //$this->current_conditions['temp_c'] = (string)$xml->weather->current_conditions->temp_c->attributes()->data; + $this->current_conditions['humidity'] = (string)$xml->current_condition->humidity; + //$this->current_conditions['humidity'] = (string)$xml->weather->current_conditions->humidity->attributes()->data; + $this->current_conditions['icon'] = (string)$xml->current_condition->weatherIconUrl; + //$this->current_conditions['icon'] = $this->prefix_images.(string)$xml->weather->current_conditions->icon->attributes()->data; + $this->current_conditions['wind_condition'] = (string)$xml->current_condition->windspeedKmph; + //$this->current_conditions['wind_condition'] = (string)$xml->weather->current_conditions->wind_condition->attributes()->data; + + foreach ($xml->weather as $forecast_conditions_value) { + $forecast_conditions_temp = array(); + $forecast_conditions_temp['day_of_week'] = (string)$forecast_conditions_value->date; + $forecast_conditions_temp['low'] = (string)$forecast_conditions_value->tempMinC; + $forecast_conditions_temp['high'] = (string)$forecast_conditions_value->tempMaxC; + $forecast_conditions_temp['icon'] = (string)$forecast_conditions_value->weatherIconUrl; + $forecast_conditions_temp['condition'] = (string)$forecast_conditions_value->weatherDesc; + $this->forecast_conditions[] = $forecast_conditions_temp; + } + } else { + $this->is_found = false; + } + + } else { + trigger_error('Google results parse problem : http error ' . $getContentCode, E_USER_WARNING); + return null; + } + } + + /** + * Get URL content using cURL. + * + * @param string $url the url + * + * @return string the html code + */ + + protected function getContent($url) + { + if (!extension_loaded('curl')) { + throw new Exception('curl extension is not available'); + } + $useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; + $curl = curl_init(); + curl_setopt($curl, CURLOPT_TIMEOUT, 10); + curl_setopt($curl, CURLOPT_USERAGENT, $useragent); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_URL, $url); + $this->response = curl_exec($curl); + $infos = curl_getinfo($curl); + curl_close($curl); + return $infos['http_code']; + } + + /** + * Get the city + */ + + public function getCity() + { + return $this->city; + } + + /** + * Get the current weather + */ + + public function getCurrent() + { + return $this->current_conditions; + } + + /** + * Get the forecast weather + */ + + public function getForecast() + { + return $this->forecast_conditions; + } + + /** + * Get the icon + */ + + public function getIcon() + { + return $this->current_conditions['icon']; + } + + /** + * If teh city was found + */ + + public function isFound() + { + return $this->is_found; + } +} \ No newline at end of file diff --git a/modules/worldweatheronline/js/worldweatheronline.js b/modules/worldweatheronline/js/worldweatheronline.js new file mode 100644 index 0000000..20e6ffa --- /dev/null +++ b/modules/worldweatheronline/js/worldweatheronline.js @@ -0,0 +1,30 @@ +$(document).ready(function(){ + $('body').append('
'); + $('#menu-bar').prepend($('#city-menu')); + $('#city').append('

Quelle ville pour la meteo ?


Enregistrer'); + $('#city-menu').fancybox({ + 'zoomSpeedIn' : 600, + 'zoomSpeedOut' : 500, + 'easingIn' : 'easeOutBack', + 'easingOut' : 'easeInBack', + 'hideOnContentClick': false, + 'padding' : 15 + }); + $('#city-submit').live('click', function(){ + var ville = $('#cityChoice').val(); + if(ville != '' || ville != null){ + $.get('ajax/update.php', {id:'worldweatheronline', city: ville}, function(msg){ + location.reload(); + }); + }else{ + $('#city').append('City can\'t be empty.'); + } + }); + + var tmp; + + /* A helper function for converting a set of elements to draggables: */ + make_draggable($('#worldweatheronline')); + + +});