Ajoute le module météo WorldWeatherOnline

Pour #1
This commit is contained in:
Clement Desmidt 2016-11-21 23:04:33 +01:00
parent 56e63ed73e
commit 8dd16c5f34
4 changed files with 237 additions and 0 deletions

View File

@ -0,0 +1,38 @@
<?php
class worldweatheronline extends Module
{
protected $moduleTitle = 'Météo';
protected $paramsList = array(
'visibility',
'city',
'x',
'y'
);
public function __construct($params){
parent::__construct(__CLASS__, $params);
}
public function build(){
$ville = $this->getParam('city');
$future_weather = [];
include $this->pathToModule.'includes/WorldweatheronlineApi.php';
$api = new WorldweatheronlineApi($ville);
if ($api->isFound()) {
$forecast = $api->getForecast();
$current = $api->getCurrent();
echo '<script type="text/javascript" src="'.$this->pathToModule.'js/'.$this->moduleName.'.js"></script>
<link rel="stylesheet" type="text/css" href="'.$this->pathToModule.'css/'.$this->moduleName.'.css">';
echo '<div id="worldweatheronline" style="left:'.$this->getParam("x").'; top:'.$this->getParam("y").';">
'.$ville.'<br/>
<div class="weatherpic"><img src="'.$api->getIcon().'"/></div>
<strong>'.'Actuellement : ' . $current['temp_c'] . '°C - ' . $current['condition'].'</strong><br/>
'.implode('<br/>', array_map(function($f) {
return sprintf('%s : %s°C | %s°C - %s', $f['day_of_week'], $f['low'], $f['high'], $f['condition']);
}, $forecast)).'
</div>';
}
}
}

View File

@ -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;}

View File

@ -0,0 +1,160 @@
<?php
class WorldweatheronlineApi
{
/** City code input **/
private $city_code = '613135'; // Montsoult
/** City label get on the google webservice **/
private $city = '';
/** Domain of the google website **/
private $domain = 'www.google.com';
/** Prefix of the img link **/
private $prefix_images = '';
/** Array with current weather **/
private $current_conditions = array();
/** Array with forecast weather **/
private $forecast_conditions = array();
/** If the city was found **/
private $is_found = true;
/** The HTML response send by the service **/
private $response;
/** API KEY **/
const KEY = '60dc184fe4be1b80bbaa8cd1103b0ab8e45a25f0';
/**
* Class constructor
* @param string $city_code is the label of the city
* @param string $lang the lang of the return weather labels
* @return ...
*/
function __construct($city_code, $lang = 'fr')
{
$this->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;
}
}

View File

@ -0,0 +1,30 @@
$(document).ready(function(){
$('body').append('<a href="#city" id="city-menu"><img src="images/interface/weather.png" /></a><div style="display:none;"><div id="city"></div></div>');
$('#menu-bar').prepend($('#city-menu'));
$('#city').append('<h3>Quelle ville pour la meteo ?</h3><br/><input type="text" id="cityChoice" name="cityChoice"/><span id="city-submit" class="green-button">Enregistrer</span>');
$('#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('<span class="error">City can\'t be empty.</span>');
}
});
var tmp;
/* A helper function for converting a set of elements to draggables: */
make_draggable($('#worldweatheronline'));
});