diff options
author | Andinus <andinus@nand.sh> | 2020-11-24 13:48:59 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-11-24 13:48:59 +0530 |
commit | 514a17069dbb06ffd272a2d57935fa8e8681969e (patch) | |
tree | 959bb2b76bbd45235f5b1c77cda48c07228278a0 | |
parent | fd9bde6f64c8d792c555678dc1dee8ae3665156e (diff) | |
download | draco-0.2.1.tar.gz |
Update instructions in README, add comments in draco.pl, fix bug v0.2.1
$comment in print_comment_chain() was changed to $comment_data to prevent confusion in future. We have to check if url has "/" at the end because if it does have it then previously we were appending "$comment_data->{id}.json" to it which makes the url look like: http://<reddit>/<post_id>//<comment_id>.json And reddit doesn't like that, reddit wants it to be: http://<reddit>/<post_id>/<comment_id>.json Notice the double '/' in previous link.
-rw-r--r-- | README.org | 33 | ||||
-rw-r--r-- | draco.1 | 2 | ||||
-rwxr-xr-x | draco.pl | 46 |
3 files changed, 57 insertions, 24 deletions
diff --git a/README.org b/README.org index ab1273f..2ad7f8c 100644 --- a/README.org +++ b/README.org @@ -20,15 +20,19 @@ This was recorded with =asciinema(1)=. [[https://asciinema.org/a/373860][https://asciinema.org/a/373860.png]] + Draco v0.1.2: https://asciinema.org/a/373860 - - alt-link: https://andinus.nand.sh/static/draco/v0.1.2.cast + Draco 2020-11-19: https://asciinema.org/a/373851 - - alt-link: https://andinus.nand.sh/static/draco/2020-11-19.cast + ++ alt-links (download) + - v0.1.2: https://andinus.nand.sh/static/draco/v0.1.2.cast + - 2020-11-19: https://andinus.nand.sh/static/draco/2020-11-19.cast * Installation Follow these instructions to get draco & then install the dependencies, they're listed below. All dependencies are in Debian & Fedora repositories. Check the /News/ section before updating or downloading latest release. +** Release +Release archives are generated by cgit/GitHub. 1. Download the release: - https://git.tilde.institute/andinus/draco @@ -37,17 +41,32 @@ Check the /News/ section before updating or downloading latest release. 3. =cd= into the directory. 4. Run =make install= as root. 5. Install dependencies. +** From Source +All commits will be signed by my [[https://andinus.nand.sh/static/D9AE4AEEE1F1B3598E81D9DFB67D55D482A799FD.asc][PGP Key]]. + +#+BEGIN_SRC sh +# Clone the project. +git clone https://git.tilde.institute/andinus/draco +cd draco + +# Install draco. Use `sudo' if `doas' is not present. +doas make install + +# Install dependencies. See the section below. +#+END_SRC * Dependencies +** OpenBSD #+BEGIN_SRC sh -# Install dependencies. (OpenBSD) doas pkg_add p5-Unicode-LineBreak p5-JSON-MaybeXS cpan install HTTP::Tiny - -# Install dependencies. (Debian - apt based) +#+END_SRC +** Debian (apt) +#+BEGIN_SRC sh sudo apt install libunicode-linebreak-perl libjson-maybexs-perl \ libhttp-tiny-perl - -# Install dependencies. (Fedora - dnf based) +#+END_SRC +** Fedora (dnf) +#+BEGIN_SRC sh sudo dnf install perl-JSON-MaybeXS perl-HTTP-Tiny perl-Unicode-LineBreak #+END_SRC * News diff --git a/draco.1 b/draco.1 index a9d2856..0363e07 100644 --- a/draco.1 +++ b/draco.1 @@ -6,7 +6,7 @@ .Nd a script to convert reddit thread to Org document .Sh SYNOPSIS .Nm draco -.Op Fl dv +.Op Fl dhv .Ar <url> .Sh DESCRIPTION .Nm diff --git a/draco.pl b/draco.pl index b33fb62..7d3c75c 100755 --- a/draco.pl +++ b/draco.pl @@ -18,7 +18,7 @@ binmode(STDOUT, "encoding(UTF-8)"); die "usage: draco [-dhv] <url>\n" unless scalar @ARGV; my $DEBUG; -my $VERSION = "v0.2.0"; +my $VERSION = "v0.2.1"; # Dispatch table to be parsed before url. my %dispatch = ( '-v' => sub { print "Draco $VERSION\n"; exit; }, @@ -138,22 +138,35 @@ sub get_response { return $response; } +# There are 3 kind of comments. +# +# 1. normal comments (includes top-level comments). +# 2. comments hidden under "load more comments". +# 3. comments hidden under "continue this thread". + # print_comment_chain will print the whole chain of comment while # accounting for level. sub print_comment_chain { - my $comment = shift @_; + # This was earlier called $comment & was changed to $comment_data + # to prevent confusion because it is $comment->{data}. + my $comment_data = shift @_; my $level = shift @_; $counter{print_comment_chain_call}++; - # $comment->{author} & $comment->{body} not being present means - # that it's a shell comment. We can get it by making another HTTP - # call. - unless ($comment->{author}) { - push @shell_comments, $comment->{id}; + # $comment_data->{author} not being present means that it's a + # comment hidden under "load more comments". We can get it by + # making another HTTP call. + unless ($comment_data->{author}) { + push @shell_comments, $comment_data->{id}; return unless $ENV{FETCH_ALL}; unless ( eval { - my $json_url = "${url}/$comment->{id}.json?limit=500&sort=top"; + # It'll fail if we fetch "${url}/$comment_data->{id}.json" + # & ${url} already has "/" at the end. So, we check if "/" + # is present, if not then we add it. + my $json_url = $url; + $json_url .= "/" unless substr $url, -1 eq "/"; + $json_url .= "$comment_data->{id}.json?limit=500&sort=top"; # Fetch the comment. my $response = get_response($json_url); @@ -176,7 +189,8 @@ sub print_comment_chain { return 1; } ) { - print STDERR "parsing shell comment: $comment->{id} : failed\n"; + my $err = $@; + print STDERR "parsing `$comment_data->{id}' failed: $err\n"; } # This comment thread has been parsed, move on to the text @@ -184,8 +198,8 @@ sub print_comment_chain { return; } - print "*" x ($level + 2), " ", "$comment->{author}"; - print " [S]" if $comment->{is_submitter}; + print "*" x ($level + 2), " ", "$comment_data->{author}"; + print " [S]" if $comment_data->{is_submitter}; print "\n"; # Print comment details. @@ -193,20 +207,20 @@ sub print_comment_chain { foreach my $detail (qw( created_utc author permalink upvote_ratio ups downs score edited is_submitter stickied controversiality )) { - print ":${detail}: =$comment->{$detail}=\n" - if scalar $comment->{$detail}; + print ":${detail}: =$comment_data->{$detail}=\n" + if scalar $comment_data->{$detail}; } print ":END:\n"; print "\n#+BEGIN_SRC markdown\n", # Break the text at 76 column & add 2 space before every new # line. - " ", $lb->break($comment->{body}) =~ s/\n/\n\ \ /gr, "\n", + " ", $lb->break($comment_data->{body}) =~ s/\n/\n\ \ /gr, "\n", "#+END_SRC\n"; # If the comment has replies then iterate over those too. - if (scalar $comment->{replies}) { - foreach my $reply ($comment->{replies}->{data}->{children}->@*) { + if (scalar $comment_data->{replies}) { + foreach my $reply ($comment_data->{replies}->{data}->{children}->@*) { if ($reply->{kind} eq "more" and $reply->{data}->{id} eq "_") { $counter{skipped_due_to_more}++; |