Browse Source

bin/pinboard: refactor, unify output format, LWP::Simple

- Uses LWP::Simple instead of shelling out to curl
  - Prints a one-line tab-separated thing for each post
  - Adds Getopt::Long and some usage info
main
Brennen Bearnes 10 months ago
parent
commit
2a763c4825
1 changed files with 72 additions and 46 deletions
  1. +72
    -46
      home/bin/pinboard

+ 72
- 46
home/bin/pinboard View File

@ -1,59 +1,76 @@
#!/usr/bin/env perl
=pod
=head1 NAME
pinboard - get some stuff from a pinboard account
=head1 USAGE
F<~/.pinboardrc> must contain a username in the format C<username:token>.
# Show some recent stuff:
pinboard
# List all tags:
pinboard tags
# Everything for a specific tag:
pinboard by-tag some-tag
=head1 DESCRIPTION
A wrapper for some Pinboard API calls I use routinely.
Pretty crap.
=head1 AUTHOR
L<Brennen Bearnes|https://p1k3.com/>
=cut
use strict;
use warnings;
use 5.10.0;
use Carp;
use Data::Dumper;
use File::HomeDir;
use Getopt::Long;
use LWP::Simple;
use Pod::Usage;
use JSON;
use Data::Dumper;
use Time::Piece;
use Carp;
use utf8;
use open qw(:std :utf8);
# Handle options, including help generated from the POD above
# (there's nothing else here at the moment, but it's a good place
# to hang things if I want options):
GetOptions(
'help' => sub { pod2usage(0) },
) or pod2usage(2);
if (defined $ARGV[0] && $ARGV[0] eq 'tags') {
print tags();
} elsif (defined $ARGV[0] && $ARGV[0] eq 'by-tag') {
croak "Must supply a tag name"
unless defined $ARGV[1];
print by_tag($ARGV[1]);
print format_posts(by_tag($ARGV[1]));
} else {
my $ISO_8601_FMT = '%Y-%m-%d';
print recent(Time::Piece->strptime('2020-03-02', $ISO_8601_FMT));
}
sub by_tag {
my $tag = shift;
my $token = get_token();
# lol this is terrible
my $url = 'https://api.pinboard.in/v1/posts/all?tag=' . $tag . '&format=json&auth_token=' . $token;
my $pinboard_json = `curl -s '${url}'`;
# print $url;
# print $pinboard_json;
my $JSON = JSON->new->utf8->pretty;
my $pinboard_hashref = $JSON->decode($pinboard_json);
my @posts = @{ $pinboard_hashref };
my @output;
foreach my $post (@posts)
{
# push @output, Dumper($post);
push @output,
"$post->{time}\t$post->{href}\t$post->{tags}\n";
}
return @output;
my $today = localtime;
print format_posts(recent($today));
}
sub tags {
my $token = get_token();
my $url = 'https://api.pinboard.in/v1/tags/get?format=json&auth_token=' . $token;
my $pinboard_json = `curl -s '${url}'`;
my $pinboard_json = get($url);
# https://stackoverflow.com/questions/45941522/parsing-utf-8-json-with-perl
# Strip byte order mark:
@ -68,17 +85,23 @@ sub tags {
$output .= "$tag\n";
}
return $output;
#
# my @output;
# foreach my $tag (@tags)
# {
# push @output, Dumper($tag);
# }
#
# if (@output > 0) {
# return join "\n", @output;
# }
# return;
}
sub by_tag {
my $tag = shift;
my $token = get_token();
# lol this is terrible
my $url = 'https://api.pinboard.in/v1/posts/all?tag='
. $tag
. '&format=json&auth_token=' . $token;
my $pinboard_json = get($url);
# print $url;
# print $pinboard_json;
my $JSON = JSON->new->utf8->pretty;
my $pinboard_hashref = $JSON->decode($pinboard_json);
return @{ $pinboard_hashref };
}
sub recent {
@ -91,17 +114,20 @@ sub recent {
. $token
);
my $pinboard_json = `curl -s '${url}'`;
my $pinboard_json = get($url);
my $JSON = JSON->new->utf8->pretty;
my $pinboard_hashref = $JSON->decode($pinboard_json);
my @posts = @{ $pinboard_hashref->{posts} };
return @{ $pinboard_hashref->{posts} };
}
# Take a list of posts and format them for display
sub format_posts {
my @output;
foreach my $post (@posts)
foreach my $post (@_)
{
# push @output, Dumper($post);
push @output,
"$post->{href}\n$post->{tags}\n$post->{description}\n";
"$post->{time}\t$post->{href}\t$post->{description}\t$post->{tags}";
}
if (@output > 0) {


Loading…
Cancel
Save