1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use Budabot\Core\xml;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22: class WeatherController {
23:
24: 25: 26: 27:
28: public $moduleName;
29:
30:
31: public $text;
32:
33: 34: 35: 36:
37: public function weatherCommand($message, $channel, $sender, $sendto, $args) {
38: $location = $args[1];
39: $blob = '';
40:
41: $geolookup = "http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query=".urlencode($location);
42: $current = "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=".urlencode($location);
43: $forecast = "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=".urlencode($location);
44: $alerts = "http://api.wunderground.com/auto/wui/geo/AlertsXML/index.xml?query=".urlencode($location);
45:
46: $geolookup = file_get_contents($geolookup);
47:
48:
49: if (xml::spliceData($geolookup, "<wui_error>", "</wui_error>") != ""){
50: $sendto->reply("No information is available for <highlight>".$location."<end>.");
51: return;
52: }
53:
54: $locations = xml::spliceMultiData($geolookup, "<name>", "</name>");
55: if (count($locations) > 1){
56: $blob .= "Multiple hits for $location.\n\n";
57: foreach ($locations as $spot) {
58: $blob .= $this->text->makeChatcmd($spot, "/tell <myname> weather $spot")."\n";
59: }
60:
61: $msg = $this->text->makeBlob('Weather Locations', $blob);
62: $sendto->reply($msg);
63: return;
64: }
65:
66: $sendto->reply("Collecting data for <highlight>".$location."<end>.");
67:
68: $current = file_get_contents($current);
69: $forecast = file_get_contents($forecast);
70: $alerts = file_get_contents($alerts);
71:
72:
73: $updated = xml::spliceData($current, "<observation_time_rfc822>", "</observation_time_rfc822>");
74:
75: if ($updated == ", :: GMT") {
76: $sendto->reply("No information is available for <highlight>".$location."<end>.");
77: return;
78: }
79:
80: $credit = xml::spliceData($current, "<credit>", "</credit>");
81: $crediturl = xml::spliceData($current, "<credit_URL>", "</credit_URL>");
82: $observeLoc = xml::spliceData($current, "<observation_location>", "</observation_location>");
83: $fullLoc = xml::spliceData($observeLoc, "<full>", "</full>");
84: $country = xml::spliceData($observeLoc, "<country>", "</country>");
85: $lat = xml::spliceData($observeLoc, "<latitude>", "</latitude>");
86: $lon = xml::spliceData($observeLoc, "<longitude>", "</longitude>");
87: $elevation = xml::spliceData($observeLoc, "<elevation>", "</elevation>");
88: $weather = xml::spliceData($current, "<weather>", "</weather>");
89: $tempstr = xml::spliceData($current, "<temperature_string>", "</temperature_string>");
90: $humidity = xml::spliceData($current, "<relative_humidity>", "</relative_humidity>");
91: $windstr = xml::spliceData($current, "<wind_string>", "</wind_string>");
92: $windgust = xml::spliceData($current, "<wind_gust_mph>", "</wind_gust_mph>");
93: $pressurestr = xml::spliceData($current, "<pressure_string>", "</pressure_string>");
94: $dewstr = xml::spliceData($current, "<dewpoint_string>", "</dewpoint_string>");
95: $heatstr = xml::spliceData($current, "<heat_index_string>", "</heat_index_string>");
96: $windchillstr = xml::spliceData($current, "<windchill_string>", "</windchill_string>");
97: $visibilitymi = xml::spliceData($current, "<visibility_mi>", "</visibility_mi>");
98: $visibilitykm = xml::spliceData($current, "<visibility_km>", "</visibility_km>");
99:
100: $latlonstr = number_format(abs($lat), 1);
101: if (abs($lat) == $lat) {
102: $latlonstr .= "N ";
103: } else {
104: $latlonstr .= "S ";
105: }
106: $latlonstr .= number_format(abs($lon), 1);
107: if (abs($lon) == $lon) {
108: $latlonstr .= "E ";
109: } else {
110: $latlonstr .= "W ";
111: }
112: $latlonstr .= $this->text->makeChatcmd("Google Map", "/start http://maps.google.com/maps?q=$lat,$lon")." ";
113: $latlonstr .= $this->text->makeChatcmd("Wunder Map", "/start http://www.wunderground.com/wundermap/?lat=$lat&lon=$lon&zoom=10")."\n\n";
114: $blob .= "Credit: <highlight>".$this->text->makeChatcmd($credit, "/start $crediturl")."<end>\n";
115: $blob .= "Last Updated: <highlight>$updated<end>\n\n";
116: $blob .= "Location: <highlight>$fullLoc, $country<end>\n";
117: $blob .= "Lat/Lon: <highlight>$latlonstr<end>";
118:
119: $blob .= "Currently: <highlight>$tempstr, $weather<end>\n";
120: $blob .= "Humidity: <highlight>$humidity<end>\n";
121: $blob .= "Dew Point: <highlight>$dewstr<end>\n";
122: $blob .= "Wind: <highlight>$windstr<end>";
123: if ($windgust) {
124: $blob .= " (Gust:$windgust mph)\n";
125: } else {
126: $blob .= "\n";
127: }
128: if ($heatstr != "NA") {
129: $blob .= "Heat Index: <highlight>$heatstr<end>\n";
130: }
131: if ($windchillstr != "NA") {
132: $blob .= "Windchill: <highlight>$windchillstr<end>\n";
133: }
134: $blob .= "Pressure: <highlight>$pressurestr<end>\n";
135: $blob .= "Visibility: <highlight>$visibilitymi miles, $visibilitykm km<end>\n";
136: $blob .= "Elevation: <highlight>$elevation<end>\n";
137:
138:
139: $alertitems = xml::spliceMultiData($alerts, "<AlertItem>", "</AlertItem>");
140:
141: if (count($alertitems) == 0) {
142: $blob .= "\n<header2>Alerts:<end> None reported.\n\n";
143: } else {
144: forEach ($alertitems as $thisalert) {
145:
146: $blob .= "\n<header2>Alert: ".xml::spliceData($thisalert, "<description>", "</description>")."<end>\n\n";
147:
148: $start = strpos($thisalert, ">", strpos($thisalert, "<date epoch="))+1;
149: $end = strpos($thisalert, "<", $start);
150: $blob .= "Issued:<highlight>" . substr($thisalert, $start, $end - $start) . "<end>\n";
151:
152: $start = strpos($thisalert, ">", strpos($thisalert, "<expires epoch="))+1;
153: $end = strpos($thisalert, "<", $start);
154: $blob .= "Expires:<highlight>" . substr($thisalert, $start, $end - $start) . "<end>\n";
155: $blob .= xml::spliceData($thisalert, "<message>", "</message>")."";
156: }
157: }
158:
159:
160: $simpleforecast = xml::spliceData($forecast, "<simpleforecast>", "</simpleforecast>");
161: $forecastday = xml::spliceMultiData($simpleforecast, "<forecastday>", "</forecastday>");
162: if (count($forecastday)>0) {
163: forEach ($forecastday as $day) {
164:
165: if (!($condition = xml::spliceData($day, "<conditions>", "</conditions>"))) {
166: break;
167: }
168:
169: $low[0] = xml::spliceData($day, "<low>", "</low>");
170: $low[1] = xml::spliceData($low[0], "<fahrenheit>", "</fahrenheit");
171: $low[2] = xml::spliceData($low[0], "<celsius>", "</celsius");
172: $high[0] = xml::spliceData($day, "<high>", "</high>");
173: $high[1] = xml::spliceData($high[0], "<fahrenheit>", "</fahrenheit");
174: $high[2] = xml::spliceData($high[0], "<celsius>", "</celsius");
175: $pop = xml::spliceData($day, "<pop>", "</pop>");
176:
177: $blob .= xml::spliceData($day, "<weekday>", "</weekday>").": <highlight>$condition<end>";
178: if (0 == $pop) {
179: $blob .= "\n";
180: } else {
181: $blob .= " ($pop% Precip)\n";
182: }
183:
184: $blob .= "High: <highlight>";
185: $blob .= $high[1]."F";
186: $blob .= $high[2]."C<end> ";
187:
188: $blob .= "Low: <highlight>".$low[1]."F";
189: $blob .= $low[2]."C<end>\n\n";
190:
191: }
192: }
193:
194: $msg = $this->text->makeBlob('Weather: '.$location, $blob);
195:
196: $sendto->reply($msg);
197: }
198: }
199: