/
portald
executable file
·224 lines (171 loc) · 5.29 KB
/
portald
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
#!/usr/bin/perl -w
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2001 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$
###############################################################################
# portald - this is the "daemon" responsible for retrieving portal and site
# block content
###############################################################################
=head1 Welcome to Portald
portald is the script that sucks down headlines from assorted
places on the internet, and puts them in the boxes for use on
Slashdot. Exciting? Nope.
=cut
use strict;
# this needs to be a global here so we can add to it from externally
# called scripts (see portald-site)
use vars qw(%savedBlocks);
use File::Spec::Functions;
use LWP::UserAgent;
use HTTP::Request;
use URI::Escape;
use XML::RSS;
use Slash;
use Slash::Utility;
my $virtual_user = $ARGV[0];
createEnvironment($ARGV[0]);
my $constants = getCurrentStatic();
my $slashdb = getCurrentDB();
my $totalChangedStories = 1;
################################################################################
sub geturl {
my $ua = new LWP::UserAgent;
my $request = new HTTP::Request('GET', $_[0]);
$ua->proxy(http => $constants->{http_proxy}) if $constants->{http_proxy};
$ua->timeout(30);
my $result = $ua->request($request);
if ($result->is_success) {
return $result->content;
} else {
return 0;
}
}
################################################################################
sub getTop10Comments {
my $A = $slashdb->getTop10Comments();
my $block;
foreach (@$A) {
my($sid, $title, $cid, $subj, $d, $nickname, $points) = @$_;
$block .= <<EOT;
<LI><B><A HREF="$constants->{rootdir}/comments.pl?sid=$sid&cid=$cid">$subj</A>
($points points) by $nickname</B> on $d <FONT SIZE="1">attached to
<A HREF="$constants->{rootdir}/article.pl?sid=$sid">$title</A></FONT>
EOT
}
setblock("top10comments", $block);
}
#################################################################
sub getSlashdotPoll {
setblock("poll", pollbooth("", 1));
}
#################################################################
sub portaldLog {
doLog('portald', \@_);
}
=head2 Fortune
The fortune command.
=cut
#################################################################
sub getUptime {
my $x = `/usr/bin/uptime`;
$x = "<B>time:</B> $x";
$x =~ s/up/\n<BR><B>uptime:<\/B>/g;
$x =~ s/load average:/\n<BR><B>load average:<\/B>/;
my $ps = `/bin/ps aux | /usr/bin/wc -l`;
$ps--;
$x .= "<BR><B>processes:</B> $ps <BR>";
my $stats = $x;
my($today, $yesterday) = $slashdb->getAccesLogCountTodayAndYestarday();
my $th = $slashdb->getVar('totalhits', 'value');
my $tc = $slashdb->getVar('totalComments', 'value');
$stats .= "<B>yesterday:</B> $yesterday<BR>
<B>today:</B> $today<BR>
<B>ever:</B> $th<BR>";
setblock('uptime', $stats);
}
#################################################################
sub setblock {
my($bid, $block) = @_;
$savedBlocks{$bid} = $block;
portaldLog("updated $bid");
}
#################################################################
sub getRDF {
#gets an RDF file, and formats it as a /. block
my($bid, $url, $other) = @_;
my $rss = new XML::RSS;
my $d = geturl($url);
if (!$d) {
portaldLog("failed to get $bid: $@");
return;
}
$d =~ s/&(?!#?[a-zA-Z0-9]+;)/&/g;
eval { $rss->parse($d) };
if ($@) {
portaldLog("$bid did not parse properly:$@");
return;
} else {
my $str;
foreach my $item (@{$rss->{items}}) {
# hopefully this xmldecode() will break nothing
my($link, $title) = map { xmldecode($_) } @{$item}{'link', 'title'};
$str .= qq!<LI><A HREF="$link">$title</A></LI>\n!;
}
setblock($bid, "$str$other");
return 1;
}
}
#################################################################
# obsoleted by the Slashd task refresh_sectionindex.pl
#sub getSectionMenu2 {
#
# my $menu = "\n\n<!-- begin section index block -->\n\n";
# my $section_menu = $slashdb->getSectionMenu2();
# for (@$section_menu) {
# my $s = $_->[0];
# my($month, $day, $count) = $slashdb->getSectionMenu2Info($s);
#
# $menu .= "\n$month/$day" if $month && $day;
# $menu .= " ($count)" if $count > 1;
# $menu .= <<EOT;
#<BR>
#<B><A HREF="$constants->{rootdir}/index.pl?section=$s">$s</A></B><BR>
#EOT
# }
# $menu .= "\n\n<!-- end section index block -->\n\n";
#
# setblock("sectionindex", $menu);
#}
#################################################################
# wow, now it's time to actually do something
doLogInit('portald');
portaldLog("Launching Portald");
$|++;
my $p_site = catfile($constants->{datadir}, 'sbin', 'portald-site');
if (-e $p_site) {
portaldLog("requiring $p_site");
require $p_site;
}
portaldLog("Updating Portal Box Thingees");
# Obsoleted by slashd Task refresh_sectionindex.pl
#getSectionMenu2();
# loop through all the RDF sites
my $RDFlist = $slashdb->getSitesRDF();
for (@{$RDFlist}) {
my($bid, $url, $rdf) = ($_->[0], $_->[1], $_->[2]);
getRDF($bid, $rdf);
}
getTop10Comments();
setblock("rand", $slashdb->randomBlock());
getSlashdotPoll();
getUptime();
foreach (keys %savedBlocks) {
$slashdb->setBlock($_, { block => $savedBlocks{$_} });
}
# from 'portald-site'
newSemiRandomBlock($slashdb) if defined &newSemiRandomBlock;
portaldLog("Sucessfully Saved Portals");
# Clean up
doLogExit('portald');
__END__