-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_nix_rfcs.pl
63 lines (52 loc) · 1.47 KB
/
fetch_nix_rfcs.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
#!/usr/bin/env perl
# file: fetch_nix_rfcs.pl
use v5.10;
use strict;
use warnings;
use Data::Dumper;
use LWP;
use HTML::TokeParser::Simple;
use JSON;
use Encode qw(encode_utf8);
use Getopt::Long qw(GetOptions);
$Data::Dumper::Terse = 1;
my %opts = ( list => '', rfc => '' );
GetOptions(
\%opts, qw(
list
rfc
)
);
use constant RFCS =>
'https://raw.githubusercontent.com/NixOS/rfcs/master/rfcs/';
die
"Usage: fetch_nix_rfcs.pl --rfc 0001-rfc-process...\nGet full list: --list\n"
unless $opts{list} || $opts{rfc};
my $p = HTML::TokeParser::Simple->new(
url => "https://github.com/NixOS/rfcs/tree/master/rfcs" );
my $ua = LWP::UserAgent->new;
my $new_agent = 'Mozilla/5.0 (' . $ua->agent . ')';
$ua->agent($new_agent);
if ( $opts{list} ) {
my $data;
while ( my $token = $p->get_token ) {
# This prints all text in an HTML doc (i.e., it strips the HTML)
next unless $token->is_text;
# GitHub stores this shit in JSON payload on the page itself...
$data = JSON->new->utf8->decode( encode_utf8( $token->as_is ) )
if ( $token->as_is =~ "payload" );
}
my $rfcs = $data->{payload}{tree}{items};
foreach my $rfc (@$rfcs) {
my $rfc = Dumper( $rfc->{name} );
say $rfc;
}
}
if ( $opts{rfc} ) {
while ( defined( my $rfc = shift ) ) {
my $url = RFCS . $rfc . ".md";
say "Fetching: $url";
my $response = $ua->get($url);
say $response->decoded_content;
}
}