-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss.pl
executable file
·1987 lines (1765 loc) · 52.2 KB
/
rss.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
# $Header:$
# 20140217 Fix CDATA processing for Risks
# 20140217 Strip \r\n from title.
# 20140217 Add gzip compression - 2.8x compression
# 20140221 Add https: support for HackerNews
# 20140224 Fix https
# 20140224 Add 'Next-dl' to the page.
# 20140304 Fix HackerNews download
# 20140304 Dont kill myself on a re-exec
# 20140304 Fix "status" file output.
# 20140304 Add "/" request support.
# 20140305 Add 404 support. Silly me.
# 20140305 Add inquirer/arstecnica.
# 20140315 Add non-blocking writes.
# 20140317 Add metrics per site.
# 20140318 Avoid infinite cpu use if we hit an error'ed connection.
# 20140319 Add art_hash limit of 10,000
# 20140406 Ignore 'Win' from HotUkDeals.
# 20140406 Add /q page and use/store cookies to do T-4h.
# 20140521 Avoid hanging on an accept/sysread with no timeout.
# 20140907 Better unicode handling; centralise the conversions.
# 20150205 Add disabled support
# 20150309 Keep old articles; fix rquote/lquote
# 20150501 Update to www.crispeditor.co.uk
# 20160515 Improve hackernews collection.
# 20160604 Break up hackernews pages.
# 20160609 Add article links to HN pages and fix the relative links.
use strict;
use warnings;
use File::Basename;
use FileHandle;
use Getopt::Long;
use IO::File;
use POSIX;
use FindBin;
use IO::Socket;
use Compress::Zlib;
use lib "$FindBin::RealBin/";
use Utils;
my %sites;
my %history = (
req => 0,
googlebot => 0,
);
#######################################################################
# Command line switches. #
#######################################################################
my %opts = (
port => 3000,
size => 250,
num => 2000,
art_history => 10000,
sleep => 60,
stock_time => 1000,
);
my $a_id = 0;
my %art_hash;
my @art_list;
my $prog;
my $prog_mtime = (stat($0))[9];
my $rss_cfg;
my $err = 0;
my $target_time = 0;
my %cookies;
my $next_scan = '';
my @argv = @ARGV;
my @writers;
my $wbits = '';
my $curl_msg = '';
my $copyright_year = "2015-2021";
my $javascript = <<EOF;
<!-- rss.pl, author: Paul.D.Fox (crispeditor-at-gmail-com). (c) $copyright_year -->
<script type='text/javascript'>
function toggle(id) {
var e = document.getElementById(id);
var e1 = document.getElementById(id + 'p');
if (e.style.display == 'block') {
e.style.display = 'none';
e1.style.background = '#d0d0d0';
} else {
e.style.display = 'block';
e1.style.background = '#00ff00';
}
}
</script>
EOF
######################################################################
# CSS stylesheet. #
######################################################################
my $css = <<EOF;
<style type='text/css'>
td, body, mp, m, p {
border-radius: 20px ;
color: #000000 ;
background-color: #ebeced ;
zzwidth: 330;
max-width: 100%;
font-family: Verdana, Helvetica, Arial, sans-serif ;
}
p.fn {
font-size: 11;
font-family: "Lucida Console", Monaco, monospace;
}
#aa
{
background:#ebeced;
border: none;
border-top: 1px solid #d0d2d5;
border-radius: 0 0 4px 4px;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.75);
margin: 5px 0 -20px -20px; padding: 18px 20px;
width: 100%;
}
px {
border:1px solid #ccc;
padding:6px 12px;
text-align:left;
vertical-align:top;
font-family: Helvetica, Arial, sans-serif ;
background-color:inherit;
}
</style>
EOF
######################################################################
# Help text. #
######################################################################
my $help_txt = <<EOF;
<table width=400>
<tr><td>
This web site is an RSS feed aggregator, but designed to be
as frugal as possible - ideal for mobile phones and low data
limits. There are two types of pages - 'p' which are 250k/90k (raw/compressed)
and 'q'. The 'q' pages uses a simple cookie to track the last time
you made a request, and give you everything +4h from that request.
<p>
The 'q' pages avoid repeatedly giving you news items you may have read.
The pages are sent as gzipped encoded pages which reduces the data size
enormously.
<p>
If you have suggestions for sites to add to the feed, or feature
enhancements, feel free to contact CRiSP.Editor at gmail.com, or
<a href="mailto:Crisp.Editor\@gmail.com?Subject=RSS%20Feed%20Suggestions" target="_top">click here</a>
<p>
The following summarises the pages available:
</td></table>
<table width=400 border=1>
<tr>
<td valign=top><a href='/'>p</a></td><td>
The first page of headlines - whatever fits into 250k uncompressed. You
can omit the 'p'. Older pages are available as p1.html, p2.html, ... or
use the "Next" button at the top of the page.
</td>
<tr>
<td valign=top><a href='/q'>q</a></td><td>Retrieves items from the last request, minus 4h, so that you
wont get a blank page if you do quick/successive requests.</td>
<tr>
<td valign=top><a href='/status'>status</a></td><td>Some metrics for the author to look at. These
include counts of requests per day, and when the next requests are due.</td>
</table>
<p>
If you like this feed, feel free to donate:
<form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="CrispEditor\@gmail.com">
<input type="hidden" name="item_name" value="CRiSP">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="amount" value="5.00">
<b> RSS</b>
<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
EOF
sub convert_chars
{ my $str = shift;
$str =~ s/<div class='mf-viral'.*$//;
$str =~ s/"/"/g;
$str =~ s/&#215;/x/g;
$str =~ s/'/'/g;
$str =~ s/&shy;/-/g;
$str =~ s/<div class='mf-viral'.*$//;
$str =~ s/<script/<Xscript/g;
$str =~ s/<code/<Xcode/g;
###############################################
# Unicode. #
###############################################
$str =~ s/\xe2\x99/ /g;
$str =~ s/\xe2\x80\x93/-/g;
$str =~ s/\xe2\x80\x99/'/g;
$str =~ s/\xe2\x80\xa6/.../g;
$str =~ s/\xc2\xa0\x20/ /g;
$str =~ s/&[lr]quote;/"/g;
$str =~ s/>/>/g;
$str =~ s/</</g;
$str =~ s/&yen;/Y/g;
$str =~ s/&#8216;/'/g;
$str =~ s/&hellip;/.../g;
$str =~ s/&lang;/L</g;
$str =~ s/&rang;/>/g;
$str =~ s/&amp;/&/g;
$str =~ s/<!--.*-->//g;
$str =~ s/<\/p>//g;
$str =~ s/\x80//g;
$str =~ s/\r//g;
$str =~ s/\t/ /g;
$str =~ s/ +/ /g;
$str =~ s/&#039;/'/g;
$str =~ s/&#237;/í/g;
$str =~ s/í/í/g;
return $str;
}
######################################################################
# Dont let disk fill up. #
######################################################################
sub do_clean
{
my @lst = reverse(glob("$opts{dir}/articles/*"));
my $dirty = 0;
my $today = strftime("$opts{dir}/archive/%Y%m%d", localtime());
for (my $i = $opts{num}; $i < @lst; $i++) {
my $a = basename($lst[$i]);
$a =~ s/^art//;
$a =~ s/^0*//;
$a = 0 if $a eq '';
$dirty = 1;
my $target = "$today/" . basename($lst[$i]);
if ( ! -d $today) {
if (!mkdir($today, 0755)) {
print "error:mkdir($today, 0755) - $!\n";
}
}
if (!rename($lst[$i], $target)) {
print time_string() . "Failed to rename: $lst[$i] -> $target - $!\n";
}
}
if (@art_list > $opts{art_history}) {
my $n = 0;
for (my $i = $opts{art_history}; $i < @art_list; $i++) {
my $id = $art_list[$i]->{id};
$n++;
delete($art_hash{$id});
}
print time_string() . "Cleaned $n articles\n";
@art_list = @art_list[0..$opts{art_history}-1];
save_art_hash() if $dirty;
}
}
######################################################################
# Generate the output page. #
######################################################################
sub do_output
{
my @lst = reverse(glob("$opts{dir}/articles/*"));
my $i = 0;
my $pg = 0;
my $size = 0;
my @secs;
for ( ; $i < @lst; $pg++) {
my $html = '';
my $txt = '';
my $cont = '<table>';
$html .= $javascript;
my $site = '';
my $last_site = '';
my $ent = 0;
my $last_ent = 0;
my $sec1 = '';
my $sec2 = '';
my $hide = '';
my %info;
for (; $i < scalar(@lst); $i++) {
my $fname = $lst[$i];
#print "Output: $fname\n";
#print "$fname $size sec1=", length($sec1), " sec2=", length($sec2), "\n";
%info = ();
my $fh = new FileHandle($fname);
my $mtime = (stat($fname))[9];
my $tstr = strftime("%d %a %H:%M", localtime($mtime));
while (<$fh>) {
chomp;
next if $_ eq '';
my $lh = $_;
$lh =~ s/=.*$//;
if (length($lh) + 1 > length($_)) {
print "$fname\n";
print "length error: $_\n";
}
my $rh = substr($_, length($lh) + 1);
$info{$lh} = $rh;
if ($lh eq 'body') {
while (<$fh>) {
$info{body} .= $_;
}
foreach my $t (qw/body title/) {
$info{$t} = convert_chars($info{$t});
}
last;
}
}
###############################################
# Generate article. #
###############################################
if (!defined($info{site})) {
print "missing info{site} for $fname\n";
unlink($fname);
next;
}
if (!defined($sites{$info{site}})) {
# print "ignore: $fname ($info{site}\n";
next;
}
#print "$info{site} vs $site\n";
if ($info{site} ne $last_site) {
if ($last_site) {
my $n = $ent - $last_ent;
my $m = '';
#$m = sprintf(" %dK", (length($sec1) + length($sec2)) / 1024);
$sec1 =~ s/\[XX]/[$n$m]/;
$size += length($sec1) + length($sec2);
push @secs, $sec1 . $sec2;
#print "#$last_ent $info{name}\n";
}
$last_site = $info{site};
$site = $info{site};
$sec1 = '';
$sec2 = '';
$sec1 .= "$hide</pre></strong></b></i></ul></div>\n";
$sec1 .= "<br>\n";
$sec1 .= "<div id='a${ent}p'>";
$sec1 .= " ";
$sec1 .= "<a href='#' onclick=\"toggle('a$ent'); return false;\">Show</a> ";
$sec1 .= "$tstr\n";
$sec1 .= "<a name='$ent'>";
$sec1 .= "<a href='" . ($sites{$site}{site} || $site) . "'>";
$sec1 .= "$info{name}</a> [XX]";
$sec1 .= "</div>\n";
$hide = "<br><span style='background-color:#ff9040;'><a href='#' onclick=\"toggle('a$ent'); return false;\">Hide</a></span>";
$sec1 .= "<div id='a$ent' style=\"display: none;\">";
$last_ent = $ent;
}
$sec2 .= "<p>$info{date3} ";
$sec2 .= "<a href='$info{link}'><b>$info{title}</b></a><br>";
$sec2 .= "$info{body}";
$txt .= "$info{date3} Title: $info{title}\n";
$txt .= " $info{body}\n";
$txt .= " $info{link}\n";
if ($size + length($sec1) + length($sec2) > $opts{size} * 1024) {
#print "bump to next $size sec1=", length($sec1), " sec2=", length($sec2), " fname=$fname\n";
$sec2 .= "<p><b>results truncated...\n";
last;
}
$ent++;
}
###############################################
# Generate output file. #
###############################################
my $ofname = $pg == 0 ? "p" : "p$pg";
my $fh = new FileHandle(">$opts{dir}/$ofname.html");
# print $fh $cont;
my $s = $html;
$s .= $_ foreach @secs;
printf $fh strftime("Generated %d %a %H:%M", localtime()) . " Size: %dK",
length($s) / 1024;
print $fh " Articles: $ent ";
if ($pg) {
print $fh " <a href='p" . ($pg == 1 ? "" : $pg-1) . ".html'>Prev</a>";
}
if ($i < @lst) {
print $fh " <a href='p" . ($pg+1) . ".html'>Next</a>";
}
print $fh "<hr>";
print $fh <<EOF;
<p>
If you want to see or try CRiSP, visit
<a href='http://www.crispeditor.co.uk'>http://www.crispeditor.co.uk</a>
and try one of the longest established and functional editors on
the web! Or subscribe to <a href='http://crtags.blogspot.com'>http://crtags.blogspot.com</a> for
a blog on technical articles and joy of computing (sometimes!).
<p>
EOF
print $fh "<hr>";
print $fh $html;
print $fh $_ foreach @secs;
print $fh "</div><hr>rss.pl, Author: Paul Fox $copyright_year crispeditor-at-gmail-com\n";
$fh = new FileHandle(">$opts{dir}/$ofname.txt");
print $fh $txt;
print time_string() . "$ofname.html: ", length($html) . " articles: $ent\n";
###############################################
# Post processing. #
###############################################
$size = 0;
@secs = ();
}
gen_status();
}
######################################################################
# Parse the html/xml files into articles. #
######################################################################
sub do_parse
{
my @stack;
my $uri;
foreach my $fname (glob("$ENV{HOME}/.rss/sites/*")) {
my $site = basename($fname);
if (!defined($sites{$site})) {
unlink($fname);
next;
}
my %info;
my $fh = new FileHandle($fname);
$uri = <$fh>;
chomp($uri);
#print "$fname: ($uri)\n";
my $str = '';
while (<$fh>) {
$str .= $_;
}
my $sec = '';
my $num_art = 0;
my $tot_art = 0;
for (my $i = 0; $i < length($str); $i++) {
my $ch = substr($str, $i, 1);
if ($ch ne '<') {
$info{$sec} .= $ch;
next;
}
$ch = substr($str, ++$i, 1);
if ($ch eq '!') {
for ($i++; $i < length($str); $i++) {
if (substr($str, $i, 3) eq ']]>') {
$i += 2;
last;
}
$info{$sec} .= substr($str, $i, 1);
}
next;
}
my $ch0 = $ch;
$sec = '';
for (; $i < length($str); $i++) {
my $ch = substr($str, $i, 1);
last if $ch eq '>';
$sec .= $ch;
if ($sec eq '[CDATA[') {
$sec = '';
for ($i++; $i < length($str); $i++) {
$ch = substr($str, $i, 1);
$sec .= $ch;
last if $sec =~ /]]$/;
}
}
}
#print "sec=$sec\n";
###############################################
# www.register.co.uk #
###############################################
if ($sec =~ /link.*href="([^"]*)"/) {
my $lnk = $1;
$lnk =~ s///\//gi;
$lnk =~ s/&/\&/g;
$info{link} = $lnk;
$sec = '';
next;
}
$sec =~ s/ .*$//;
if ($sec eq 'item' || $sec eq 'entry') {
%info = ();
next;
}
#print "yes - '$sec'\n";
$sec =~ s/ .*$//;
next if $sec !~ /\/(item|entry)$/;
###############################################
# Check for HotUKDeals categories. #
###############################################
next if $sites{$site}{category} &&
$info{category} &&
$info{category} !~ /$sites{$site}{category}/;
$info{title} ||= "NO-TITLE";
$info{title} =~ s/\t\t/ /g;
$info{title} =~ s/\[CDATA\[//;
if ($sites{$site}{ignore_title} &&
$info{title} =~ /$sites{$site}{ignore_title}/i) {
# print time_string() . "Ignore_title: $info{title}\n";
next;
}
$info{description} = $info{summary} if $info{summary};
$info{description} = $info{content} if $info{content};
#print "content=", substr($info{content}, 0, 100), "\n" if $info{content};
#print "link=$info{link}\n" if $info{content};
if (!defined($info{description})) {
print time_string() . "ERROR(parse): $fname - no description\n";
next;
}
$info{description} =~ s/\[CDATA\[//;
$info{description} =~ s/<img[^>]*>//;
$info{description} =~ s/</</g;
$info{description} =~ s/>/>/g;
$info{description} =~ s/€/e/g;
$info{description} =~ s/&#8217;/'/g;
$info{description} =~ s/&#8220;/'/g;
$info{description} =~ s/&#8221;/'/g;
$info{description} =~ s/&nbsp;/ /g;
$info{description} =~ s/\xe2\x80\x93/-/g;
$info{description} =~ s/—/-/g;
$info{description} =~ s/–/-/g;
$info{description} =~ s/"/"/g;
$info{description} =~ s/&rsquo;/'/g;
$info{description} =~ s/&mdash;/-/g;
$info{description} =~ s/<img [^>]*>//g;
$info{description} =~ s/<iframe [^>]*>//g;
$info{description} =~ s/ at Slashdot\.//g;
$info{description} =~ s/&lt;/</g;
$info{description} =~ s/<div class='mf-viral'.*$//;
$info{description} =~ s/&gt;/>/g;
$info{description} =~ s/http:\/\/twitter[^"]*"/"/;
$info{description} =~ s/http:\/\/www.facebook[^"]*"/"/;
$info{description} =~ s/http:\/\/plus.google[^"]*"/"/;
###############################################
# Slashdot... #
###############################################
$info{description} =~ s/<div class="share_submission".*$//;
###############################################
# HotUKDeals. #
###############################################
$info{description} =~ s/^Found by .*<a[^<]*<\/a><br\/>//;
###############################################
# Get date. #
###############################################
my $d = $info{pubDate};
if ($d) {
$d =~ s/^.* (\d\d:\d\d):.*$/$1/;
} elsif ($info{"dc:date"}) {
$d = $info{"dc:date"};
$d =~ s/^.*T//;
} elsif ($info{"updated"}) {
$d = $info{"updated"};
$d =~ s/^.*T//;
} else {
$d = "--";
}
$d =~ s/Z//;
###############################################
# Compile to standard form. #
###############################################
my %art;
$art{title} = $info{title};
$art{link} = $info{link} || $info{uri};
$art{link} =~ s/^\s+//;
$art{link} =~ s/\s+$//;
$art{link} =~ s///\//g;
#print "cat=$info{category} -- $sites{$site}{category}\n" if $site eq 'HotUKDeals';
#$art{title} .= "cat=" . ($info{category} || '');
$art{text} = $info{description};
#$art{text} .= "cat=" . ($info{category} || '');
$art{id} = $a_id;
$art{src} = $site;
$art{name} = $sites{$uri}{name};
$art{site} = $uri;
%info = ();
$art{title} =~ s/[\r\n]//g;
$art{link} =~ s/[\r\n]//g;
###############################################
# Skip if we have seen this before. #
###############################################
#if (defined($art_hash{$art{link}})) {
#print "dup art $art{link}\n";
#}
$tot_art++;
next if defined($art_hash{$art{link}});
$num_art++;
$a_id++;
my $fname = sprintf("$opts{dir}/articles/art%07d", $a_id);
my $fh = new FileHandle(">$fname");
die "Cannot create $fname - $!" if !$fh;
print $fh "id=$art{id}\n";
print $fh "site=$art{site}\n";
print $fh "date1=" . time() . "\n";
print $fh "date2=" . strftime("%Y%m%d %H:%M:%S", localtime()) . "\n";
print $fh "date3=" . strftime("%H:%M", localtime()) . "\n";
print $fh "date4=$d\n";
print $fh "link=$art{link}\n";
print $fh "name=", $art{name} || "", "\n";
print $fh "title=$art{title}\n";
print $fh "body=\n";
print $fh $art{text};
$art_hash{$art{link}} = $art{id};
my $art_info = {
link => $art{link},
id => $art{id},
time => time()
};
unshift @art_list, $art_info;
}
$sites{$site}{tot_art} += $tot_art;
$sites{$site}{tot_new} += $num_art;
$sites{$site}{mtime} = time() if $num_art;
$sites{$site}{art_new} = $num_art;
$sites{$site}{art_tot} = $tot_art;
print "$site: $num_art/$tot_art\n";
}
my $fname = "$opts{dir}/seq_no";
my $fh = new FileHandle(">" . $fname);
print $fh "$a_id\n";
$fh->close();
save_art_hash();
}
sub gen_status
{
###############################################
# Generate status page. #
###############################################
my $fh = new FileHandle(">$opts{dir}/status.html");
if ($fh) {
print $fh $javascript;
print $fh "<p>\n";
print $fh "<a href='p.html'>Page 1</a> ";
print $fh "b=", $history{req404} || 0, " ";
print $fh "c=", $history{cookie} || 0, " g=$history{googlebot} r=$history{req}<br>\n";
foreach my $k (reverse(sort(keys(%history)))) {
next if $k !~ /^s2/;
printf $fh "%s=%s<br>\n",
substr($k, 1), $history{$k};
}
}
save_history();
}
######################################################################
# Simple cookie code. #
######################################################################
sub get_cookie
{
my $c = $history{cookie}++;
$c = "simple-$c";
return $c;
}
######################################################################
# Get all the hackernews comments. #
######################################################################
sub get_hackernews
{
my $mtime = (stat("$opts{dir}/hackernews/index.html"))[9];
return if $mtime && time() - $mtime < 2 * 3600;
my $hn_dir = "$opts{dir}/hackernews";
mkdir($hn_dir, 0755);
my @lst = reverse(glob("$opts{dir}/articles/*"));
foreach my $fname (@lst) {
my $info = read_article($fname);
next if ($info->{site} || '') ne "HackerNews";
my $uri = $info->{body};
$uri =~ s/^.*(https:[^"]*)".*/$1/s;
my $id = $uri;
$id =~ s/^.*id=//;
###############################################
# Dont get file if its quite old - #
# probably not updating much. #
###############################################
my $tmp = "$hn_dir/$id";
my $mtime = (stat($tmp))[9];
next if $mtime && time() - $mtime > 30;
print "$fname $uri\n";
my $cmd = "curl -k '$uri' >>$tmp 2>/dev/null";
spawn($cmd);
}
###############################################
# Create index #
###############################################
my %idx;
foreach my $fname (reverse(glob("$hn_dir/*"))) {
next if basename($fname) !~ /^\d+$/;
my $fh = new FileHandle($fname);
my $title = "";
my $slink = '';
my $cmts;
my $score = 0;
while (<$fh>) {
if (!$slink &&
/<a href="(http[^"]*)"/ && $1 !~ /http:\/\/www.ycombinator/) {
$slink = $1;
}
if (/<title>(.*)<\/title>/) {
$title = $1;
$title =~ s/ \| Hacker News//;
next;
}
if (/>(\d+) comment/) {
$cmts = $1;
}
if (/">(\d+) point/) {
#print "got score $fname $1\n";
$score = $1;
}
last if $cmts && $score;
}
###############################################
# Keep it if it looks good. #
###############################################
#if (!defined($cmts)) {
#die "$fname";
#}
if ($cmts && $cmts > 10) {
$idx{$fname}{cmt} = $cmts;
$idx{$fname}{score} = $score;
$idx{$fname}{title} = $title;
$idx{$fname}{size} = (stat($fname))[7];
$idx{$fname}{slink} = $slink;
}
}
my $last_mo = '';
my $ofh;
my $idx = "";
my %seen;
foreach my $f (sort(keys(%idx))) {
my $mtime = (stat($f))[9];
my $this_mo = strftime("%Y%m", localtime($mtime));
if (!defined($seen{$this_mo})) {
$idx .= "<a href='/hackernews/$this_mo.html'>$this_mo</a> ";
$seen{$this_mo} = '';
}
}
foreach my $m (keys(%seen)) {
$seen{$m} = "<p>$idx<p><table>\n";
}
foreach my $f (reverse(sort(keys(%idx)))) {
my $mtime = (stat($f))[9];
my $this_mo = strftime("%Y%m", localtime($mtime));
my $t = $idx{$f}{title};
$t = substr($t, 0, 54) . "..." if length($t) > 54;
# printf "%3d %3dKB %3d | %s\n",
# $idx{$f}{cmt}, $idx{$f}{size} / 1024,
# $idx{$f}{score},
# $t,
# basename($f);
my $str = '';
$str .= "<tr>\n";
$str .= "<td>" . strftime("%Y%m%d", localtime($mtime)) . "</td>";
$str .= sprintf("<td width=40 align=right>%d</td>",
$idx{$f}{cmt});
$str .= sprintf("<td width=40 align=right>%dKB</td>",
$idx{$f}{size} / 1024);
$str .= sprintf("<td width=40><a href='%s'>Link</a></td>",
$idx{$f}{slink});
$str .= sprintf("<td><a href='%s'>%s</td>\n",
basename($f),
$idx{$f}{title});
$seen{$this_mo} .= $str;
}
my $str = '';
foreach my $m (sort(keys(%seen))) {
$ofh = new FileHandle(">$opts{dir}/hackernews/$m.html");
$str = $seen{$m};
print $ofh $seen{$m};
print $ofh "<p>Bytes: ", length($str), "\n";
}
$ofh = new FileHandle(">$opts{dir}/hackernews/index.html");
print $ofh $str;
print $ofh "<p>Bytes: ", length($str), "\n";
}
######################################################################
# Get a page or the next page. #
######################################################################
sub get_page
{ my $epoch = shift;
my @lst = reverse(glob("$opts{dir}/articles/*"));
my $i = 0;
my $pg = 0;
my $size = 0;
my @secs;
my $html = $javascript;
for ( ; $i < @lst; $pg++) {
my $html = '';
my $txt = '';
my $site = '';
my $last_site = '';
my $ent = 0;
my $last_ent = 0;
my $sec1 = '';
my $sec2 = '';
my %info;
for (; $i < scalar(@lst); $i++) {
my $fname = $lst[$i];
#print "Output: $fname\n";
%info = ();
my $fh = new FileHandle($fname);
my $mtime = (stat($fname))[9];
my $tstr = strftime("%d %a %H:%M", localtime($mtime));
my $ignore = 0;
while (<$fh>) {
chomp;
next if $_ eq '';
my $lh = $_;
$lh =~ s/=.*$//;
if (length($lh) + 1 > length($_)) {
print "$fname\n";
print "length error: $_\n";
}
my $rh = substr($_, length($lh) + 1);
if ($epoch && $lh eq 'date1' && $rh < $epoch) {
$ignore = 1;
last;
}
#print "epoch=$epoch lh=$lh rh=$rh\n";
$info{$lh} = $rh;
if ($lh eq 'body') {
while (<$fh>) {
$info{body} .= $_;
}
foreach my $t (qw/body title/) {
$info{$t} = convert_chars($info{$t});
}
last;
}
}
###############################################
# Generate article. #
###############################################
#print "$info{site} vs $site\n";
last if $ignore;
if (!defined($sites{$info{site}})) {
# print "ignore: $fname ($info{site}\n";
next;
}
if ($info{site} ne $last_site) {
if ($last_site) {
my $n = $ent - $last_ent;
$sec1 =~ s/\[XX]/[$n]/;
$size += length($sec1) + length($sec2);
push @secs, $sec1 . $sec2;
#print "#$last_ent $info{name}\n";
}
$last_site = $info{site};
$site = $info{site};
$sec1 = '';
$sec2 = '';
$sec1 .= "</div>\n";
$sec1 .= "<br>\n";
$sec1 .= "<div id='a${ent}p'>";
$sec1 .= " ";
$sec1 .= "<a href='#' onclick=\"toggle('a$ent'); return false;\">Show</a> ";
$sec1 .= "$tstr\n";
$sec1 .= "<a name='$ent'><a href='$sites{$site}{site}'>$info{name}</a> [XX]";
$sec1 .= "</div>\n";
$sec1 .= "<div id='a$ent' style=\"display: none;\">";
$last_ent = $ent;
}
$sec2 .= "<p>$info{date3} ";
$sec2 .= "<a href='$info{link}'><b>$info{title}</b></a><br>";
$sec2 .= "$info{body}";
$txt .= "$info{date3} Title: $info{title}\n";
$txt .= " $info{body}\n";
$txt .= " $info{link}\n";
if ($size + length($sec1) + length($sec2) > $opts{size} * 1024) {
$sec2 .= "<p><b>results truncated...\n";
last;
}
$ent++;
}
###############################################
# Generate output file. #
###############################################
my $secs = join("", @secs);
my $page = sprintf(strftime("Generated %d %a %H:%M", localtime()) . " Size: %dK",
(length($html) + length($secs)) / 1024);
$page .= " Articles: $ent ";
if ($pg) {
$page .= " <a href='p" . ($pg == 1 ? "" : $pg-1) . ".html'>Prev</a>";
}
if ($i < @lst) {
$page .= " <a href='p" . ($pg+1) . ".html'>Next</a>";
}
$page .= "<hr>" . $html;
$html = $page .
$html .
$secs .
"</div><hr>rss.pl, Author: Paul Fox $copyright_year crispeditor-at-gmail-com\n";
return $html if $epoch;
###############################################
# Post processing. #
###############################################
$size = 0;
@secs = ();
}
}
######################################################################
# Retrieve page from the web site. #
######################################################################
sub get_rss
{ my $s = shift;