-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhacker_news_logger.pl
60 lines (54 loc) · 1.31 KB
/
hacker_news_logger.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
#! /usr/bin/env perl
#For cron
# 5 * * * * perl ./hacker_news_logger.pl
use warnings;
use strict;
my $file = "./hacker_news_log.txt";
my $webpage = "./hacker_news_log.html";
my $vtime = time - 3*24*60*60; #store last 3 days
my $leastPoints = 50;
unless (-e $file){
`touch $file`;
}
my %cur_links;
open(my $fdesc, "<", $file);
while(<$fdesc>){
my $title = $_;
chomp $title;
my $link = <$fdesc>;
chomp $link;
my $time = <$fdesc>;
chomp $time;
<$fdesc>;
if($time > $vtime){
$cur_links{$title} = [$link, $time];
}
}
close $fdesc;
my $page = `wget -O - -q news.ycombinator.com`;
#my @matches = ($page =~ m/class="title"><a[^<]*/g);
my @matches = ($page =~ m/class="title"><a.*?points/g);
foreach my $line(@matches){
$line =~ m/([0-9]*) points/;
if($1 < $leastPoints){
next;
}
$line =~ m/.*>(.*?)<\/a>/;
my $title = $1;
$line =~ m/href="(.*?)"/;
my $link = $1;
if ($link =~ m/^item\?id=/){
$link = "http://news.ycombinator.com/".$link;
}
$cur_links{$title} = [$link, time];
}
open(my $write, ">", $file);
open(my $web, ">", $webpage);
my $index = 0;
foreach my $key (sort keys %cur_links){
print $write $key."\n".$cur_links{$key}[0]."\n".$cur_links{$key}[1]."\n\n";
printf $web("%s %s\n", $index, ' <a target="_blank" href="'.$cur_links{$key}[0].'">'.$key.'</a><br/>');
$index+=1;
}
close $web;
close $write;