Skip to content

Instantly share code, notes, and snippets.

Created August 3, 2017 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/71d97c3b5845df31c6b425393b46a473 to your computer and use it in GitHub Desktop.
Save anonymous/71d97c3b5845df31c6b425393b46a473 to your computer and use it in GitHub Desktop.
[jdv@new-host-2 ~]$ cat hn_job_parse.pl6
use v6.d.PREVIEW;
use HTTP::Tinyish;
use JSON::Pretty;
my $dir = "$*HOME/.$*PROGRAM"; mkdir $dir;
my @ids = <14901313 14901314>;
my $url_format = 'https://hacker-news.firebaseio.com'
~ '/v0/item/%s.json?print=pretty';
sub fetch ($url, Bool :$cache = True) {
$url ~~ /item\/(\d+)\.json/;
my $file = "$dir/$0".IO;
if ! $cache or ! $file.e {
await Promise.in(.002 + 1.rand);
say "fetching $url";
my %res = HTTP::Tinyish.new(:agent<Mozilla/4.0>).get($url);
if (%res<status> == 200) { $file.spurt(%res<content>) }
else { die $url, %res<status> }
} #else { say "have $url" }
$file.slurp;
}
my @items;
#@items.push(from-json(fetch(sprintf($url_format, $_), :!cache))) for @ids;
@items.push(from-json(fetch(sprintf($url_format, $_)))) for @ids;
react { my @jobs;
whenever [@items[0..1]>><kids>>>.Slip.flat.pairs].Supply -> $p {
@jobs .= grep(*.status == Planned);
if @jobs >= 12 {
await Promise.anyof: @jobs
}
@jobs.push: start {
@items[$p.key]
= from-json(fetch(sprintf($url_format, $p.value)));
}
}
await @jobs;
}
say "{@items.elems} total";
say "{@items.grep({.defined}).elems} defined";
[jdv@new-host-2 ~]$
[jdv@new-host-2 ~]$
[jdv@new-host-2 ~]$ RAKUDO_MAX_THREADS=1 perl6 hn_job_parse.pl6
732 total
733 defined
[jdv@new-host-2 ~]$
[jdv@new-host-2 ~]$
[jdv@new-host-2 ~]$ RAKUDO_MAX_THREADS=1 perl6 hn_job_parse.pl6
732 total
733 defined
[jdv@new-host-2 ~]$
[jdv@new-host-2 ~]$
[jdv@new-host-2 ~]$ perl6 hn_job_parse.pl6
734 total
735 defined
[jdv@new-host-2 ~]$
[jdv@new-host-2 ~]$
[jdv@new-host-2 ~]$ perl6 hn_job_parse.pl6
734 total
736 defined
[jdv@new-host-2 ~]$
[jdv@new-host-2 ~]$
[jdv@new-host-2 ~]$ perl6 hn_job_parse.pl6
736 total
732 defined
[jdv@new-host-2 ~]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment