-
Notifications
You must be signed in to change notification settings - Fork 2
/
weather.pl
executable file
·7423 lines (6197 loc) · 196 KB
/
weather.pl
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
#!/usr/bin/perl -w
# ############################################################################
# Abstract
# ############################################################################
#
# Use the 'My Yahoo' or 'Weather Underground' webpages to add weather information
# to xplanet maps
#
# homepage/newest version: http://hans.ecke.ws/xplanet
#
# xplanet information at http://xplanet.sourceforge.net
#
# ############################################################################
# Configuration / Installation
# ############################################################################
#
# 1.) Requirements
#
# xplanet version 0.91 or later http://xplanet.sourceforge.net
# Perl version 5.6 or later http://www.perl.com (Unix)
# http://www.activestate.com/Products/ActivePerl/ (Windows)
# geo_locator.pl version 1.0.2 or later http://hans.ecke.ws/xplanet
# Please install geo_locator and MAKE SURE IT WORKS (TEST IT!)
# before proceeding to install weather.pl!
#
# 2.) Setup Yahoo
#
# * Start either Netscape, Galeon or Mozilla. NOT Internet Explorer or Opera!
# * Make sure you have cookies enabled and yahoo.com is allowed to set and read cookies
# * If you do not have a Yahoo! account, then please create one.
# * Login to weather.yahoo.com with your Yahoo! account
# * Add/select a weather channel and choose which cities you wish to have on the map
# * In your Yahoo-preferences (or when you sign in to Yahoo), check the box that says
# "remember my ID on this computer"
# * When leaving Yahoo, DONT SIGN OUT
#
# * Here is how you check that it works:
# * close all browser windows
# * re-start the browser
# * type 'weather.yahoo.com' in the location bar and hit enter
# * when http://weather.yahoo.com comes up, you should see all your weather
# locations on the screen and it should great you with "Hello, <Name>", NOT
# "Hello Guest"
#
# 3.) Setup Weather Underground (if you are not satisfied with Yahoo)
#
# * Login with netscape, galeon or mozilla to wunderground.com, enable cookies
# * Add/select cities to show (they call that "favorites")
#
# 4.) Install the Script
#
# * Copy the script into your xplanet directory
#
# * Set the environment variables
# XPLANET_DIR - Your xplanet installation directory
# You don't need to set this if you put the weather.pl script
# into your xplanet directory
# WEATHERPL_COOKIES - The cookie file for your browser. It should contain
# cookies from yahoo.com and/or wunderground.com
# For Mozilla under Windows it will often be something like
# C:\WINDOWS\Application Data\Mozilla\Profiles\default\***.slt\cookies.txt
# C:\Documents and Settings\<name>\Application Data\Mozilla\Profiles\default\***.slt\cookies.txt
# C:\WINNT\Profiles\<name>\Application Data\Mozilla\Profiles\default\***.slt\cookies.txt
# If you use Windows you set those in C:\autoexec.bat or "advanced settings" for WindowsXP
# If you use Unix you should know how to do that ($HOME/.profile or $HOME/.cshrc)
#
# * Once this works, you might try customizing further by editing the xplanet.conf
# configuration file.
#
# ############################################################################
# Usage
# ############################################################################
#
# * call the script. It will write a markerfile 'weather'
# * ask xplanet to include this markerfile into its maps using the
# option '-markerfile weather'
#
# If anything does not work, try editing the xplanet.conf configuration file
#
# ############################################################################
# Trouble Shooting
# ############################################################################
#
# If you run into trouble, the first thing to do is to run weather.pl with the
# "-d" command line switch. This will enable logging. It will output a lot of
# information. You might be able to figure out your problem from this.
#
# Don't hesitate to write me if something goes wrong! If you write me with a
# specific problem, first run weather.pl with the "-d" command line switch
# and include any output you will get.
#
# Below are some problems people encounter often. The examples are tailored for
# Windows. If you are using Unix you know how to convert that for you.
#
# * I setup some places in my.yahoo.com but weather.pl shows only some
# generic ones like London, New York, and San Francisco.
#
# See above in the "Configuration" section. You have probably not setup
# your cookies correctly. Write me if you have everything setup correctly
# but it still does not work.
#
# * I setup many places in my.yahoo.com (wunderground.com) but weather.pl
# only recognizes a few of them. What's wrong?
#
# First, its very good that weather.pl recognizes _some_ of those places.
# This means it can communicae both with the internet and geo_locator.pl
# (which provides coordinates to location names).
#
# Lets assume weather.pl does not recognize your home town "Vuuma" because
# geo_locator.pl can not find its coordinates.
#
# Start a command line prompt and ask geo_locator.pl for those places: You'd
# call something like
# C:\> perl .\geo_locator.pl Vuuma
# to ask for the location of the city Vuuma.
# * If geo_locator.pl gives you coordinates for Vuuma we have a problem in
# communication either with the internet or between geo_locator.pl and
# weather.pl. Please contact the author.
# * If geo_locator.pl does not recognize the name it will remain silent.
# This is somewhat understandable. After all, we can not have information
# to all places on earth.
#
# Now try this:
# C:\> perl .\geo_locator.pl -s m -n TGN Vuuma
# * If geo_locator.pl finds Vuuma in the TGN (Getty's Thesaurus on the net)
# it will give you the coordinates. Add them to the file markers/weather_markers
# (you should be able to figure out the format by yourself).
# * If geo_locator.pl does not find Vuuma in the TGN you are on your own. Look
# in the internet for Vuuma's coordiantes. Usually a query like
# "longitude latitude Vuuma" in Google should work. Find those coordinates and
# add them to the markers/weather_markers marker file.
#
# Re-run geo_locator.pl to see if it understands you now:
# C:\> perl .\geo_locator.pl Vuuma
# * If geo_locator.pl still does not find Vuuma you either did not add its
# coordinates correctly or have another problem. Write the author in that case.
# * If you added Vuuma's coordinates correctly it will find Vuuma and show its
# coordinates. You should be all set. Run weather.pl and see what happens.
#
# * I see the temperatures etc just fine, but the icons are not shown. Looking in
# the images/ directory, I see the icon files (something like 72.gif). But on the
# maps xplanet draws there are no icons.
#
# Try running xplanet manually. If you see a message like this:
# Sorry this program was not compiled with gif support
# Can't read image 85.gif
# then you should try finding a copy of the xplanet executable that can read
# and understand) our gif icons. For Windows, this would be the xplanet.exe that
# comes in xplanet-0.94a.zip.
#
# * I see the icons just fine, but the temperatures and location names are not
# shown. For each location, there is only a small dot, no label.
# (the answer is Linux-centric, sorry)
#
# I wrote to Hari Nair (xplanet's author) about this. Here is his answer:
#
# > unless you have an active X server,
# > xplanet uses the FreeType library to do text
# > annotation. If the binary isn't compiled with
# > FreeType support, you will get the messages you
# > describe. The RPM I compiled for the web site was
# > on a Red Hat 7.0 system, which didn't have
# > FreeType installed. I would suggest compiling a
# > new version with FreeType if you have these problems.
#
# A quick'n dirty fix is to ensure that the DISPLAY environment variable
# is always set. Before calling xplanet, do something like this on the
# command line:
# $ export DISPLAY=":0" (if your shell is bash or ksh)
# $ setenv DISPLAY :0 (if your shell is tcsh or csh)
# This might fix your problem.
#
# On weather.pl's homepage you can also find an xplanet.rpm file that has
# been compiled with freetype support. Try replacing your copy with this one.
#
#
# ############################################################################
# Legalese
# ############################################################################
#
# Copyright 2001 João Pedro Gonçalves [email protected] for version 1
# Copyright 2001, 2002 Hans Ecke [email protected] for all versions>=2
#
# Licence: Gnu Public License. In short: This comes without any warranty. Redistribution
# of the original or changed versions must leave this Copyright statement intact -AND-
# provide the sourcecode for free.
#
# Comment, suggestions, bugreports are very much appreciated!
#
# ############################################################################
# Changelog
# ############################################################################
#
# Version
# 4.1.8: Updated wunderground.com parsing
# 4.1.6: 'Freezing Fog' added. Thanks Jim Schreiber.
# 4.1.5: some icon location fixes
# 4.1.4: windows fixes
# 4.1.3: added lots of new conditions, thanks to Guylhem Aznar, Tony Nugent, Roger Brockie
# updated yahoo.com parsing
# recover if weather icons can't be downloaded
# 4.1.2: added 'Light Snow Showers' as a wunderground.com weather condition, thanks Guylhem Aznar
# more warning messages, thanks Guylhem Aznar
# 4.1.1: added 'N/A' as a wunderground.com weather condition
# 4.1: auto-updating; improved config file handling
# 4.0: source code cleanup: share source between all xplanet perl scripts
# included 'unknown' icon. Thanks Gerrit Cap!
# new unified configuration system using the xplanet.conf file
# changed license to GPL
# 3.2.15: added 'light rain showers' for wunderground weather conditions, thanks Jens Gecius
# 3.2.14: separated cookie code out more (code cleanup)
# cygwin compatibility fixes, thanks Gerrit Cap
# divided config section in two: primary and advanced
# 3.2.13: fixed bug with cookie handling that disabled most functionality of the script
# UPGRADE!
# 3.2.12: improved webpage caching
# 3.2.11: updated wunderground.com parsing, thanks Rojas Donovan
# changed weather icon file names from xxx.gif to weather_xxx.gif
# 3.2.10: caching get_webpage()
# updated, extended weather vocabulary for wunderground.com mode
# windows and non-US compatibility fixes, thanks Stefan P. Wolf
# updated wunderground.com parsing
# fixed month and year in writedebug() thanks Stefan P. Wolf
# detect invalid cookie file and die with message
# 3.2.9: minor windows compatibility fix
# updated wunderground parsing
# thanks Chris Harper for the bugreports
# small debuging changes
# fixed default noicon template
# 3.2.8: fixed bug in communication with geo_locator.pl: lookup for cities with
# words that start with lower case letters like "Rio de Janeiro"
# thanks Gerhard A. Blab for excellent bugreport!
# 3.2.7: documentation updated
# 3.2.6: added 2 more (weather condition) -> (icon) mappings for weather underground
# thanks again Matz for error reports!
# one more debug output line
# 3.2.5: more documentation added
# -d command line switch
# improved weather underground support: $template_noicon and friends.
# thanks Fr. Kipling Cooper for suggestions/ bugreports
# 3.2.4: work on (weather underground weather description) => (yahoo weather icon) mapping
# 3.2.3: better documentation
# Mozilla compatibility
# -- thanks Matz Johansson for patience in working on the Windows compatibility --
# 3.2.2: minor fixes
# 3.2.1: minor fixes; now requires geo_locator 1.0.2
# try harder with the Windows compatibility
# 3.2: Windows compatibility
# 3.1.1: $cookie_file looks for a environment variable WEATHERPL_COOKIES now
# this makes editing this script unnecessary in most cases
# 3.1: much faster coordinate lookup. We only call out to geo_locator
# once instead for each location. this should speed weather.pl up
# considerably.
# 3.0.3: http proxy support
# check that cookie file is not empty
# 3.0.2: adjustment for geo_locator.pl 1.0
# better autodetection of directories
# 3.0.1: small bugfixes
# 3: support for multiple weather sources: wunderground.com + yahoo.com
# nicer weather marker file written
# more flexible template syntax - thanks Felix for the suggestion
# bcca.org marker file
# use new geo_locator.pl script for coordinates
# require perl 5.6
# reacts to command line with some help text
# much more error detection
# 2.3.2: code cleanup
# 2.3.1: slightly more robust parsing. Thanks Felix
# 2.3: made the URL (http://weather.yahoo.com) a config option
# 2.2: bug fix where I mixed latitude/longitude. Thanks Felix
# updated for xplanet 0.91
# better mozilla cookie file support. Thanks Felix for the suggestion
# 2.1: more marker files, especially from ed-u.com
# 2: updated from the version by Joao
# heuristics to find longitude/latitude, instead of hard-coding inside the script
#
require 5.006;
use FindBin;
use lib $FindBin::Bin;
use File::Basename;
use Cwd;
use File::Spec;
use filetest 'access';
# if we need an icon for weather underground, we try to download it from here
my $default_weather_icon_location="http://us.i1.yimg.com/us.yimg.com/i/my/we";
# if downloaded copy found, when will it be considered too old?
# -1 means: consider any content of cache fresh
# in seconds (one hour = 60*60 = 3600)
our $cache_expiration=60*60*2.5;
# the configuration file
my $conffile=$ENV{'XPLANET_SCRIPTS_CONF'} || 'xplanet.conf';
our $VERSION="4.1.8";
BEGIN {
### Start of inlined library Hans2::FindBin.
$INC{'Hans2/FindBin.pm'} = './Hans2/FindBin.pm';
{
package Hans2::FindBin;
BEGIN {
use Exporter ();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @EXP_VAR @NON_EXPORT);
# $Exporter::Verbose=1;
# set the version for version checking
$VERSION = 1.00;
# if using RCS/CVS, this may be preferred
# $VERSION = do { my @r = (q$Revision: 2.21 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
# The above must be all one line, for MakeMaker
@ISA = qw(Exporter);
@EXP_VAR = qw(
$Bin
$RealBin
$Script
$Scriptbase
);
@EXPORT = (qw(
&abs2rel
),@EXP_VAR);
@NON_EXPORT = qw(
);
@EXPORT_OK = qw();
%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
}
# exported variables
use vars @EXP_VAR;
# exportable variables
use vars @EXPORT_OK;
#non exported package globals
use vars @NON_EXPORT;
use FindBin;
use File::Spec;
use File::Basename;
$Bin = File::Spec->canonpath($FindBin::Bin);
$RealBin = File::Spec->canonpath($FindBin::RealBin);
$Script = $FindBin::Script;
$Scriptbase = $Script;
$Scriptbase = (fileparse($Script,qr/\.\w{2,4}/))[0];
sub abs2rel($$) {
my ($path,$base)=@_;
return $path if !File::Spec->file_name_is_absolute($path);
$path=File::Spec->abs2rel($path,$base);
my ($volume,$directories,$file) = File::Spec->splitpath($path);
return $file if !$directories;
return File::Spec->catfile($directories,$file);
}
END {}
1;
};
### End of inlined library Hans2::FindBin.
Hans2::FindBin->import();
}
BEGIN {
### Start of inlined library Hans2::Util.
$INC{'Hans2/Util.pm'} = './Hans2/Util.pm';
{
package Hans2::Util;
BEGIN {
use Exporter ();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @EXP_VAR @NON_EXPORT);
# $Exporter::Verbose=1;
# set the version for version checking
$VERSION = 1.00;
# if using RCS/CVS, this may be preferred
# $VERSION = do { my @r = (q$Revision: 2.21 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
# The above must be all one line, for MakeMaker
@ISA = qw(Exporter);
@EXP_VAR = qw(
$I_am_interactive
$in_windows
$in_cygwin
$PATH_CONCAT
$EOF_CHAR_WRITTEN
);
@EXPORT = (qw(
),@EXP_VAR);
@NON_EXPORT = qw(
);
@EXPORT_OK = qw();
%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
}
# exported variables
use vars @EXP_VAR;
# exportable variables
use vars @EXPORT_OK;
#non exported package globals
use vars @NON_EXPORT;
use POSIX qw(locale_h strtod);
use File::Basename;
use File::Spec;
use Config;
use filetest 'access';
BEGIN {
Hans2::FindBin->import();
}
BEGIN {
### Start of inlined library Hans2::Cwd.
$INC{'Hans2/Cwd.pm'} = './Hans2/Cwd.pm';
{
package Hans2::Cwd;
BEGIN {
use Exporter ();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @EXP_VAR @NON_EXPORT);
# $Exporter::Verbose=1;
# set the version for version checking
$VERSION = 1.00;
# if using RCS/CVS, this may be preferred
# $VERSION = do { my @r = (q$Revision: 2.21 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
# The above must be all one line, for MakeMaker
@ISA = qw(Exporter);
@EXP_VAR = qw(
);
@EXPORT = (qw(
&getcwd
),@EXP_VAR);
@NON_EXPORT = qw(
);
@EXPORT_OK = qw();
%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
}
# exported variables
use vars @EXP_VAR;
# exportable variables
use vars @EXPORT_OK;
#non exported package globals
use vars @NON_EXPORT;
BEGIN {
require Cwd;
}
use File::Spec;
sub getcwd() {
return File::Spec->canonpath(Cwd::getcwd());
}
END {}
1;
};
### End of inlined library Hans2::Cwd.
Hans2::Cwd->import();
}
BEGIN {
### Start of inlined library Hans2::Debug.
$INC{'Hans2/Debug.pm'} = './Hans2/Debug.pm';
{
package Hans2::Debug;
BEGIN {
use Exporter ();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @EXP_VAR @NON_EXPORT);
# $Exporter::Verbose=1;
# set the version for version checking
$VERSION = 1.00;
# if using RCS/CVS, this may be preferred
# $VERSION = do { my @r = (q$Revision: 2.21 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
# The above must be all one line, for MakeMaker
@ISA = qw(Exporter);
@EXP_VAR = qw(
$DEBUG
);
@EXPORT = (qw(
&writedebug
&writestdout
&writestderr
),@EXP_VAR);
@NON_EXPORT = qw(
$off
);
@EXPORT_OK = qw();
%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
}
# exported variables
use vars @EXP_VAR;
# exportable variables
use vars @EXPORT_OK;
#non exported package globals
use vars @NON_EXPORT;
use POSIX qw(locale_h strtod);
use File::Basename;
use File::Spec;
use Config;
BEGIN {
Hans2::FindBin->import();
}
select STDERR; $| = 1; # make unbuffered
select STDOUT; $| = 1; # make unbuffered
$off = 0;
# before anything else, look if we should run in DEBUG mode
$DEBUG=0;
if(@ARGV) {
my @argv_new;
foreach(@ARGV) {
if(/^-+d/) {
$ENV{'DEBUG'}=1;
$DEBUG=1;
}
else {
push @argv_new,$_;
}
}
@ARGV=@argv_new;
}
my $prefix_tty=" " x (length($Scriptbase) + 2);
my $prefix_log;
{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= localtime(time());
$mon++;
$year+=1900;
foreach($mon,$mday,$hour,$min,$sec) {
$_ ="0$_" if $_<10;
}
my $txt="$Scriptbase($$) $mday/$mon $hour:$min:$sec ";
$prefix_log=" " x length($txt);
}
my $indent_num=0;
my $indent_length=4;
sub push_indent($) {
my ($msg)=@_;
writedebug($msg) if $msg;
$indent_num++;
}
sub pop_indent() {
$indent_num-- if $indent_num>0;
}
sub writelogline($) {
my ($msg)=@_;
return if !$ENV{'LOGFILE'};
$msg =~ s/\n/\n$prefix_log/g;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= localtime(time());
$mon++;
$year+=1900;
foreach($mon,$mday,$hour,$min,$sec) {
$_ ="0$_" if $_<10;
}
local *LOG;
if(!open(LOG, ">>".$ENV{'LOGFILE'})) {
my $lf=$ENV{'LOGFILE'};
delete $ENV{'LOGFILE'}; # make sure we don't go to a endless loop
die "could not write to logfile $lf\n";
};
print LOG "$Scriptbase($$) $mday/$mon $hour:$min:$sec $msg\n";
close LOG;
}
sub writettyline($) {
my ($msg)=@_;
$msg =~ s/\n/\n$prefix_tty/g;
print STDERR "$msg\n";
}
# INTERFACE
# write out debug message if DEBUG is set
sub writedebug($) {
my ($msg)=@_;
return if $off;
$msg =~ s/[\s\n\r]+$//;
my $pref=" " x ($indent_num*$indent_length);
$msg=$pref . $msg;
$msg =~ s/\n/\n$pref/g;
writelogline($msg);
writettyline("$Scriptbase: $msg") if $ENV{'DEBUG'};
}
sub writewarn($) {
my ($msg)=@_;
return if $off;
$msg =~ s/[\s\n\r]+$//;
writelogline("!warning: ".$msg);
print STDERR $msg."\n";
}
sub writedie($) {
my ($msg)=@_;
return if $off;
$msg =~ s/[\s\n\r]+$//;
writelogline("!fatal: ".$msg);
# print STDERR $msg."\n";
}
sub writestdout($) {
my ($msg)=@_;
return if $off;
$msg =~ s/[\s\n\r]+$//;
writelogline($msg);
print STDOUT $msg."\n";
}
sub writestderr($) {
my ($msg)=@_;
return if $off;
$msg =~ s/[\s\n\r]+$//;
writelogline($msg);
print STDERR $msg."\n";
}
$SIG{__WARN__}=\&writewarn;
$SIG{__DIE__} =\&writedie;
writedebug("-------------------");
writedebug("initialized logging");
writedebug("$^X is version ".sprintf("%vd",$^V));
writedebug("OS: $^O");
{ my $h=$ENV{'HOME'} || "<undefined>";
writedebug("users home: $h");
}
writedebug("users id: effective: $>; real: $<");
if(@ARGV) {
writedebug("command line args: \'".join("\', \'",@ARGV)."\'");
}
else {
writedebug("command line args: <none>");
}
END {}
1;
};
### End of inlined library Hans2::Debug.
Hans2::Debug->import();
}
$in_windows= ( $^O =~ /win/i ? 1 : 0);
$in_cygwin = ( $^O =~ /cygwin/i ? 1 : 0);
$PATH_CONCAT=$Config{'path_sep'};
$EOF_CHAR_WRITTEN="^D";
$EOF_CHAR_WRITTEN="^Z" if $in_windows;
writedebug(File::Spec->catfile($Bin,$Script)." called from ".getcwd());
if($in_windows) {
writedebug("Windows environment detected");
}
else {
writedebug("Non-windows environment detected");
}
if($in_cygwin) {
writedebug("Cygwin environment detected");
}
else {
writedebug("Non-cygwin environment detected");
}
# from perl recipes
#
# returns true if we read from a file or redirection or are called by cron
sub I_am_interactive() {
return -t STDIN && -t STDOUT;
}
$I_am_interactive=I_am_interactive();
END {}
1;
};
### End of inlined library Hans2::Util.
Hans2::Util->import();
}
BEGIN {
### Start of inlined library Hans2::WebGet.
$INC{'Hans2/WebGet.pm'} = './Hans2/WebGet.pm';
{
package Hans2::WebGet;
BEGIN {
use Exporter ();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @EXP_VAR @NON_EXPORT);
# $Exporter::Verbose=1;
# set the version for version checking
$VERSION = 1.00;
# if using RCS/CVS, this may be preferred
# $VERSION = do { my @r = (q$Revision: 2.21 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
# The above must be all one line, for MakeMaker
@ISA = qw(Exporter);
@EXP_VAR = qw(
$HAVE_COOKIES
);
@EXPORT = (qw(
&URL_2_filename
&get_webpage
),@EXP_VAR);
@NON_EXPORT = qw(
$cache_expiration
$ua
);
@EXPORT_OK = qw();
%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
}
# exported variables
use vars @EXP_VAR;
# exportable variables
use vars @EXPORT_OK;
#non exported package globals
use vars @NON_EXPORT;
BEGIN {
# use Hans2::FindBin;
# use File::Spec;
# my $cook=File::Spec->catfile(File::Spec->catdir($Bin,'HTTP'),'Cookies.pm');
# unlink($cook) if -f $cook;
}
use File::Spec;
use LWP::UserAgent;
use LWP::Simple;
use File::Basename;
BEGIN {
### Start of inlined library HTTP::Cookies.
$INC{'HTTP/Cookies.pm'} = './HTTP/Cookies.pm';
{
package HTTP::Cookies;
use HTTP::Date qw(str2time time2str);
use HTTP::Headers::Util qw(split_header_words join_header_words);
use LWP::Debug ();
use vars qw($VERSION);
$VERSION = sprintf("%d.%02d", q$Revision: 1.25 $ =~ /(\d+)\.(\d+)/);
my $EPOCH_OFFSET = 0; # difference from Unix epoch
if ($^O eq "MacOS") {
require Time::Local;
$EPOCH_OFFSET = Time::Local::timelocal(0,0,0,1,0,70);
}
sub new
{
my $class = shift;
my $self = bless {
COOKIES => {},
}, $class;
my %cnf = @_;
for (keys %cnf) {
$self->{lc($_)} = $cnf{$_};
}
$self->load;
$self;
}
sub add_cookie_header
{
my $self = shift;
my $request = shift || return;
my $url = $request->url;
my $domain = _host($request, $url);
$domain = "$domain.local" unless $domain =~ /\./;
my $secure_request = ($url->scheme eq "https");
my $req_path = _url_path($url);
my $req_port = $url->port;
my $now = time();
_normalize_path($req_path) if $req_path =~ /%/;
my @cval; # cookie values for the "Cookie" header
my $set_ver;
my $netscape_only = 0; # An exact domain match applies to any cookie
while ($domain =~ /\./) {
LWP::Debug::debug("Checking $domain for cookies");
my $cookies = $self->{COOKIES}{$domain};
next unless $cookies;
# Want to add cookies corresponding to the most specific paths
# first (i.e. longest path first)
my $path;
for $path (sort {length($b) <=> length($a) } keys %$cookies) {
LWP::Debug::debug("- checking cookie path=$path");
if (index($req_path, $path) != 0) {
LWP::Debug::debug(" path $path:$req_path does not fit");
next;
}
my($key,$array);
while (($key,$array) = each %{$cookies->{$path}}) {
my($version,$val,$port,$path_spec,$secure,$expires) = @$array;
LWP::Debug::debug(" - checking cookie $key=$val");
if ($secure && !$secure_request) {
LWP::Debug::debug(" not a secure requests");
next;
}
if ($expires && $expires < $now) {
LWP::Debug::debug(" expired");
next;
}
if ($port) {
my $found;
if ($port =~ s/^_//) {
# The correponding Set-Cookie attribute was empty
$found++ if $port eq $req_port;
$port = "";
} else {
my $p;
for $p (split(/,/, $port)) {
$found++, last if $p eq $req_port;
}
}
unless ($found) {
LWP::Debug::debug(" port $port:$req_port does not fit");
next;
}
}
if ($version > 0 && $netscape_only) {
LWP::Debug::debug(" domain $domain applies to " .
"Netscape-style cookies only");
next;
}
LWP::Debug::debug(" it's a match");
# set version number of cookie header.
# XXX: What should it be if multiple matching
# Set-Cookie headers have different versions themselves
if (!$set_ver++) {
if ($version >= 1) {
push(@cval, "\$Version=$version");
} elsif (!$self->{hide_cookie2}) {
$request->header(Cookie2 => '$Version="1"');
}
}
# do we need to quote the value
if ($val =~ /\W/ && $version) {
$val =~ s/([\\\"])/\\$1/g;
$val = qq("$val");
}
# and finally remember this cookie
push(@cval, "$key=$val");
if ($version >= 1) {
push(@cval, qq(\$Path="$path")) if $path_spec;
push(@cval, qq(\$Domain="$domain")) if $domain =~ /^\./;
if (defined $port) {
my $p = '$Port';
$p .= qq(="$port") if length $port;
push(@cval, $p);
}
}
}
}
} continue {
# Try with a more general domain, alternately stripping
# leading name components and leading dots. When this
# results in a domain with no leading dot, it is for
# Netscape cookie compatibility only:
#
# a.b.c.net Any cookie
# .b.c.net Any cookie
# b.c.net Netscape cookie only
# .c.net Any cookie
if ($domain =~ s/^\.+//) {
$netscape_only = 1;
} else {
$domain =~ s/[^.]*//;
$netscape_only = 0;
}
}
$request->header(Cookie => join("; ", @cval)) if @cval;
$request;
}
sub extract_cookies
{
my $self = shift;
my $response = shift || return;
my @set = split_header_words($response->_header("Set-Cookie2"));
my @ns_set = $response->_header("Set-Cookie");
return $response unless @set || @ns_set; # quick exit
my $request = $response->request;
my $url = $request->url;
my $req_host = _host($request, $url);
$req_host = "$req_host.local" unless $req_host =~ /\./;
my $req_port = $url->port;
my $req_path = _url_path($url);
_normalize_path($req_path) if $req_path =~ /%/;
if (@ns_set) {
# The old Netscape cookie format for Set-Cookie
# http://www.netscape.com/newsref/std/cookie_spec.html
# can for instance contain an unquoted "," in the expires
# field, so we have to use this ad-hoc parser.
my $now = time();
# Build a hash of cookies that was present in Set-Cookie2
# headers. We need to skip them if we also find them in a
# Set-Cookie header.
my %in_set2;
for (@set) {
$in_set2{$_->[0]}++;
}
my $set;
for $set (@ns_set) {
my @cur;
my $param;
my $expires;
for $param (split(/;\s*/, $set)) {
my($k,$v) = split(/\s*=\s*/, $param, 2);
$v =~ s/\s+$//;
#print "$k => $v\n";
my $lc = lc($k);
if ($lc eq "expires") {
my $etime = str2time($v);
if ($etime) {
push(@cur, "Max-Age" => str2time($v) - $now);
$expires++;
}
} else {
push(@cur, $k => $v);
}
}
next if $in_set2{$cur[0]};