├── Baidu Weather.alfredworkflow
├── README.md
├── baidu_weather-2.png
├── baidu_weather.png
└── src
├── icon.png
├── info.plist
├── unknown.png
├── weather.php
├── workflows.php
├── 多云.png
├── 多云转晴.png
├── 大雨.png
├── 大雨转阴.png
├── 大雪.png
├── 小雨.png
├── 小雪.png
├── 晴.png
├── 晴转多云.png
├── 阴.png
├── 阴转晴.png
├── 阵雨.png
├── 阵雨转阴.png
├── 雨加冰雹.png
├── 雨夹雪.png
├── 雷阵雨.png
├── 雷阵雨转多云.png
└── 雾.png
/Baidu Weather.alfredworkflow:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/Baidu Weather.alfredworkflow
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | weather-workflow
2 | ================
3 |
4 | ###UPDATE
5 |
6 | - 2016-03-07
7 | @chxj1992 添加根据IP自动获取当前城市
8 |
9 | Weather forecast workflow using Baidu API
10 |
11 | 
12 |
13 | 
14 |
15 | ### 使用百度车联网API,速度快
16 |
17 | 百度车联网API:http://developer.baidu.com/map/carapi-7.htm
18 |
19 |
20 | ### 图标灵感来自
21 |
22 | 云朵:http://www.easyicon.net/572315-weather_freezing_rain_icon.html
23 |
24 | 太阳:http://www.zcool.com.cn/work/ZMTQ3MjE5Mg==.html#commentMaoDian
25 |
26 | 还有一些天气图标未做,后续会跟上
27 |
28 |
--------------------------------------------------------------------------------
/baidu_weather-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/baidu_weather-2.png
--------------------------------------------------------------------------------
/baidu_weather.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/baidu_weather.png
--------------------------------------------------------------------------------
/src/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/icon.png
--------------------------------------------------------------------------------
/src/info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | bundleid
6 | Baidu Weather
7 | connections
8 |
9 | createdby
10 | Wenson Smith
11 | description
12 | Weather forecast using Baidu API
13 | disabled
14 |
15 | name
16 | Baidu Weather
17 | objects
18 |
19 |
20 | config
21 |
22 | argumenttype
23 | 2
24 | escaping
25 | 63
26 | keyword
27 | tq
28 | runningsubtext
29 | 正在查询
30 | script
31 | require_once('weather.php');
32 |
33 | $weather = new Weather();
34 |
35 | //如果是制定城市查询,去掉这里的注释
36 | //$query = "{query}";
37 |
38 |
39 | //默认为北京市
40 | $query = "北京";
41 | $weather->getWeather($query);
42 |
43 |
44 | subtext
45 | 请输入城市名称
46 | title
47 | 百度天气预报
48 | type
49 | 1
50 | withspace
51 |
52 |
53 | type
54 | alfred.workflow.input.scriptfilter
55 | uid
56 | 32976DE4-B029-4681-A930-AF5904492FB0
57 | version
58 | 0
59 |
60 |
61 | readme
62 |
63 | uidata
64 |
65 | 32976DE4-B029-4681-A930-AF5904492FB0
66 |
67 | ypos
68 | 150
69 |
70 |
71 | webaddress
72 | www.seekbetter.me
73 |
74 |
75 |
--------------------------------------------------------------------------------
/src/unknown.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/unknown.png
--------------------------------------------------------------------------------
/src/weather.php:
--------------------------------------------------------------------------------
1 | workflows = new Workflows();
13 | }
14 |
15 | public function getWeather($query){
16 | if ( empty($query) ) {
17 | $query = $this->getDefaultCity();
18 | }
19 | $api = $this->url.$query;
20 | $res = $this->workflows->request($api);
21 | $res = json_decode( $res );
22 | if ($res->error === 0) {
23 | $forecast = $res->results[0]->weather_data;
24 | foreach($forecast as $key=>$value) {
25 | $date = mb_substr($value->date,0,6);
26 | $this->workflows->result( $key,
27 | $query,
28 | $query." ".$date." ".$value->weather,
29 | $value->wind.", 温度:".$value->temperature,
30 | $value->weather.".png");
31 | }
32 | }else{
33 | $this->workflows->result( '',
34 | '',
35 | '没查到呀',
36 | '没找到你所查询城市的天气',
37 | 'unknown.png' );
38 | }
39 |
40 | echo $this->workflows->toxml();
41 | }
42 |
43 | private function getDefaultCity(){
44 | $res = $this->workflows->request($this->locationUrl);
45 | $location = json_decode($res, true);
46 |
47 | return !empty($location['content']['address']) ? $location['content']['address'] : '北京市';
48 | }
49 |
50 | }
--------------------------------------------------------------------------------
/src/workflows.php:
--------------------------------------------------------------------------------
1 | path = exec('pwd');
31 | $this->home = exec('printf $HOME');
32 |
33 | if ( file_exists( 'info.plist' ) ):
34 | $this->bundle = $this->get( 'bundleid', 'info.plist' );
35 | endif;
36 |
37 | if ( !is_null( $bundleid ) ):
38 | $this->bundle = $bundleid;
39 | endif;
40 |
41 | $this->cache = $this->home. "/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/".$this->bundle;
42 | $this->data = $this->home. "/Library/Application Support/Alfred 2/Workflow Data/".$this->bundle;
43 |
44 | if ( !file_exists( $this->cache ) ):
45 | exec("mkdir '".$this->cache."'");
46 | endif;
47 |
48 | if ( !file_exists( $this->data ) ):
49 | exec("mkdir '".$this->data."'");
50 | endif;
51 |
52 | $this->results = array();
53 | }
54 |
55 | /**
56 | * Description:
57 | * Accepts no parameter and returns the value of the bundle id for the current workflow.
58 | * If no value is available, then false is returned.
59 | *
60 | * @param none
61 | * @return false if not available, bundle id value if available.
62 | */
63 | public function bundle()
64 | {
65 | if ( is_null( $this->bundle ) ):
66 | return false;
67 | else:
68 | return $this->bundle;
69 | endif;
70 | }
71 |
72 | /**
73 | * Description:
74 | * Accepts no parameter and returns the value of the path to the cache directory for your
75 | * workflow if it is available. Returns false if the value isn't available.
76 | *
77 | * @param none
78 | * @return false if not available, path to the cache directory for your workflow if available.
79 | */
80 | public function cache()
81 | {
82 | if ( is_null( $this->bundle ) ):
83 | return false;
84 | else:
85 | if ( is_null( $this->cache ) ):
86 | return false;
87 | else:
88 | return $this->cache;
89 | endif;
90 | endif;
91 | }
92 |
93 | /**
94 | * Description:
95 | * Accepts no parameter and returns the value of the path to the storage directory for your
96 | * workflow if it is available. Returns false if the value isn't available.
97 | *
98 | * @param none
99 | * @return false if not available, path to the storage directory for your workflow if available.
100 | */
101 | public function data()
102 | {
103 | if ( is_null( $this->bundle ) ):
104 | return false;
105 | else:
106 | if ( is_null( $this->data ) ):
107 | return false;
108 | else:
109 | return $this->data;
110 | endif;
111 | endif;
112 | }
113 |
114 | /**
115 | * Description:
116 | * Accepts no parameter and returns the value of the path to the current directory for your
117 | * workflow if it is available. Returns false if the value isn't available.
118 | *
119 | * @param none
120 | * @return false if not available, path to the current directory for your workflow if available.
121 | */
122 | public function path()
123 | {
124 | if ( is_null( $this->path ) ):
125 | return false;
126 | else:
127 | return $this->path;
128 | endif;
129 | }
130 |
131 | /**
132 | * Description:
133 | * Accepts no parameter and returns the value of the home path for the current user
134 | * Returns false if the value isn't available.
135 | *
136 | * @param none
137 | * @return false if not available, home path for the current user if available.
138 | */
139 | public function home()
140 | {
141 | if ( is_null( $this->home ) ):
142 | return false;
143 | else:
144 | return $this->home;
145 | endif;
146 | }
147 |
148 | /**
149 | * Description:
150 | * Returns an array of available result items
151 | *
152 | * @param none
153 | * @return array - list of result items
154 | */
155 | public function results()
156 | {
157 | return $this->results;
158 | }
159 |
160 | /**
161 | * Description:
162 | * Convert an associative array into XML format
163 | *
164 | * @param $a - An associative array to convert
165 | * @param $format - format of data being passed (json or array), defaults to array
166 | * @return - XML string representation of the array
167 | */
168 | public function toxml( $a=null, $format='array' ) {
169 |
170 | if ( $format == 'json' ):
171 | $a = json_decode( $a, TRUE );
172 | endif;
173 |
174 | if ( is_null( $a ) && !empty( $this->results ) ):
175 | $a = $this->results;
176 | elseif ( is_null( $a ) && empty( $this->results ) ):
177 | return false;
178 | endif;
179 |
180 | $items = new SimpleXMLElement(""); // Create new XML element
181 |
182 | foreach( $a as $b ): // Lop through each object in the array
183 | $c = $items->addChild( 'item' ); // Add a new 'item' element for each object
184 | $c_keys = array_keys( $b ); // Grab all the keys for that item
185 | foreach( $c_keys as $key ): // For each of those keys
186 | if ( $key == 'uid' ):
187 | $c->addAttribute( 'uid', $b[$key] );
188 | elseif ( $key == 'arg' ):
189 | $c->addAttribute( 'arg', $b[$key] );
190 | elseif ( $key == 'type' ):
191 | $c->addAttribute( 'type', $b[$key] );
192 | elseif ( $key == 'valid' ):
193 | if ( $b[$key] == 'yes' || $b[$key] == 'no' ):
194 | $c->addAttribute( 'valid', $b[$key] );
195 | endif;
196 | elseif ( $key == 'autocomplete' ):
197 | $c->addAttribute( 'autocomplete', $b[$key] );
198 | elseif ( $key == 'icon' ):
199 | if ( substr( $b[$key], 0, 9 ) == 'fileicon:' ):
200 | $val = substr( $b[$key], 9 );
201 | $c->$key = $val;
202 | $c->$key->addAttribute( 'type', 'fileicon' );
203 | elseif ( substr( $b[$key], 0, 9 ) == 'filetype:' ):
204 | $val = substr( $b[$key], 9 );
205 | $c->$key = $val;
206 | $c->$key->addAttribute( 'type', 'filetype' );
207 | else:
208 | $c->$key = $b[$key];
209 | endif;
210 | else:
211 | $c->$key = $b[$key];
212 | endif;
213 | endforeach;
214 | endforeach;
215 |
216 | return $items->asXML(); // Return XML string representation of the array
217 |
218 | }
219 |
220 | /**
221 | * Description:
222 | * Remove all items from an associative array that do not have a value
223 | *
224 | * @param $a - Associative array
225 | * @return bool
226 | */
227 | private function empty_filter( $a ) {
228 | if ( $a == '' || $a == null ): // if $a is empty or null
229 | return false; // return false, else, return true
230 | else:
231 | return true;
232 | endif;
233 | }
234 |
235 | /**
236 | * Description:
237 | * Save values to a specified plist. If the first parameter is an associative
238 | * array, then the second parameter becomes the plist file to save to. If the
239 | * first parameter is string, then it is assumed that the first parameter is
240 | * the label, the second parameter is the value, and the third parameter is
241 | * the plist file to save the data to.
242 | *
243 | * @param $a - associative array of values to save
244 | * @param $b - the value of the setting
245 | * @param $c - the plist to save the values into
246 | * @return string - execution output
247 | */
248 | public function set( $a=null, $b=null, $c=null )
249 | {
250 | if ( is_array( $a ) ):
251 | if ( file_exists( $b ) ):
252 | $b = $this->path."/".$b;
253 | elseif ( file_exists( $this->data."/".$b ) ):
254 | $b = $this->data."/".$b;
255 | elseif ( file_exists( $this->cache."/".$b ) ):
256 | $b = $this->cache."/".$b;
257 | else:
258 | $b = $this->data."/".$b;
259 | endif;
260 | else:
261 | if ( file_exists( $c ) ):
262 | $c = $this->path."/".$c;
263 | elseif ( file_exists( $this->data."/".$c ) ):
264 | $c = $this->data."/".$c;
265 | elseif ( file_exists( $this->cache."/".$c ) ):
266 | $c = $this->cache."/".$c;
267 | else:
268 | $c = $this->data."/".$c;
269 | endif;
270 | endif;
271 |
272 | if ( is_array( $a ) ):
273 | foreach( $a as $k => $v ):
274 | exec( 'defaults write "'. $b .'" '. $k .' "'. $v .'"');
275 | endforeach;
276 | else:
277 | exec( 'defaults write "'. $c .'" '. $a .' "'. $b .'"');
278 | endif;
279 | }
280 |
281 | /**
282 | * Description:
283 | * Read a value from the specified plist
284 | *
285 | * @param $a - the value to read
286 | * @param $b - plist to read the values from
287 | * @return bool false if not found, string if found
288 | */
289 | public function get( $a, $b ) {
290 |
291 | if ( file_exists( $b ) ):
292 | $b = $this->path."/".$b;
293 | elseif ( file_exists( $this->data."/".$b ) ):
294 | $b = $this->data."/".$b;
295 | elseif ( file_exists( $this->cache."/".$b ) ):
296 | $b = $this->cache."/".$b;
297 | else:
298 | return false;
299 | endif;
300 |
301 | exec( 'defaults read "'. $b .'" '.$a, $out ); // Execute system call to read plist value
302 |
303 | if ( $out == "" ):
304 | return false;
305 | endif;
306 |
307 | $out = $out[0];
308 | return $out; // Return item value
309 | }
310 |
311 | /**
312 | * Description:
313 | * Read data from a remote file/url, essentially a shortcut for curl
314 | *
315 | * @param $url - URL to request
316 | * @param $options - Array of curl options
317 | * @return result from curl_exec
318 | */
319 | public function request( $url=null, $options=null )
320 | {
321 | if ( is_null( $url ) ):
322 | return false;
323 | endif;
324 |
325 | $defaults = array( // Create a list of default curl options
326 | CURLOPT_RETURNTRANSFER => true, // Returns the result as a string
327 | CURLOPT_URL => $url, // Sets the url to request
328 | CURLOPT_FRESH_CONNECT => true
329 | );
330 |
331 | if ( $options ):
332 | foreach( $options as $k => $v ):
333 | $defaults[$k] = $v;
334 | endforeach;
335 | endif;
336 |
337 | array_filter( $defaults, // Filter out empty options from the array
338 | array( $this, 'empty_filter' ) );
339 |
340 | $ch = curl_init(); // Init new curl object
341 | curl_setopt_array( $ch, $defaults ); // Set curl options
342 | $out = curl_exec( $ch ); // Request remote data
343 | $err = curl_error( $ch );
344 | curl_close( $ch ); // End curl request
345 |
346 | if ( $err ):
347 | return $err;
348 | else:
349 | return $out;
350 | endif;
351 | }
352 |
353 | /**
354 | * Description:
355 | * Allows searching the local hard drive using mdfind
356 | *
357 | * @param $query - search string
358 | * @return array - array of search results
359 | */
360 | public function mdfind( $query )
361 | {
362 | exec('mdfind "'.$query.'"', $results);
363 | return $results;
364 | }
365 |
366 | /**
367 | * Description:
368 | * Accepts data and a string file name to store data to local file as cache
369 | *
370 | * @param array - data to save to file
371 | * @param file - filename to write the cache data to
372 | * @return none
373 | */
374 | public function write( $a, $b )
375 | {
376 | if ( file_exists( $b ) ):
377 | $b = $this->path."/".$b;
378 | elseif ( file_exists( $this->data."/".$b ) ):
379 | $b = $this->data."/".$b;
380 | elseif ( file_exists( $this->cache."/".$b ) ):
381 | $b = $this->cache."/".$b;
382 | else:
383 | $b = $this->data."/".$b;
384 | endif;
385 |
386 | if ( is_array( $a ) ):
387 | $a = json_encode( $a );
388 | file_put_contents( $b, $a );
389 | return true;
390 | elseif ( is_string( $a ) ):
391 | file_put_contents( $b, $a );
392 | return true;
393 | else:
394 | return false;
395 | endif;
396 | }
397 |
398 | /**
399 | * Description:
400 | * Returns data from a local cache file
401 | *
402 | * @param file - filename to read the cache data from
403 | * @return false if the file cannot be found, the file data if found. If the file
404 | * format is json encoded, then a json object is returned.
405 | */
406 | public function read( $a )
407 | {
408 | if ( file_exists( $a ) ):
409 | $a = $this->path."/".$a;
410 | elseif ( file_exists( $this->data."/".$a ) ):
411 | $a = $this->data."/".$a;
412 | elseif ( file_exists( $this->cache."/".$a ) ):
413 | $a = $this->cache."/".$a;
414 | else:
415 | return false;
416 | endif;
417 |
418 | $out = file_get_contents( $a );
419 | if ( !is_null( json_decode( $out ) ) ):
420 | $out = json_decode( $out );
421 | endif;
422 |
423 | return $out;
424 | }
425 |
426 | /**
427 | * Description:
428 | * Helper function that just makes it easier to pass values into a function
429 | * and create an array result to be passed back to Alfred
430 | *
431 | * @param $uid - the uid of the result, should be unique
432 | * @param $arg - the argument that will be passed on
433 | * @param $title - The title of the result item
434 | * @param $sub - The subtitle text for the result item
435 | * @param $icon - the icon to use for the result item
436 | * @param $valid - sets whether the result item can be actioned
437 | * @param $auto - the autocomplete value for the result item
438 | * @return array - array item to be passed back to Alfred
439 | */
440 | public function result( $uid, $arg, $title, $sub, $icon, $valid='yes', $auto=null, $type=null )
441 | {
442 | $temp = array(
443 | 'uid' => $uid,
444 | 'arg' => $arg,
445 | 'title' => $title,
446 | 'subtitle' => $sub,
447 | 'icon' => $icon,
448 | 'valid' => $valid,
449 | 'autocomplete' => $auto,
450 | 'type' => $type
451 | );
452 |
453 | if ( is_null( $type ) ):
454 | unset( $temp['type'] );
455 | endif;
456 |
457 | array_push( $this->results, $temp );
458 |
459 | return $temp;
460 | }
461 |
462 | }
--------------------------------------------------------------------------------
/src/多云.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/多云.png
--------------------------------------------------------------------------------
/src/多云转晴.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/多云转晴.png
--------------------------------------------------------------------------------
/src/大雨.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/大雨.png
--------------------------------------------------------------------------------
/src/大雨转阴.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/大雨转阴.png
--------------------------------------------------------------------------------
/src/大雪.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/大雪.png
--------------------------------------------------------------------------------
/src/小雨.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/小雨.png
--------------------------------------------------------------------------------
/src/小雪.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/小雪.png
--------------------------------------------------------------------------------
/src/晴.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/晴.png
--------------------------------------------------------------------------------
/src/晴转多云.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/晴转多云.png
--------------------------------------------------------------------------------
/src/阴.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/阴.png
--------------------------------------------------------------------------------
/src/阴转晴.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/阴转晴.png
--------------------------------------------------------------------------------
/src/阵雨.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/阵雨.png
--------------------------------------------------------------------------------
/src/阵雨转阴.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/阵雨转阴.png
--------------------------------------------------------------------------------
/src/雨加冰雹.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/雨加冰雹.png
--------------------------------------------------------------------------------
/src/雨夹雪.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/雨夹雪.png
--------------------------------------------------------------------------------
/src/雷阵雨.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/雷阵雨.png
--------------------------------------------------------------------------------
/src/雷阵雨转多云.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/雷阵雨转多云.png
--------------------------------------------------------------------------------
/src/雾.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wensonsmith/weather-workflow/aa570a7fc3dae7666228b10ce2ab9403cd9d3e94/src/雾.png
--------------------------------------------------------------------------------