|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* OpenWeatherMap provider for the Weather plugin for Phergie |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/chrismou/phergie-irc-plugin-react-weather for the canonical source repository |
|
7
|
|
|
* @copyright Copyright (c) 2014 Chris Chrisostomou (http://mou.me) |
|
8
|
|
|
* @license http://phergie.org/license New BSD License |
|
9
|
|
|
* @package Chrismou\Phergie\Plugin\Weather |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Chrismou\Phergie\Plugin\Weather\Provider; |
|
13
|
|
|
|
|
14
|
|
|
use Phergie\Irc\Plugin\React\Command\CommandEvent as Event; |
|
15
|
|
|
|
|
16
|
|
|
class Wunderground implements WeatherProviderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $apiUrl = 'http://api.wunderground.com/api'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $appId = ""; |
|
27
|
|
|
|
|
28
|
1 |
|
public function __construct(array $config = array()) |
|
29
|
|
|
{ |
|
30
|
1 |
|
if (isset($config['appId'])) { |
|
31
|
1 |
|
$this->appId = $config['appId']; |
|
32
|
1 |
|
} |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Return the url for the API request |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function getApiRequestUrl(Event $event) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$params = $event->getCustomParams(); |
|
44
|
|
|
|
|
45
|
|
|
// Final parameter should be the country |
|
46
|
1 |
|
$country = $params[count($params) - 1]; |
|
47
|
|
|
// Remove the final paramater |
|
48
|
1 |
|
unset($params[count($params) - 1]); |
|
49
|
|
|
// Merge the remainder of the supplied params and remove disallowed punctuation |
|
50
|
1 |
|
$place = trim(implode("_", preg_replace('/[^\da-z\ ]/i', '', $params))); |
|
51
|
1 |
|
return sprintf("%s/%s/conditions/q/%s/%s.json", $this->apiUrl, $this->appId, $country, $place); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Validate the provided parameters |
|
56
|
|
|
* The plugin requires at least one parameter (in most cases, this will be a location string) |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $params |
|
59
|
|
|
* @return boolean |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function validateParams(array $params) |
|
62
|
|
|
{ |
|
63
|
1 |
|
return (count($params) >= 2) ? true : false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns an array of lines to send back to IRC when the http request is successful |
|
68
|
|
|
* |
|
69
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
|
70
|
|
|
* @param string $apiResponse |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function getSuccessLines(Event $event, $apiResponse) |
|
74
|
|
|
{ |
|
75
|
1 |
|
$data = json_decode($apiResponse); |
|
76
|
1 |
|
if (isset($data->current_observation)) { |
|
77
|
1 |
|
$data = $data->current_observation; |
|
78
|
|
|
return array( |
|
79
|
1 |
|
sprintf( |
|
80
|
1 |
|
"%s | %s | Temp: %dC | Humidity: %s | %s", |
|
81
|
1 |
|
$data->display_location->full, |
|
82
|
1 |
|
$data->weather, |
|
83
|
1 |
|
round($data->temp_c), |
|
84
|
1 |
|
$data->relative_humidity, |
|
85
|
1 |
|
$data->forecast_url |
|
86
|
1 |
|
) |
|
87
|
1 |
|
); |
|
88
|
|
|
} else { |
|
89
|
1 |
|
return $this->getNoResultsLines($event, $apiResponse); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Return an array of lines to send back to IRC when there are no results |
|
95
|
|
|
* |
|
96
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
|
97
|
|
|
* @param string $apiResponse |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function getNoResultsLines(Event $event, $apiResponse) |
|
101
|
|
|
{ |
|
102
|
1 |
|
return array('No weather found for this location'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Return an array of lines to send back to IRC when the request fails |
|
107
|
|
|
* |
|
108
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
|
109
|
|
|
* @param string $apiError |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function getRejectLines(Event $event, $apiError) |
|
113
|
|
|
{ |
|
114
|
1 |
|
return array('Something went wrong... ಠ_ಠ'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns an array of lines for the help response |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function getHelpLines() |
|
123
|
|
|
{ |
|
124
|
|
|
return array( |
|
125
|
1 |
|
'Usage: weather [place] [country]', |
|
126
|
1 |
|
'[place] - town, city, etc. Can be multiple words', |
|
127
|
1 |
|
'[country] - full name or country code (uk, us, etc)', |
|
128
|
|
|
'Instructs the bot to query Weather Undergound for info for the specified location' |
|
129
|
1 |
|
); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|