-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.tcl
1449 lines (1312 loc) · 63.3 KB
/
weather.tcl
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#---------------------------------------------------------------------#
# incith:weather v2.9b#
# #
# retrieves weather and forecast data from www.wunderground.com #
# tested on Eggdrop v1.6.18 #
# #
# Usage: #
# .chanset #channel +weather #
# !weather <search> #
# returns the current conditions for the city found #
# !forecast <search> #
# returns a 3-day forecast for the city found #
# !sky <location> #
# returns the astronomy data for the city found #
# #
# Forum Support: #
# http://forum.egghelp.org/viewtopic.php?t=10466 #
# #
# ChangeLog: #
# 2.0: script re-created. more updates to come. #
# 2.1: option for update time to be displayed in 24-hour (default) #
# - this removes AM/PM and the timezone if enabled #
# more informative locations #
# uses mobile.wunderground.com for forecast data if it has to #
# 2.2: fix for updated time. #
# 2.3: can use !w/!fc -set <location> to store a default, if you're #
# an added member of the bots userlist (only way for now). #
# small fix for updated time on some locations preventing #
# results from being sent, igloolik was one such city. #
# 2.4: added new variable 'public_to_private' to control whether #
# replies are sent to the channel or to the user requesting. #
# if it is set, the user will get /msg'd with the results, or #
# they will get /notice'd if variable 'notices' is set to 1. #
# 2.5a: fixed high/low showing yesterdays data sometimes.. also if #
# todays forecast is not available it will try mobiles. #
# 2.6: moved error checking higher up in fetch_html, fixes london. #
# convert EEST to EET to as Tcl does not recognize EEST (?) #
# this fixes any location with EEST. #
# 2.6a: there should never be a clock issue again as timezones are #
# no longer being used in calculations, at all. #
# 2.7: astronomy is back, !sky <location> #
# multiple results problem fixed (hull, united kingdom). #
# variable weather_format controls which weather data's sent. #
# forecast_format controls which forecast data's sent. #
# pressure has been added back to !weather (%w9%). #
# fixed a bug in the way i was grabbing wind direction #
# basically, it wasn't showing 'Calm' when it actually was. #
# <country> searches (!w malaysia for example) will now return #
# multiple results correctly. #
# wunderground seems to have gone to a 3-day forecast for most #
# locations, so I've modified the script accordingly. #
# tries to fetch the current local time and date for accurate #
# high/low display. #
# 2.7a: oops, small fix for 'unable to convert date time string' #
# 2.8: fixed multiple results (especially !w india). #
# this works now by stripping locations without temperatures #
# no more 'cannot read (update_time)' in multiple results. #
# 2.8a: more multiple results fixes. #
# 2.8b: more multiple results fixes. woot. #
# 2.8c: fixed conditions/location/todays_day/UV, and astronomy. #
# 2.8d: more fixes for forecast and location. #
# 2.8e: wunderground changed something that broke forecast; fixed. #
# 2.8f: fixed a bug for the location 'unknown' and similar results. #
# changed flood protection to ignore +f flagged users. #
# wunderground removed <nobr> tags breaking a lot of things. #
# fixed locations that have blank sunrise/sunset data. #
# 2.8g: removed <nobr> tags from html to fix things breaking again. #
# 2.8h: fixed multiple results (they changed the layout). again. #
# 2.8i: it's not my fault, honest! more multiple results fixes. #
# 2.8j: fixes: temp, dew point, wind speed, better mobile backup. #
# 2.8k: added longitude & latitude back (optional, show_lat_lon var) #
# major improvements to multiple results. #
# forecast will show the % chance of precipitation. #
# script was fetching mobile more often than it needed, fixed. #
# 2.8L: forecast modified for unknown change, capital L for LOLS! #
# 2.8m: more small fixes and updates. #
# 2.8n: windchill fix. #
# 2.8o: they made units cookie based, fix to compensate. #
# 2.8p: fix for public_to_private not working. #
# 2.8q: Correct missing celsius information, current weather only #
# returns farenheight. Script now converts on it's own. #
# 2.8r: Added "nickname" prefixes to commands optional. default is on#
# 2.9: Correct missing celsius info for dewpoint. same fix as temp. #
# 2.9a: Corrected the way I was removing extraneous decimel points #
# and ending 0's. #
# 2.9b: Corrected cruft within updated time. #
# #
# Contact: #
# E-mail ([email protected]) cleanups, ideas, bugs, etc., to me. #
# #
# TODO: #
# - locales for output (languages) #
# + fix multiple results, take best match, etc #
# - merge _handlers, allow all %var% in all _formats, max_results #
# - limit size of output, check length of variables? #
# #
# LICENSE: #
# This code comes with ABSOLUTELY NO WARRANTY. #
# #
# This program is free software; you can redistribute it and/or #
# modify it under the terms of the GNU General Public License as #
# published by the Free Software Foundation; either version 2 of #
# the License, or (at your option) any later version. #
# later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# See the GNU General Public License for more details. #
# (http://www.gnu.org/copyleft/library.txt) #
# #
# Copyleft (C) 2005-10, Jordan #
# [email protected] (paypal donations accepted) #
# irc.freenode.net / #incith #
#---------------------------------------------------------------------#
package require http 2.3
setudef flag weather
namespace eval incith {
namespace eval weather {
# the bind prefix/command char ("!" or ".", etc)
variable command_char "."
# binds ("one two three") as many as you need
variable weather_binds "wz"
variable forecast_binds "fc forecast wzf"
variable astronomy_binds "sky astronomy"
variable time_binds "time"
# if you want to change the order or remove some items that are sent
# w0 - location, w1 - updated, w2 - conditions, w3 - temperature, w4 - windchill
# w5 - high/low, w6 - humidity, w7 - dew point, w8 - UV index, w9 - pressure
# w10 - wind, nick - nickname of user triggering
variable weather_format "%nick% %w0% %w1% %w2% %w3% %w4% %w5% %w8% %w6% %w7% %w9% %w10%"
# some control over the forecast format, too
# f0 - location, f1 - updated, f2 - first day high/low, f3 - second day high/low
# f4 - third day, f5 - fourth day, f6 - fifth day
# nick - nickname of user triggering
variable forecast_format "%nick% %f0% %f1% %f2% %f3% %f4% %f5% %f6%"
# some control over the time format, too
# w0 - location, w1 - local time
variable time_format "%nick% %w0% %w1%"
# show latitude and longitude for locations?
variable show_lat_lon 1
# symbol to put beside temperature numbers
variable degree_symbol "°"
# display celcius temperatures first, and fahrenheit in ()
variable celcius_first 1
# allow binds to be used in /msg's to the bot?
variable private_messages 1
# send public/channel replies to the user instead of to the channel?
variable public_to_private 1
# send replies as notices instead of messages?
variable notices 0
# this will be used to seperate items (Foo; Bar; Baz)
# setting this to "\n" will have *everything* on a new line
variable seperator "; "
# use this if you would only like to break results into 3 new lines
variable seperate_lines 0
# convert update time into 24-hour time
variable 24hour_time 1
# weather granularity
# Enter the amount of digits after the decimal point you want
variable granularity 1
# make use of bolding where appropriate?
variable bold 1
# the maximum length a line sent should be
variable split_length "403"
# if you're using a proxy, enter it here (proxy.example.com:3128)
variable proxy ""
# how long (in seconds) before the http request times out?
variable timeout 15
# number of minute(s) to ignore flooders, 0 to disable flood protection
variable ignore 1
# prefix results with nickname?
variable usenicks 1
# how many requests in how many seconds is considered flooding?
# by default, this allows 3 queries in 10 seconds, the 4th being ignored
# and ignoring requests for 'variable ignore' minutes
variable flood 4:10
}
}
# script begings
namespace eval incith {
namespace eval weather {
variable version "incith:weather-2.9b"
variable debug 0
}
}
# bind the binds
foreach bind [split ${incith::weather::weather_binds} " "] {
# public message binds
bind pub -|- "${incith::weather::command_char}${bind}" incith::weather::weather_handler
# private message binds
if {${incith::weather::private_messages} >= 1} {
bind msg -|- "${incith::weather::command_char}${bind}" incith::weather::weather_handler
}
}
foreach bind [split ${incith::weather::forecast_binds} " "] {
# public message binds
bind pub -|- "${incith::weather::command_char}${bind}" incith::weather::forecast_handler
# private message binds
if {${incith::weather::private_messages} >= 1} {
bind msg -|- "${incith::weather::command_char}${bind}" incith::weather::forecast_handler
}
}
foreach bind [split ${incith::weather::astronomy_binds} " "] {
# public message binds
bind pub -|- "${incith::weather::command_char}${bind}" incith::weather::astronomy_handler
# private message binds
if {${incith::weather::private_messages} >= 1} {
bind msg -|- "${incith::weather::command_char}${bind}" incith::weather::astronomy_handler
}
}
foreach bind [split ${incith::weather::time_binds} " "] {
# public message binds
bind pub -|- "${incith::weather::command_char}${bind}" incith::weather::time_handler
# private message binds
if {${incith::weather::private_messages} >= 1} {
bind msg -|- "${incith::weather::command_char}${bind}" incith::weather::time_handler
}
}
namespace eval incith {
namespace eval weather {
# [custom_format] : converts a string to the desired output
#
proc custom_format {htmla type input nick} {
array set html $htmla
# for seperate line results option
if {${incith::weather::seperate_lines} >= 1} {
set alternate_sep "\n"
} else {
set alternate_sep ${incith::weather::seperator}
}
# spaces make things look wrong
regsub -all {\s+} $input {} input
if {$::incith::weather::usenicks > 0 } {
set input [string map [list %nick% "\002$nick's\002 $type request"] $input]
} else {
set input [string map [list %nick% ""] $input]
}
if {$type == "weather"} {
if {[info exists html(location)]} {
if {[info exists html(latitude)] && [info exists html(longitude)] && $incith::weather::show_lat_lon >= 1} {
set input [string map "{%w0%} {${incith::weather::seperator}[ibold $html(location)] ($html(latitude)\/$html(longitude))}" $input]
} else {
set input [string map "{%w0%} {${incith::weather::seperator}[ibold $html(location)]}" $input]
}
} else {
set input [string map "{%w0%} {}" $input]
}
if {[info exists html(update_time)] && [info exists html(update_date)]} {
set input [string map "{%w1%} {${incith::weather::seperator}[ibold "Updated:"] $html(update_time) $html(update_timezone) ($html(update_date))}" $input]
} else {
set input [string map "{%w1%} {}" $input]
}
if {[info exists html(conditions)] && [string length $html(conditions)] >= 1} {
set input [string map "{%w2%} {${incith::weather::seperator}[ibold "Conditions:"] $html(conditions)}" $input]
} else {
set input [string map "{%w2%} {}" $input]
}
if {[info exists html(tempf)] && [info exists html(tempc)]} {
set input [string map "{%w3%} {${alternate_sep}[ibold "Temperature:"] [i2fac $html(tempf) $html(tempc)]}" $input]
} else {
set input [string map "{%w3%} {}" $input]
}
if {[info exists html(windchillf)] && [info exists html(windchillc)]} {
set input [string map "{%w4%} {${incith::weather::seperator}[ibold "Windchill:"] [i2fac $html(windchillf) $html(windchillc)]}" $input]
} else {
set input [string map "{%w4%} {}" $input]
}
# compare the date the weather was updated to output todays high/low
if {([info exists html(fc1d)]) && $html(fc1d) == $html(todays_day)} {
set input [string map "%w5% {${incith::weather::seperator}[ibold "High/Low:"] [if2c $html(fc1hf) $html(fc1lf)]}" $input]
} elseif {([info exists html(fc2d)]) && $html(fc2d) == $html(todays_day)} {
set input [string map "%w5% {${incith::weather::seperator}[ibold "High/Low:"] [if2c $html(fc2hf) $html(fc2lf)]}" $input]
} elseif {([info exists html(fc3d)]) && $html(fc3d) == $html(todays_day)} {
set input [string map "%w5% {${incith::weather::seperator}[ibold "High/Low:"] [if2c $html(fc3hf) $html(fc3lf)]}" $input]
} elseif {([info exists html(fc4d)]) && $html(fc4d) == $html(todays_day)} {
set input [string map "%w5% {${incith::weather::seperator}[ibold "High/Low:"] [if2c $html(fc4hf) $html(fc4lf)]}" $input]
} elseif {([info exists html(fc5d)]) && $html(fc5d) == $html(todays_day)} {
set input [string map "%w5% {${incith::weather::seperator}[ibold "High/Low:"] [if2c $html(fc5hf) $html(fc5lf)]}" $input]
} else {
set input [string map "%w5% {${incith::weather::seperator}[ibold "High/Low:"] Unavailable}" $input]
}
if {[info exists html(humidity)]} {
set input [string map "{%w6%} {${incith::weather::seperator}[ibold "Humidity:"] $html(humidity)%}" $input]
} else {
set input [string map "{%w6%} {}" $input]
}
if {[info exists html(dewf)] && [info exists html(dewc)]} {
set input [string map "{%w7%} {${alternate_sep}[ibold "Dew Point:"] [i2fac $html(dewf) $html(dewc)]}" $input]
} else {
set input [string map "{%w7%} {}" $input]
}
if {[info exists html(uv_min)] && [info exists html(uv_max)]} {
set input [string map "{%w8%} {${incith::weather::seperator}[ibold "UV:"] $html(uv_min)\/$html(uv_max)}" $input]
} else {
set input [string map "{%w8%} {}" $input]
}
if {[info exists html(pressure_hpa)] && [info exists html(pressure_in)]} {
set input [string map "{%w9%} {${incith::weather::seperator}[ibold "Pressure:"] ${html(pressure_in)} in\/${html(pressure_hpa)} hPa}" $input]
} else {
set input [string map "{%w9%} {}" $input]
}
if {[info exists html(windd)]} {
set temp_wind "${incith::weather::seperator}[ibold "Wind:"] $html(windd)"
if {[info exists html(windm)] && [info exists html(windk)]} {
if {${incith::weather::celcius_first} >= 1} {
append temp_wind " at $html(windk) KPH ($html(windm) MPH)"
} else {
append temp_wind " at $html(windm) MPH ($html(windk) KPH)"
}
}
set input [string map "{%w10%} {${temp_wind}}" $input]
} else {
set input [string map "{%w10%} {}" $input]
}
regsub -- "^\\s*${incith::weather::seperator}" $input {} input
return $input
} elseif {$type == "forecast"} {
if {[info exists html(location)]} {
set input [string map "{%f0%} {${incith::weather::seperator}[ibold "$html(location) Forecast"] (High/Low)}" $input]
} else {
set input [string map "{%f0%} {}" $input]
}
if {[info exists html(update_time)] && [info exists html(update_date)]} {
set input [string map "{%f1%} {${incith::weather::seperator}[ibold "Updated:"] $html(update_time) $html(update_timezone) ($html(update_date))}" $input]
} else {
set input [string map "{%f1%} {}" $input]
}
# prepend the chance of precipitation if necessary
if {[info exists html(fc1chance)]} {
set html(fc1c) "$html(fc1chance) $html(fc1c)"
}
if {[info exists html(fc2chance)]} {
set html(fc2c) "$html(fc2chance) $html(fc2c)"
}
if {[info exists html(fc3chance)]} {
set html(fc3c) "$html(fc3chance) $html(fc3c)"
}
if {[info exists html(fc4chance)]} {
set html(fc4c) "$html(fc4chance) $html(fc4c)"
}
if {[info exists html(fc5chance)]} {
set html(fc5c) "$html(fc5chance) $html(fc5c)"
}
if {[info exists html(fc1hf)] && [info exists html(fc1lf)] && [info exists html(fc1c)]} {
set input [string map "{%f2%} {${incith::weather::seperator}[ibold "$html(fc1d):"] $html(fc1c), [if2c $html(fc1hf) $html(fc1lf)]}" $input]
} else {
set input [string map "{%f2%} {}" $input]
}
if {[info exists html(fc2hf)] && [info exists html(fc2lf)] && [info exists html(fc2c)]} {
set input [string map "{%f3%} {${alternate_sep}[ibold "$html(fc2d):"] $html(fc2c), [if2c $html(fc2hf) $html(fc2lf)]}" $input]
} else {
set input [string map "{%f3%} {}" $input]
}
if {[info exists html(fc3hf)] && [info exists html(fc3lf)] && [info exists html(fc3c)]} {
set input [string map "{%f4%} {${incith::weather::seperator}[ibold "$html(fc3d):"] $html(fc3c), [if2c $html(fc3hf) $html(fc3lf)]}" $input]
} else {
set input [string map "{%f4%} {}" $input]
}
if {[info exists html(fc4hf)] && [info exists html(fc4lf)] && [info exists html(fc4c)]} {
set input [string map "{%f5%} {${incith::weather::seperator}[ibold "$html(fc4d):"] $html(fc4c), [if2c $html(fc4hf) $html(fc4lf)]}" $input]
} else {
set input [string map "{%f5%} {}" $input]
}
if {[info exists html(fc5hf)] && [info exists html(fc5lf)] && [info exists html(fc5c)]} {
set input [string map "{%f6%} {${incith::weather::seperator}[ibold "$html(fc5d):"] $html(fc5c), [if2c $html(fc5hf) $html(fc5lf)]}" $input]
} else {
set input [string map "{%f6%} {}" $input]
}
regsub -- "^\\s*${incith::weather::seperator}" $input {} input
return $input
} elseif {$type == "time"} {
if {[info exists html(local_timezone)] && [info exists html(local_date)] && [info exists html(local_timezone)]} {
set input [string map "{%w1%} {${incith::weather::seperator}[ibold "Local Time:"] $html(local_time) $html(local_timezone) ($html(update_date))}" $input]
} else {
set input [string map "{%w1%} {}" $input]
}
if {[info exists html(location)]} {
if {[info exists html(latitude)] && [info exists html(longitude)] && $incith::weather::show_lat_lon >= 1} {
set input [string map "{%w0%} {${incith::weather::seperator}[ibold $html(location)] ($html(latitude)\/$html(longitude))}" $input]
} else {
set input [string map "{%w0%} {${incith::weather::seperator}[ibold $html(location)]}" $input]
}
} else {
set input [string map "{%w0%} {}" $input]
}
regsub -- "^\\s*${incith::weather::seperator}" $input {} input
return $input
} else {
return 0
}
}
# [time_handler] : handles public & private messages for !time
#
proc time_handler {nick uhand hand args} {
if {[llength $args] >= 2} { # public message
if {${incith::weather::public_to_private} >= 1} {
set where $nick
} else {
set where [lindex $args 0]
if {[lsearch -exact [channel info $where] +weather] == -1} {
return
}
}
set chan [lindex $args 0]
set input [lindex $args 1]
} else { # private message
set where $nick
set chan "private"
set input [lindex $args 0]
if {${incith::weather::private_messages} <= 0} {
return
}
}
# flood protection
if {[flood $nick $uhand $hand $where]} {
return
}
# user defaults, user must exist as a user on the bot
if {[lindex $input 0] == "-set"} {
if {[validuser $hand]} {
setuser $hand XTRA incith:weather.location "[lrange $input 1 end]"
send_output $nick "Default weather location set to [lrange $input 1 end]."
return
} else {
send_output $nick "Sorry, your bot handle was not found. Unable to set a default."
return
}
set input [lrange $input 1 end]
} elseif {[regexp -- "^\\s*$" $input] && [validuser $hand]} {
set input [getuser $hand XTRA incith:weather.location]
}
# log it
putlog "${incith::weather::version}: <${nick}/${chan}> ${incith::weather::command_char}time $input"
# no input given
if {[regexp -- "^\\s*$" $input]} {
send_output $where "Please visit http://classic.wunderground.com for more details on searching methods."
return
}
# fetch the html
array set html [fetch_html $input]
# check for html's existence
if {[info exists html(error)]} {
send_output $where $html(error)
return
}
# for seperate line results option
if {${incith::weather::seperate_lines} >= 1} {
set alternate_sep "\n"
} else {
set alternate_sep ${incith::weather::seperator}
}
# make sure we have data to send, and send it
set reply [custom_format [array get html] "time" $incith::weather::time_format $nick]
if {$reply == "0"} { unset reply }
if {[info exists reply]} {
send_output $where $reply
} else {
send_output $where "There was a problem while attempting to parse the data."
}
}
# [weather_handler] : handles public & private messages for !weather
#
proc weather_handler {nick uhand hand args} {
if {[llength $args] >= 2} { # public message
if {${incith::weather::public_to_private} >= 1} {
set where $nick
} else {
set where [lindex $args 0]
if {[lsearch -exact [channel info $where] +weather] == -1} {
return
}
}
set chan [lindex $args 0]
set input [lindex $args 1]
} else { # private message
set where $nick
set chan "private"
set input [lindex $args 0]
if {${incith::weather::private_messages} <= 0} {
return
}
}
# flood protection
if {[flood $nick $uhand $hand $where]} {
return
}
# user defaults, user must exist as a user on the bot
if {[lindex $input 0] == "-set"} {
if {[validuser $hand]} {
setuser $hand XTRA incith:weather.location "[lrange $input 1 end]"
putserv "notice $nick :Default weather location set to [lrange $input 1 end]"
return
} else {
putserv "notice $nick :Sorry, your bot handle was not found. Unable to set a default until you are known. Who are you?"
return
}
set input [lrange $input 1 end]
} elseif {[regexp -- "^\\s*$" $input] && [validuser $hand]} {
set input [getuser $hand XTRA incith:weather.location]
}
# log it
putlog "${incith::weather::version}: <${nick}/${chan}> ${incith::weather::command_char}weather $input"
# no input given
if {[regexp -- "^\\s*$" $input]} {
send_output $where "Please visit http://classic.wunderground.com for more details on searching methods."
return
}
# fetch the html
array set html [fetch_html $input]
# check for html's existence
if {[info exists html(error)]} {
send_output $where $html(error)
return
}
# for seperate line results option
if {${incith::weather::seperate_lines} >= 1} {
set alternate_sep "\n"
} else {
set alternate_sep ${incith::weather::seperator}
}
# make sure we have data to send, and send it
set reply [custom_format [array get html] "weather" $incith::weather::weather_format $nick]
if {$reply == "0"} { unset reply }
if {[info exists reply]} {
send_output $where $reply
} else {
send_output $where "There was a problem while attempting to parse the data."
}
}
# [forecast_handler] : handles forecast requests
#
proc forecast_handler {nick uhand hand args} {
if {[llength $args] >= 2} { # public message
if {${incith::weather::public_to_private} >= 1} {
set where $nick
} else {
set where [lindex $args 0]
if {[lsearch -exact [channel info $where] +weather] == -1} {
return
}
}
set chan [lindex $args 0]
set input [lindex $args 1]
} else { # private message
set where $nick
set chan "private"
set input [lindex $args 0]
if {${incith::weather::private_messages} <= 0} {
return
}
}
# flood protection
if {[flood $nick $uhand $hand $where]} {
return
}
# user defaults, user must exist as a user on the bot
if {[lindex $input 0] == "-set"} {
if {[validuser $hand]} {
setuser $hand XTRA incith:weather.location "[lrange $input 1 end]"
send_output $nick "Default weather location set to [lrange $input 1 end]."
return
} else {
send_output $nick "Sorry, your bot handle was not found. Unable to set a default."
}
set input [lrange $input 1 end]
} elseif {[regexp -- "^\\s*$" $input] && [validuser $hand]} {
set input [getuser $hand XTRA incith:weather.location]
}
# log it
putlog "${incith::weather::version}: <${nick}/${chan}> ${incith::weather::command_char}forecast $input"
# no input given
if {[regexp -- "^\\s*$" $input]} {
send_output $where "Please visit http://classic.wunderground.com for more details on searching methods."
return
}
# fetch the html
array set html [fetch_html $input]
# check for html's existence
if {[info exists html(error)]} {
send_output $where $html(error)
return
}
# for seperate line results option
if {${incith::weather::seperate_lines} >= 1} {
set alternate_sep "\n"
} else {
set alternate_sep ${incith::weather::seperator}
}
# make sure we have data to send, and send it
set reply [custom_format [array get html] "forecast" $incith::weather::forecast_format $nick]
if {$reply == "0"} { unset reply }
if {[info exists reply]} {
send_output $where $reply
} else {
send_output $where "There was a problem while attempting to parse the data."
}
}
# [astronomy_handler] : handles public & private messages for !sky
#
proc astronomy_handler {nick uhand hand args} {
if {[llength $args] >= 2} { # public message
if {${incith::weather::public_to_private} >= 1} {
set where $nick
} else {
set where [lindex $args 0]
if {[lsearch -exact [channel info $where] +weather] == -1} {
return
}
}
set chan [lindex $args 0]
set input [lindex $args 1]
} else { # private message
set where $nick
set chan "private"
set input [lindex $args 0]
if {${incith::weather::private_messages} <= 0} {
return
}
}
# flood protection
if {[flood $nick $uhand $hand $where]} {
return
}
# user defaults, user must exist as a user on the bot
if {[lindex $input 0] == "-set"} {
if {[validuser $hand]} {
setuser $hand XTRA incith:weather.location "[lrange $input 1 end]"
send_output $nick "Default weather location set to [lrange $input 1 end]."
return
} else {
send_output $nick "Sorry, your bot handle was not found. Unable to set a default."
return
}
set input [lrange $input 1 end]
} elseif {[regexp -- "^\\s*$" $input] && [validuser $hand]} {
set input [getuser $hand XTRA incith:weather.location]
}
# log it
putlog "${incith::weather::version}: <${nick}/${chan}> ${incith::weather::command_char}sky $input"
# no input given
if {[regexp -- "^\\s*$" $input]} {
send_output $where "Please visit http://classic.wunderground.com for more details on searching methods."
return
}
# fetch the html
array set html [fetch_html $input]
# check for html's existence
if {[info exists html(error)]} {
send_output $where $html(error)
return
}
# for seperate line results option
if {${incith::weather::seperate_lines} >= 1} {
set alternate_sep "\n"
} else {
set alternate_sep ${incith::weather::seperator}
}
# make sure we have data to send, and send it
if {[info exists html(location)]} {
if {$::incith::weather::usenicks > 0 } {
set reply "\002$nick's\002 sky request${incith::weather::seperator}"
} else {
set reply ""
}
append reply "[ibold "$html(location) Astronomy"]"
if {[info exists html(sun_rise)] && [info exists html(sun_set)]} {
append reply "${incith::weather::seperator}[ibold "Sunrise:"] $html(sun_rise)${incith::weather::seperator}[ibold "Sunset:"] $html(sun_set)"
}
if {[info exists html(moon_rise)] && [info exists html(moon_set)]} {
if {[info exists html(moon_phase)] && [info exists html(moon_percent)]} {
append reply "${incith::weather::seperator}[ibold "Moon:"] $html(moon_phase) ($html(moon_percent))"
}
append reply "${incith::weather::seperator}[ibold "Moonrise:"] $html(moon_rise)"
append reply "${incith::weather::seperator}[ibold "Moonset:"] $html(moon_set)"
}
if {[info exists html(length_light)]} {
append reply "${incith::weather::seperator}[ibold "Visible Light:"] $html(length_light)"
}
if {[info exists html(length_day)]} {
append reply "${incith::weather::seperator}[ibold "Daylight Length:"] $html(length_day)"
}
if {[info exists html(length_tomorrow)]} {
append reply "${incith::weather::seperator}[ibold "Tomorrow:"] $html(length_tomorrow)"
}
send_output $where $reply
} else {
send_output $where "There was a problem while attempting to parse the data."
}
}
# [fetch_html] : fetches and returns a list of usable data
#
proc fetch_html {query {query_url "http://classic.wunderground.com/cgi-bin/findweather/getForecast?query="} {no_format 0}} {
set multiple_results 0
# store the website we are retrieving
set output(query) $query
if {$no_format != 1} {
set output(query) [::http::formatQuery $query]
set output(query) [string trimleft $output(query) "+"]
}
# setup proxy information, if any
if {[string match {*:*} ${incith::weather::proxy}] == 1} {
set proxy_info [split ${incith::weather::proxy} ":"]
}
# the "browser" we are using
set ua "Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7e"
if {[info exists proxy_info] == 1} {
set http [::http::config -useragent $ua -proxyhost [lindex $proxy_info 0] -proxyport [lindex $proxy_info 1]]
} else {
set http [::http::config -useragent $ua]
}
# retrieve the html; round()'ed because -timeout likes integers. [catch] for error messages
catch {set http [::http::geturl "${query_url}$output(query)" -headers {Cookie "Units=both"} -timeout [expr round(1000 * ${incith::weather::timeout})]]} output(status)
# make sure the http request succeeded
if {![string match {::http::*} $output(status)]} {
set output(error) "Failed to connect."
} elseif {[::http::status $http] == "timeout"} {
set output(error) "The operation timed out after ${incith::weather::timeout} seconds."
} elseif {[::http::status $http] != "ok"} {
set output(error) "The server could not complete our request (server error)."
}
# in case we're erroring out, close the socket and report why
if {[info exists output(error)]} {
::http::cleanup $http
return [array get output]
}
# $html will contain our html source code
set html [::http::data $http]
# we no longer require the connection
::http::cleanup $http
# debug: output the html to a file
if {${incith::weather::debug} >= 1} {
set fopen [open incith-weather-pre.txt w]
puts $fopen $html
close $fopen
}
if {[regexp {Search Results} $html] || [regexp {Scroll down to view a list of all all cities} $html]} {
set multiple_results 1
}
# html cleanups
regsub -all {\n} $html {} html
regsub -all {\t} $html {} html
regsub -all { } $html { } html
regsub -all {°} $html {} html
regsub -all {>} $html {>} html
regsub -all {<} $html {<} html
regsub -all {<td style="padding: 0;">.*?</td>} $html {} html
regsub -all {<td title="Add This Location to Your Favorite Cities"><a href="/php/editfavs\.php\?addfav=[\d\w]+\.[\d\w]+"><img src="http://icons-..\.wxug\.com/graphics/wu2/favsBarSave\.png" width="48" height="13" alt="Add to My Favorites" /></a></td>} $html {} html
# debug: output the html to a file
if {${incith::weather::debug} >= 1} {
set fopen [open incith-weather-post.txt w]
puts $fopen $html
close $fopen
}
# check for problems
set multiple 0
if {[regexp {<h1>There has been an error!</h1>} $html]} {
regexp -- {</h1>.*?<p id.*?>(.*?)<p class="b">} $html - preparse
regsub {</p>} $preparse ". " preparse
regsub -all -- {<.*?>} $preparse "" preparse
set output(error) $preparse
} elseif {[regexp {Redirect page<br><br>There is nothing} $html] || [regexp {Choose the first letter of a city or search for any location.</p>} $html] || [regexp {<h1>Europe</h1><div id="titleBar">Choose a Country</div>} $html] || [regexp {<h1>Australia</h1><div id="titleBar">Select a country or Australian province below.</div>} $html] || [regexp {<h1>Africa</h1><div id="titleBar">Choose a Country</div>} $html]} {
set multiple_results 0
set output(error) "Please refine your search."
} elseif {$multiple_results == 1} {
# todo: store bad results too in case duplicate results both have no data
# e.g., first bad result put to bad_list, second matching bad result put to good list
set i 1; set multiple 2; set num_results 0
set loop_reply ""; set loop_loc ""; set loop_url ""
# skip down a bit in the html
regsub -- {.*?Search Results} $html {} html
regsub -- {.*?Scroll down to view a list of all all cities} $html {} html
# this regexp removes locations with no data
foreach {junk row} [regexp -inline -all -- {<tr>(.*?)</tr>} $html] {
if {![string match "*</td><td> </td><td> </td><td> </td><td> </td><td> </td>*" $row]} {
if {[regexp -- {<tr><td><a href="(.*?\.html)">(.*?)</a></td><td> <span} $junk - loop_url loop_loc]} {
lappend lr $loop_loc
lappend lu $loop_url
}
}
}
# if after filtering we only have 1 result, use it
if {[llength [set lr [lsort -unique -increasing $lr]]] == 1} {
unset output
array set output [fetch_html "[lindex $lu 0]" "http://classic.wunderground.com" 1]
} else {
set output(error) "Multiple Results Found: [join $lr "; "]"
}
}
# bail out if there's a problem
if {[info exists output(error)]} {
return [array get output]
}
# html parsing
#
# conditions
regexp -- {Local Time:<span class="b">(.+? A?P?M)\s+(.+?)( on (.+?))?</span>} $html - output(local_time) output(local_timezone) output(local_date) output(local_date)
regexp -- {Updated: <span.*? value="\d+">(.+? A?P?M)\s+(.+?) on (.+?)</span>(?:</div>|</span>)} $html - output(update_time) output(update_timezone) output(update_date)
if {![info exists output(update_time)]} {
regexp -- {Updated: (.+? A?P?M)\s+(.+?)( on (.+?))?</td>} $html - output(update_time) output(update_timezone) output(update_date) output(update_date)
}
if {[string match "*>*" $output(update_time)]} { set output(update_time) [lindex [split $output(update_time) >] end] }
# multiple location parses just in case
regexp -- {id="cityTable"><tr><td class="nobr full"><h1>(.+?)\ ?</h1></td>} $html - output(location)
if {![info exists output(location)]} {
regexp -- {<link rel="alternate" type="application/rss\+xml" title="(.+?)\ ?RSS" href=} $html - output(location)
}
if {![info exists output(location)]} {
regexp -- {<div class="subG b">(.+?)(?:\ \(Airport\)|\(PWS\))?</div>} $html - output(location)
}
#regexp -- {pwsvariable="tempf".+?> <span class="nobr"><span class="b">(.+?)</span> F</span> / <span class="nobr"><span class="b">(.+?)</span> C</span></span></div>} $html - output(tempf) output(tempc)
if {[regexp -nocase {<span class="nobr"><span class="b">(.*?)</span>(.*?)</span>} $html - output(tempf) output(tempc)]} {
set output(tempc) [string index $output(tempc) end]
if {[string equal "F" $output(tempc)]} {
set output(tempc) [format "%.${incith::weather::granularity}f" [expr {5.0/9.0*($output(tempf) - 32.0)}]]
if {[string match *\.* $output(tempc)]} {
set c [split $output(tempc) .]
set ot1 [lindex $c 0]
set ot2 [string trim [lindex $c 1] " 0"]
if {[string length $ot2]} { set output(tempc) "${ot1}.${ot2}" } { set output(tempc) $ot1 }
}
} else {
set output(tempc) $output(tempf)
set output(tempf) [format "%.${incith::weather::granularity}f" [expr {9.0/5.0*$output(tempc) + 32.0}]]
if {[string match *\.* $output(tempf)]} {
set c [split $output(tempf) .]
set ot1 [lindex $c 0]
set ot2 [string trim [lindex $c 1] " 0"]
if {[string length $ot2]} { set output(tempf) "${ot1}.${ot2}" } { set output(tempf) $ot1 }
}
}
}
if {[info exists output(tempf)]} {
set templength [string length $output(tempf)]
if {$templength > 20} {
putlog "length fubar"
set output(error) "Something is fubar! :)"
return [array get output]
}
}
regexp -- {Windchill:</td>.*?<span class="nobr"><span class="b">(.*?)</span>.*?<span class="nobr"><span class="b">(.+?)</span>} $html - output(windchillf) output(windchillc)
regexp -- {pwsvariable="humidity" english="" metric="" value="(.+?)">} $html - output(humidity)
if {[regexp -- {pwsvariable="dewptf".+?>.*?<span class="nobr"><span class="b">(.*?)</span>(.*?)</span>} $html - output(dewf) output(dewc)]} {
set output(dewc) [string index $output(dewc) end]
if {[string equal "F" $output(dewc)]} {
set output(dewc) [format "%.${incith::weather::granularity}f" [expr {5.0/9.0*($output(dewf) - 32.0)}]]
if {[string match *\.* $output(dewc)]} {
set c [split $output(dewc) .]
set od1 [lindex $c 0]
set od2 [string trim [lindex $c 1] " 0"]
if {[string length $od2]} { set output(dewc) "${od1}.${od2}" } { set output(dewc) $od1 }
}
} else {
set output(dewc) $output(dewf)
set output(dewf) [format "%.${incith::weather::granularity}f" [expr {9.0/5.0*$output(dewc) + 32.0}]]
if {[string match *\.* $output(dewf)]} {
set c [split $output(dewf) .]
set od1 [lindex $c 0]
set od2 [string trim [lindex $c 1] " 0"]
if {[string length $od2]} { set output(dewf) "${od1}.${od2}" } { set output(dewf) $od1 }
}
}
}
# normal wind:
regexp -- {Wind:</td>.+? pwsvariable="windspeedmph" english="mph" metric="km/h"> <span class="nobr"><span class="b">(.+?)</span> mph</span> / <span class="nobr"><span class="b">(.+?)</span> km/h</span>} $html - output(windm) output(windk)
# wind direction..
regexp -- {from the </span>.+? pwsvariable="winddir" english="" metric="" value="(.+?)">} $html - output(windd)
# calm wind direction:
if {![info exists output(windd)]} {
regexp -- {Wind:</td>.+? pwsvariable="windspeedmph" english="mph" metric="km/h">(Calm)</span>} $html - output(windd)
}
# since adding 'from the </span>.+? ' to the above RE, if it fails, the wind should be Calm
# removing 'from the </span>.+? ' causes it to fetch wind dir data from the wrong location
# moved snippet of code below mobile fetch
regexp -- {<div class="b" style="font-size: 14px;">(.+?)</div>} $html - output(conditions)
regexp -- {<td>UV:</td><td class="b">(.+?) <span class="nb">out of (.+?)</span></td>} $html - output(uv_min) output(uv_max)
# parse only the pressure block
regexp -- {pwsvariable="baromin" english="in" metric="hPa" value=".*?">(.*?)</span></td>} $html - output(pressure_data)
if {[info exists output(pressure_data)]} {
regexp -- {<b>(.+?)</b> in / <b>(.+?)</b> hPa} $output(pressure_data) - output(pressure_in) output(pressure_hpa)
}
# lat/lon
regexp -- {Lat/Lon: <span class="b">(\d+\.?\d+?\ \w)\ (\d+\.?\d+?\ \w)</span>} $html - output(latitude) output(longitude)
if {[info exists output(latitude)] && [info exists output(longitude)]} {
regsub -- "\ " $output(latitude) ${incith::weather::degree_symbol} output(latitude)
regsub -- "\ " $output(longitude) ${incith::weather::degree_symbol} output(longitude)
}
# astronomy sunrise/set and moonrise/set data
regexp -- {<td>Actual Time</td><td>(.*?)</td><td>(.*?)</td></tr>} $html - output(sun_rise) output(sun_set)
if {[info exists output(sun_rise)]} {
if {$output(sun_rise) == ""} {
set output(sun_rise) "Unavailable"
}
}
if {[info exists output(sun_set)]} {
if {$output(sun_set) == ""} {
set output(sun_set) "Unavailable"
}
}
if {![info exists output(sun_rise)]} {