When I wanted to set up popularity ranking at my http://oakpointcommunity.org, I decided to expand upon code that I already use here. I like the results, and will someday get around to doing the same thing here.
The first task is to track page visits. I use a Perl database for that. Here's the section that does page tracking:
#!/usr/bin/perl
print "Content-TYPE: text/html\n\n";
$thispage=$ENV{REQUEST_URI};
... (unrelated code)
open(LOCKF, "\>$sitedata/opcsclock");
flock LOCKF,2;
dbmopen(%sites,"$sitedata/opcsitecounts",0666);
dbmopen(%siterips,"$sitedata/opcsiterip",0666);
@exstat=split ":",$sites{$thispage};
my $rip=$ENV{REMOTE_ADDR};
# $siterips{$thispage} looks like this: "82.99.30.50:66.31.145.227:72.74.95.127:65.54.97.151:24.218.209.170:24.60.229.77"
if ($siterips{$thispage} !~ /$rip/) {
bump daily, weekly, monthly, yearly and all-time counters
$exstat[0]++; $exstat[1]++; $exstat[2]++;$exstat[3]++;
$exstat[4]++;
$sites{$thispage}=join ':',@exstat;
@rips=split /:/,$siterips{$thispage};
# keep rips to 5 elements
pop @rips if ($#rips > 4);;
push @rips,$rip;
$rip=join ":",@rips;
$siterips{$thispage}=$rip;
}
dbmclose %sites;
dbmclose %siterips;
flock LOCKF,8;
close LOCKF;
That's simple enough. At midnight a cron job clears out daily counters, resets weekly if it's Sunday, etc:
@exstat=split /:/,$opcsites{$_};
# daily
$exstat[0]=0;
# monthly
$exstat[1]=0 if $mday == 1;
#yearly;
$exstat[2]=0 if $mday == 1 and $mon == 0;
# weekly;
$exstat[3]=0 if $wday == 0 ;
$opcsites{$_}=join ':',@exstat;
}
dbmclose %opcsites;
flock LOCKF,8; close LOCKF;
With these two in place, we have everything needed to produce the "popular" page. Here's the internal code that orders the pages:
#!/usr/bin/perl
print "Content-TYPE: text/html\n\n";
dbmopen(%sites,"$sitedata/opcsitecounts",0666);
@stuff=();
$count=0;
foreach (keys %sites) {
next if /index.html/;
@exstat=split /:/,$sites{$_};
#daily
$today=$exstat[0] * 365;
#monthly
$month=$exstat[1] * 12;
#yearly
$year=$exstat[2] ;
#weekly
$week=$exstat[3] * 4;
$all=$exstat[4] *2 ;
$score=$all + $today + $month + $week + $year;
$foo=sprintf("%.6d $_",$score);
push @stuff,$foo;
}
dbmclose %sites;
$x=0;
@s=();
foreach (reverse sort @stuff) {
@f=split / /;
$count=$f[0];
$least=($count / 7 ) if not $least;
$most=$count if not $most;
$starcount=($count / $most) * 10;
s/.* //;
$late=$_;
open(I,"$sitedata/titles.dat");
$desc="";
while (<I>) {
next if not /$late/;
chomp;
@s=split /\|/;
$desc=$s[1];
}
print "<!-- $late $count\n -->";
next if not $desc;
$x++;
last if $count < $least;
last if $starcount < 2;
last if $x > 15;
print "<li><a href=\"$late\" style=\"text-decoration:none\"\>$desc</a\> ";
print "*" x $starcount;
print "</li>\n";
}
This lets a page with good hits recently compete with sites that have had a lot of hits in the past. It also calculates a "*" count to give a visual indication of relative popularity. Of course you can tweak the multipliers to give more or less weight to the various buckets. One change I implemented involves giving a boost to "today" when it exceeds the average for the day.
More Articles by Anthony Lawrence - Find me on Google+
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar