-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupdate.php
More file actions
148 lines (124 loc) · 5.5 KB
/
update.php
File metadata and controls
148 lines (124 loc) · 5.5 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
<?php
// do not forget to setup the cron-job in order to make this process automated.
// Required data from Weather Underground
// you can define variables here or via URL, make sure to comment out or delete hardcoded variables to get them from URL
// start of hardcoded variables
// API key https://www.wunderground.com/weather/api/
$wuAPI = "ebEXAMPLEab"; // change contents, keep quotes
$wuID = "Kexample37";
// Data needed from PWS weather
$pwsID = "FexampleER"; // change contents, keep quotes
// $pwsID = filter_var("FexampleER", FILTER_SANITIZE_STRING); // Example of sanitized variable, worth trying if you run into errors
$psw = "1ExAmPlE."; // seems to dislike commas, try simplier password in case you get ID/pass error (periods "." are ok)
// End of hardcoded variables
// get missing data from URL (if available)
if(!isset($wuAPI))
$wuAPI = filter_input(INPUT_GET,"wuAPI",FILTER_SANITIZE_STRING);
if(!isset($wuID))
$wuID = filter_input(INPUT_GET,"wuID",FILTER_SANITIZE_STRING);
if(!isset($pwsID))
$pwsID = filter_input(INPUT_GET,"pwsID",FILTER_SANITIZE_STRING);
if(!isset($psw))
$psw = filter_input(INPUT_GET,"psw",FILTER_SANITIZE_STRING);
// start of code
if(isset($wuAPI) && isset($wuID) && isset($pwsID) && isset($psw)){
$wuData = file_get_contents('http://api.wunderground.com/api/' . $wuAPI . '/conditions/q/pws:' .$wuID . '.json');
$data = json_decode($wuData,true);
if(isset($data['current_observation'])){
$date = new DateTime("@" . $data['current_observation']['observation_epoch']);
$delta = time() - $data['current_observation']['observation_epoch'];
if($delta > 2000){ // to get rid of old data spikes
echo("The data from ".$delta." seconds ago was too old for trasfer, will retry on next attempt");
} else {
$url = "http://www.pwsweather.com/pwsupdate/pwsupdate.php?ID=". $pwsID ."&PASSWORD=". urlencode($psw) ."&dateutc=" . $date->format('Y-m-d+H:i:s') .
($data['current_observation']['wind_degrees'] >= 0 ? "&winddir=" . $data['current_observation']['wind_degrees'] : '' ) .
($data['current_observation']['wind_mph'] >= 0 ? "&windspeedmph=" . $data['current_observation']['wind_mph'] : '' ) .
($data['current_observation']['wind_gust_mph'] >= 0 ? "&windgustmph=". $data['current_observation']['wind_gust_mph'] : "" ) .
// I would be impressed if anyone recorded temperatures close to absolute zero.
($data['current_observation']['temp_f'] > -459 ? "&tempf=" . $data['current_observation']['temp_f'] : "" ) .
($data['current_observation']['precip_1hr_in'] >= 0 ? "&rainin=" . $data['current_observation']['precip_1hr_in'] : "" ) .
($data['current_observation']['precip_today_in'] >= 0 ? "&dailyrainin=" . $data['current_observation']['precip_today_in'] : "" ) .
($data['current_observation']['pressure_in'] >= 0 ? "&baromin=" . $data['current_observation']['pressure_in'] : "" ) .
($data['current_observation']['dewpoint_f'] > -100 ? "&dewptf=" . $data['current_observation']['dewpoint_f'] : "" ) .
(substr($data['current_observation']['relative_humidity'], 0, 1) <> '-' ? "&humidity=" . substr($data['current_observation']['relative_humidity'], 0, -1) : "" ) .
"&softwaretype=ebviaphpV0.3&action=updateraw";
$pwsdata = file_get_contents($url);
$results = explode("\n", $pwsdata);
switch ($results[6]){ // 6 represents the 7th line (count starts at 0) which carries useful information
case "ERROR: Not a vailid Station ID":
echo (
'<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PWSweather Error</title>
</head>
<body>
<h1>We got an error from PWS weather:</h1>
<p>Your PWS weather ID (pwsID) appears to be invalid</p>
</body> </html>');
break;
case "ERROR: Not a vailid Station ID/Password":
echo (
'<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PWSweather Error</title>
</head>
<body>
<h1>We got an error from PWS weather:</h1>
<p>Your PWS account password (psw) appears to be invalid</p>
</body> </html>');
break;
case "Data Logged and posted in METAR mirror.":
echo("The latest data from ".$delta." seconds ago was transfered to PWS weather station " . $pwsID);
break;
default:
echo $pwsdata;
break;
}
}
} else {
//http_response_code(400); // bad request
// we got an error
if(isset($data['response']['error'])){
echo (
'<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Weather Underground Error</title>
</head>
<body>
<h1>We got an error from Weather Underground:</h1><p>');
switch($data['response']['error']['type']){
case "keynotfound":
echo('Your Weather Underground API key (wuAPI) appears to be invalid');
break;
case "Station:OFFLINE":
echo('Your Weather Underground Station ID (wuID) appears to be invalid');
break;
default:
echo('This appears to be a temporary error, please try again later</p>');
echo("<p>Exact Error type: " . $data['response']['error']['type'] . "</p>");
echo("<p>Which means: " . $data['response']['error']['description']);
break;
}
echo ('</p></body> </html>');
}
}
} else {
echo (
'<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Insuficient Data</title>
</head>
<body>
<p>Not enough URL or Hardcoded parameters</p>
</body>
</html>');
}
?>