|
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) 2016 Chris Chrisostomou (https://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 OpenWeatherMap implements WeatherProviderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $apiUrl = 'http://api.openweathermap.org/data/2.5/weather'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $appId = ""; |
|
27
|
|
|
|
|
28
|
2 |
|
public function __construct(array $config = array()) |
|
29
|
|
|
{ |
|
30
|
2 |
|
if (isset($config['appId'])) { |
|
31
|
1 |
|
$this->appId = $config['appId']; |
|
32
|
1 |
|
} |
|
33
|
2 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Return the url for the API request |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function getApiRequestUrl(Event $event) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$params = $event->getCustomParams(); |
|
45
|
1 |
|
$query = trim(implode(" ", $params)); |
|
46
|
|
|
|
|
47
|
|
|
$querystringParams = array( |
|
48
|
1 |
|
'q' => $query, |
|
49
|
1 |
|
'appid' => $this->appId |
|
50
|
1 |
|
); |
|
51
|
|
|
|
|
52
|
1 |
|
return sprintf("%s?%s", $this->apiUrl, http_build_query($querystringParams)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Validate the provided parameters |
|
57
|
|
|
* The plugin requires at least one parameter (in most cases, this will be a location string) |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $params |
|
60
|
|
|
* |
|
61
|
|
|
* @return boolean |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function validateParams(array $params) |
|
64
|
|
|
{ |
|
65
|
1 |
|
return (count($params)) ? true : false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns an array of lines to send back to IRC when the http request is successful |
|
70
|
|
|
* |
|
71
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
|
72
|
|
|
* @param string $apiResponse |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function getSuccessLines(Event $event, $apiResponse) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$data = json_decode($apiResponse); |
|
79
|
1 |
|
if (isset($data->name) && $data->name) { |
|
80
|
|
|
return array( |
|
81
|
1 |
|
sprintf( |
|
82
|
1 |
|
"%s, %s | %s | Temp: %dC | Humidity: %s%% | Sunrise: %s | Sunset: %s", |
|
83
|
1 |
|
$data->name, |
|
84
|
1 |
|
$data->sys->country, |
|
85
|
1 |
|
$data->weather[0]->main, |
|
86
|
1 |
|
round($data->main->temp - 273.15), |
|
87
|
1 |
|
$data->main->humidity, |
|
88
|
1 |
|
date("H:i:s", $data->sys->sunrise), |
|
89
|
1 |
|
date("H:i:s", $data->sys->sunset) |
|
90
|
1 |
|
) |
|
91
|
1 |
|
); |
|
92
|
|
|
} else { |
|
93
|
1 |
|
return $this->getNoResultsLines($event, $apiResponse); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Return an array of lines to send back to IRC when there are no results |
|
99
|
|
|
* |
|
100
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
|
101
|
|
|
* @param string $apiResponse |
|
102
|
|
|
* |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function getNoResultsLines(Event $event, $apiResponse) |
|
106
|
|
|
{ |
|
107
|
1 |
|
return array('No weather data found for this location'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Return an array of lines to send back to IRC when the request fails |
|
112
|
|
|
* |
|
113
|
|
|
* @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
|
114
|
|
|
* @param string $apiError |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function getRejectLines(Event $event, $apiError) |
|
119
|
|
|
{ |
|
120
|
1 |
|
return array('Something went wrong... ಠ_ಠ'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns an array of lines for the help response |
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function getHelpLines() |
|
129
|
|
|
{ |
|
130
|
|
|
return array( |
|
131
|
1 |
|
'Usage: weather [place] [country]', |
|
132
|
1 |
|
'[place] - address, town, city, zip code, etc. Can be multiple words', |
|
133
|
|
|
'Instructs the bot to query OpenWeatherMap for weather info for the specified location' |
|
134
|
1 |
|
); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|