Diffs
Services_WeatherUnderground/branches/0_1_5/WeatherUnderground.php
@@ -0,0 +1,262 @@
+<?php
+/**
+ * Services_WeatherUnderground 0.1.5
+ *
+ * @author FreeBSE <freebse@live.jp> <http://panasocli.cc/wordpress>
+ * @package Services_WeatherUnderground
+ * @version Services_WeatherUnderground v 0.1.5 2010/03/07
+ *
+ */
+
+//API URL
+define('WG_API_AP', 'http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml');
+
+//各種設定
+define('MPH_MS', 0.44704);
+define('CACHE_DIR', 'tmp/');
+define('CACHE_BASE_DIR', dirname(__FILE__) . '/WeatherUnderground/' . CACHE_DIR);
+define('LIFE_TIME', 1800);
+
+//エラーコード
+define('CITY_NOT_FOUND', 0x01);
+
+interface WeatherUnderground{
+ public function getWeatherData();
+ public function getRawWeatherData();
+}
+
+class Services_WeatherUnderground implements WeatherUnderground {
+
+ public $weather = null;
+
+ private $cache_options = array(
+ 'cacheDir' => CACHE_DIR,
+ 'lifeTime' => LIFE_TIME
+ );
+
+ /**
+ *コンストラクタ
+ *
+ * @param $query string
+ * プロパティに代入
+ */
+ public function __construct($query){
+ $this->cacheRemove();
+ $data = $this->getWeather($query);
+ $this->weather = $this->toArray($data);
+ }
+
+ /**
+ * WUGのAPIを叩く
+ * @param $query string
+ * @return XML
+ */
+ private function getWeather($query){
+ $id = $query;
+ if($this->cacheGet($id)){
+ return $this->cacheGet($id);
+ }
+ require_once 'HTTP/Client.php';
+ $client = new HTTP_Client();
+ $client->get($this->makeUrl($query));
+ $response = $client->currentResponse();
+ $response['body'] = mb_convert_encoding($response['body'], 'UTF-8', 'auto');
+ $this->cacheSet($response['body'], $id);
+ unset($id);
+ unset($query);
+ return $response['body'];
+ }
+
+ /**
+ *
+ * 指定されたキャッシュがあるかチェックする
+ *
+ * @param Object $Cache_Lite
+ * @param String $id
+ * @return 成功:キャッシュデータ 失敗:FALSE
+ */
+ private function cacheCheck($Cache_Lite, $id){
+ if(!is_dir('tmp')) mkdir('tmp');
+ if (!$Cache_Lite->get($id)) {
+ return false;
+ }
+ return $Cache_Lite->get($id);
+ }
+
+ /**
+ *
+ * キャッシュを取得する
+ *
+ * @param String $id
+ * @return 成功:キャッシュデータ 失敗:FALSE
+ */
+ private function cacheGet($id){
+ require_once('Cache/Lite.php');
+ $Cache_Lite = new Cache_Lite($this->cache_options);
+ if(!$this->cacheCheck($Cache_Lite, $id)){
+ return false;
+ }
+ return $Cache_Lite->get($id);
+ }
+
+ /**
+ *
+ * キャッシュを作る
+ *
+ * @param 天気データ $data
+ * @param String $id
+ * @return 失敗:FALSE
+ */
+ private function cacheSet($data, $id){
+ if(!is_dir('tmp')) mkdir('tmp');
+ if(strpos(PHP_OS, 'WIN') !== 0) chmod(0777, 'tmp');
+ $Cache_Lite = new Cache_Lite($this->cache_options);
+ if(!$this->cacheCheck($Cache_Lite, $id)){
+ $r = $Cache_Lite->save($data, $id);
+ if($r === false){
+ return $r;
+ }
+ }
+ unset($Cache_Lite);
+ return true;
+ }
+
+ /**
+ * キャッシュを削除する
+ */
+ private function cacheRemove(){
+ if(!is_dir(CACHE_BASE_DIR)) return false;
+ $dir = scandir(CACHE_BASE_DIR);
+ foreach($dir as $val){
+ if($val !== '.' && $val !== '..' && ((int) (time() - filemtime(CACHE_BASE_DIR . $val)) > LIFE_TIME)){
+ if(file_exists(CACHE_BASE_DIR . $val)){
+ unlink(CACHE_BASE_DIR . $val);
+ }
+ }
+ }
+ unset($dir);
+ }
+
+ /**
+ * XMLを配列に変換する
+ *
+ * @param XML $data
+ * @return Array
+ */
+ private function toArray($data){
+ require_once 'XML/Unserializer.php';
+ $xml = new XML_Unserializer();
+ $xml->setOption('parseAttributes',true);
+ $xml->unserialize($data);
+ return $xml->getUnserializedData();
+ }
+
+ /**
+ * WeatherUndergroundb API REST URLの生成
+ * @return string
+ */
+ private function makeUrl($query){
+ return sprintf('%s?query=%s', WG_API_AP, $query);
+ }
+
+ /**
+ * WUGの生の天気情報を取得する
+ * @return Array
+ */
+ public function getRawWeatherData(){
+ if(!$this->weather['station_id']){
+ return CITY_NOT_FOUND;
+ }
+ return $this->weather;
+ }
+
+ /**
+ * 風向を日本語に変換
+ *
+ * @param String $winddir
+ * @return String
+ */
+ protected function getWindDir($winddir){
+ switch($winddir){
+ case 'NNW':$wind_dir = '北北西';break;
+ case 'NW':$wind_dir = '北西';break;
+ case 'WNW':$wind_dir = '西北西';break;
+ case 'W':$wind_dir = '西';break;
+ case 'West':$wind_dir = '西';break;
+ case 'N':$wind_dir = '北';break;
+ case 'North':$wind_dir = '北';break;
+ case 'E':$wind_dir = '東';break;
+ case 'East':$wind_dir = '東';break;
+ case 'NE':$wind_dir = '北東';break;
+ case 'NNE':$wind_dir = '北北東';break;
+ case 'ENE':$wind_dir = '東北東';break;
+ case 'S':$wind_dir = '南';break;
+ case 'South':$wind_dir = '南';break;
+ case 'SE':$wind_dir = '南東';break;
+ case 'SSE':$wind_dir = '南南東';break;
+ case 'ESE':$wind_dir = '東南東';break;
+ case 'WSW':$wind_dir = '西南西';break;
+ case 'SSW':$wind_dir = '南南西';break;
+ case 'SW':$wind_dir = '南西';break;
+ case 'Variable':$wind_dir = '無風';break;
+ }
+ return $wind_dir;
+ }
+
+ /**
+ * 風速変換を行います
+ *
+ * @param int $mph Mile Per Hour
+ * @return int Metor
+ */
+ protected function convertMphToMetor($mph){
+ $mph = (int) round($mph * MPH_MS, 2);
+ return $mph;
+ }
+
+ /**
+ * わかりやすい形で天気情報を取得する
+ * @return Array
+ */
+ public function getWeatherData(){
+ if(!$this->weather['station_id']){
+ unset($this->weather);
+ return CITY_NOT_FOUND;
+ }
+
+ //風向情報の取得
+ $wind_dir = $this->getWindDir($this->weather['wind_dir']);
+
+ //風速変換
+ $mph = $this->convertMphToMetor($this->weather['wind_mph']);
+
+ $mph = $mph == 0 ? '無風' : $mph . ' m/s';
+
+ $weather = array(
+ //街
+ 'city' => $this->weather['display_location']['city'],
+ //国
+ 'country' => $this->weather['display_location']['state_name'],
+ //天気画像
+ 'image' => $this->weather['icon_url_base'] . $this->weather['icon'] . $this->weather['icon_url_name'],
+ //観測地
+ 'observation_location' => $this->weather['observation_location']['city'],
+ //観測時間
+ 'observation_time' => date("Y年m月d日 H時i分",strtotime(preg_replace("/Last Updated on |JST/", "", $this->weather['observation_time']))),
+ //天気
+ //'weather' => $this->weather['weather'],
+ //気温
+ 'temp' => $this->weather['temp_c'] . '℃',
+ //湿度
+ 'humidity' => $this->weather['relative_humidity'],
+ //風向
+ 'wind_dir' => $wind_dir,
+ //風速
+ 'wind_speed' => $mph,
+ //気圧
+ 'pressure' => $this->weather['pressure_mb'] . ' hPa',
+ );
+ return $weather;
+ }
+}
+?>
\ ファイルの末尾に改行がありません
Services_WeatherUnderground/branches/0_1_5/WeatherUnderground/sample.php
@@ -0,0 +1,113 @@
+<?php
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+require_once('../WeatherUnderground.php');
+$wg = new Services_WeatherUnderground('Osaka');
+if($wg->getWeatherData() === CITY_NOT_FOUND){
+ echo sprintf('指定された観測地は見つかりませんでした「Error Code 0x%02X」', $wg->getWeatherData());
+}else{
+ print_r($wg->getWeatherData());
+}
+
+$weather = $wg->getWeatherData();
+?>
+<table border="1">
+ <tr>
+ <td>
+ 国
+ </td>
+ <td>
+ <?php echo $weather['country'] ?>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ 街
+ </td>
+ <td>
+ <?php echo $weather['city'] ?>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ 観測地
+ </td>
+ <td>
+ <?php echo $weather['observation_location'] ?>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ 観測時間
+ </td>
+ <td>
+ <?php echo $weather['observation_time'] ?>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ 天気
+ </td>
+ <td>
+ <img src="<?php echo $weather['image'] ?>">
+ </td>
+ </tr>
+ <tr>
+ <td>
+ 気温
+ </td>
+ <td>
+ <?php echo $weather['temp'] ?>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ 湿度
+ </td>
+ <td>
+ <?php echo $weather['humidity'] ?>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ 風向
+ </td>
+ <td>
+ <?php echo $weather['wind_dir'] ?>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ 風速
+ </td>
+ <td>
+ <?php echo $weather['wind_speed'] ?>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ 気圧
+ </td>
+ <td>
+ <?php echo $weather['pressure'] ?>
+ </td>
+ </tr>
+</table>
+<?php
+
+$wg = new Services_WeatherUnderground('Test');
+if($wg->getWeatherData() === CITY_NOT_FOUND){
+ echo sprintf('指定された観測地は見つかりませんでした「Error Code 0x%02X」', $wg->getWeatherData());
+}else{
+ print_r($wg->getWeatherData());
+}
+
+/*
+foreach($wg->weather as $key => $val){
+ echo $key , " = " , $val."<br>";
+};
+*/
+
+?>
属性に変更があったパス: Services_WeatherUnderground/branches/0_1_5/WeatherUnderground
___________________________________________________________________
名前: svn:ignore
+ .project
.settings
.cache