|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +use HTTP::Tiny; |
| 7 | +use JSON::MaybeXS; |
| 8 | + |
| 9 | +# Priting UTF-8 to STDOUT. |
| 10 | +binmode(STDOUT, "encoding(UTF-8)"); |
| 11 | + |
| 12 | +die "usage: draco <url>\n" unless scalar @ARGV; |
| 13 | + |
| 14 | +# $url contains the reddit post. |
| 15 | +my $url = shift @ARGV; |
| 16 | +my $json_url = "${url}.json"; |
| 17 | + |
| 18 | +my $http = HTTP::Tiny->new( verify_SSL => 1 ); |
| 19 | + |
| 20 | +# Fetch the post. |
| 21 | +my $response = $http->get($json_url); |
| 22 | +die "Unexpected response - $response->{status}: $response->{reason}" |
| 23 | + unless $response->{success}; |
| 24 | + |
| 25 | +# Decode json. |
| 26 | +my $json_data = decode_json($response->{content}); |
| 27 | + |
| 28 | +# $post contains post data |
| 29 | +my $post = $json_data->[0]->{data}->{children}->[0]->{data}; |
| 30 | + |
| 31 | +print "* ", "$post->{title}\n"; |
| 32 | + |
| 33 | +# Add various details to :PROPERTIES:. |
| 34 | +print ":PROPERTIES:\n"; |
| 35 | +foreach my $detail (qw( subreddit created_utc author permalink |
| 36 | + upvote_ratio ups downs score )) { |
| 37 | + print ":${detail}: =$post->{$detail}=\n" |
| 38 | + if scalar $post->{$detail}; |
| 39 | +} |
| 40 | +print ":END:\n"; |
| 41 | + |
| 42 | +# Add selftext/url if present. |
| 43 | +print "\n#+BEGIN_SRC markdown\n", "$post->{selftext}\n", "#+END_SRC\n" |
| 44 | + if scalar $post->{selftext}; |
| 45 | +print "$post->{url}\n" if scalar $post->{selftext}; |
| 46 | + |
| 47 | +# $comments contains comment data. We are interested in: replies, |
| 48 | +# author, body, created_utc & permalink. |
| 49 | +my $comments = $json_data->[1]->{data}->{children}; |
| 50 | +# Iterate over top-level comments. |
| 51 | +foreach my $comment ($comments->@*) { |
| 52 | + print_comment_chain($comment->{data}, 0); |
| 53 | +} |
| 54 | + |
| 55 | +# print_comment_chain will print the whole chain of comment while |
| 56 | +# accounting for level. |
| 57 | +sub print_comment_chain { |
| 58 | + my $comment = shift @_; |
| 59 | + my $level = shift @_; |
| 60 | + |
| 61 | + print "*" x ($level + 2), " ", "$comment->{author}\n"; |
| 62 | + |
| 63 | + # Print comment details. |
| 64 | + print ":PROPERTIES:\n"; |
| 65 | + foreach my $detail (qw( created_utc author permalink upvote_ratio |
| 66 | + ups downs score edited is_submitter |
| 67 | + stickied controversiality )) { |
| 68 | + print ":${detail}: =$comment->{$detail}=\n" |
| 69 | + if scalar $comment->{$detail}; |
| 70 | + } |
| 71 | + print ":END:\n"; |
| 72 | + |
| 73 | + print "\n#+BEGIN_SRC markdown\n", "$comment->{body}\n", "#+END_SRC\n"; |
| 74 | + |
| 75 | + # If the comment has replies then iterate over those too. |
| 76 | + if (scalar $comment->{replies}) { |
| 77 | + foreach my $reply ($comment->{replies}->{data}->{children}->@*) { |
| 78 | + print_comment_chain($reply->{data}, $level + 1); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments