-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjweather.class.php
More file actions
194 lines (155 loc) · 5.79 KB
/
jweather.class.php
File metadata and controls
194 lines (155 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
class jweather {
private $debugCount = 1;
private $debug = true;
private $location;
private $unit;
private $imgfolder;
private $tmp;
private function debug($str, $title = 'Debug') {
if ($this->debug) {
echo '<br/><fieldset><legend>'.$title.' - '.$this->debugCount.'</legend><pre>';
print_r($str);
echo '<pre></fieldset><br/>';
$this->debugCount++;
}
}
private function show($ur, $content) {
if ($ur) {
$jsonStr = file_get_contents($ur);
}
else {
$jsonStr = $content;
}
$phpobj = json_decode($jsonStr);
$feed = $phpobj->query->results->channel;
$wd = $feed->wind->direction;
if ($wd>=348.75&&$wd<=360){$wd="N";}
if ($wd>=0&&$wd<11.25){$wd="N";}
if ($wd>=11.25&&$wd<33.75){$wd="NNE";}
if ($wd>=33.75&&$wd<56.25){$wd="NE";}
if ($wd>=56.25&&$wd<78.75){$wd="ENE";}
if ($wd>=78.75&&$wd<101.25){$wd="L";}
if ($wd>=101.25&&$wd<123.75){$wd="ESE";}
if ($wd>=123.75&&$wd<146.25){$wd="SE";}
if ($wd>=146.25&&$wd<168.75){$wd="SSE";}
if ($wd>=168.75&&$wd<191.25){$wd="S";}
if ($wd>=191.25&&$wd<213.75){$wd="SSO";}
if ($wd>=213.75&&$wd<236.25){$wd="SO";}
if ($wd>=236.25&&$wd<258.75){$wd="OSO";}
if ($wd>=258.75&&$wd<281.25){$wd="O";}
if ($wd>=281.25&&$wd<303.75){$wd="ONO";}
if ($wd>=303.75&&$wd<326.25){$wd="NO";}
if ($wd>=326.25&&$wd<348.75){$wd="NNO";}
$wf = $feed->item->forecast[0];
// Determine Day or Night
$pubdate = $feed->item->pubDate;
$n = strpos($pubdate, ":");
$pubdate = substr($pubdate,$n-2,8);
$tpb = strtotime($pubdate);
$tsr = strtotime($feed->astronomy->sunrise);
$tss = strtotime($feed->astronomy->sunset);
if ($tpb>$tsr && $tpb<$tss) {
$daynight = 'd';
} else {
$daynight = 'n';
}
$html = "<div class='clearfix'></div>";
foreach ($feed->item->forecast as $forecast) {
$html .= '<div class=" col-sm-2 text-center"><div class="weatherMain">' . $forecast->day.','.$forecast->date .'<br>';
$html .= '<div class="weatherItem" style="background-image: url(' . $this->imgfolder . $forecast->code . $daynight . '.png); background-repeat: no-repeat;">';
$html .= '<div class="weatherCity">' . $feed->location->city . '</div>';
$html .= '<div class="weatherTemp">' . $forecast->low . '°' . $feed->units->temperature . ', ' . $forecast->high . '°' . $feed->units->temperature . '</div>';
$html .= '<div class="weatherDesc">' . $feed->item->condition->text . '</div>';
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
}
echo $html;
return $jsonStr;
}
private function urlToPath($url)
{
$dir = $this->tmp . DIRECTORY_SEPARATOR . "YahooWeatherPHPAPI";
if (!is_dir($dir)) {
mkdir($dir);
}
$path = $dir . DIRECTORY_SEPARATOR . md5($url);
return $path;
}
/**
* @inheritdoc
*/
public function isCached($url)
{
$path = $this->urlToPath($url);
if (!file_exists($path) || filectime($path) + 100 < time()) {
echo "Weather data is NOT cached!\n";
return false;
}
echo "Weather data is cached!\n";
return true;
}
/**
* @inheritdoc
*/
public function getCached($url)
{
return file_get_contents($this->urlToPath($url));
}
/**
* @inheritdoc
*/
public function setCached($url, $content)
{
file_put_contents($this->urlToPath($url), $content);
}
/**
* @inheritdoc
*/
public function setTempPath($path)
{
if (!is_dir($path)) {
mkdir($path);
}
$this->tmp = $path;
}
public function __construct($location ,$unit = 'c', $imgfolder = 'img/', $debug = false) {
$this->tmp = sys_get_temp_dir();
try {
if (is_string($location)) {
$this->location = $location;
} else {
throw new Exception("Invalid argument - LOCATION should be a STRING, but it is an ".gettype($location).".");
}
if (is_string($unit)) {
$this->unit = $unit;
} else {
throw new Exception("Invalid argument - UNIT should be a STRING, but it is an ".gettype($unit).".");
}
if (is_string($imgfolder)) {
$this->imgfolder = $imgfolder;
} else {
throw new Exception("Invalid argument - IMG FOLDER should be a STRING, but it is an ".gettype($imgfolder).".");
}
if (is_bool($debug)) {
$this->debug = $debug;
} else {
throw new Exception("Invalid argument - DEBUG should be a BOOLEAN, but it is an ".gettype($debug).".");
}
$this->debug("Location: $location\nUnit: $unit\nDebug: $debug");
$query = "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='".$this->location."') and u='c'";
$url = 'http://query.yahooapis.com/v1/public/yql?q='.urlencode($query).'&rnd='.date('Y') . (date('n')-1) . date('w') . date('G') .'&format=json';
$this->setTempPath(__DIR__.'/temps');
if ($this->isCached($url)){
$jsonStr = $this->getCached($url);
$this->show(false,$jsonStr);
} else {
$jsonStr = $this->show($url,false);
$this->setCached($url,$jsonStr);
}
} catch(Exception $e) {
$this->debug($e->getMessage(),'Exception');
}
}
}