Skip to content

Commit 79a9c99

Browse files
committed
Initial commit
1 parent b30de26 commit 79a9c99

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

Makefile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Install to /usr/local unless otherwise specified, such as `make
2+
# PREFIX=/app`.
3+
PREFIX?=/usr/local
4+
5+
INSTALL?=install
6+
INSTALL_PROGRAM=$(INSTALL) -Dm 755
7+
INSTALL_DATA=$(INSTALL) -Dm 644
8+
9+
bindir=$(DESTDIR)$(PREFIX)/bin
10+
sharedir=$(DESTDIR)$(PREFIX)/share
11+
12+
# OpenBSD doesn't index /usr/local/share/man by default so
13+
# /usr/local/man will be used.
14+
platform_id != uname -s
15+
mandir != if [ $(platform_id) = OpenBSD ]; then \
16+
echo $(DESTDIR)$(PREFIX)/man; \
17+
else \
18+
echo $(DESTDIR)$(PREFIX)/share/man; \
19+
fi
20+
21+
help:
22+
@echo "targets:"
23+
@awk -F '#' '/^[a-zA-Z0-9_-]+:.*?#/ { print $0 }' $(MAKEFILE_LIST) \
24+
| sed -n 's/^\(.*\): \(.*\)#\(.*\)/ \1|-\3/p' \
25+
| column -t -s '|'
26+
27+
install: draco.pl draco.1 README.org # system install
28+
$(INSTALL_PROGRAM) draco.pl $(bindir)/draco
29+
30+
$(INSTALL_DATA) draco.1 $(mandir)/man1/draco.1
31+
$(INSTALL_DATA) README.org $(sharedir)/doc/draco/README.org
32+
33+
34+
uninstall: # system uninstall
35+
rm -f $(bindir)/draco
36+
rm -f $(mandir)/man1/draco.1
37+
rm -fr $(sharedir)/doc/draco/
38+
39+
.PHONY: install uninstall help

draco.1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.Dd $Mdocdate: November 19 2020 $
2+
.Dt DRACO 1
3+
.Os
4+
.Sh NAME
5+
.Nm draco
6+
.Nd a script to convert reddit thread to Org document
7+
.Sh SYNOPSIS
8+
.Nm draco
9+
.Ar <url>
10+
.Sh DESCRIPTION
11+
.Nm
12+
is a script to convert reddit thread to Org document. It accepts a url
13+
& prints the Org document to STDOUT.
14+
15+
It'll also print comments along with their replies. It's limited by
16+
the reddit API.
17+
.Pp
18+
.Sh AUTHOR
19+
.An Andinus Aq Mt [email protected]

draco.pl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)