Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.0 KiB

  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use 5.10.0;
  5. use File::HomeDir;
  6. use JSON;
  7. use Data::Dumper;
  8. use Time::Piece;
  9. use Carp;
  10. if ($ARGV[0] eq 'tags') {
  11. print tags();
  12. } else {
  13. my $ISO_8601_FMT = '%Y-%m-%d';
  14. print recent(Time::Piece->strptime('2020-03-02', $ISO_8601_FMT));
  15. }
  16. sub tags {
  17. # See the bit about auth here:
  18. # https://pinboard.in/api/
  19. my $home = File::HomeDir->my_home;
  20. my $token = file_get_contents("${home}/.pinboardrc");
  21. chomp($token);
  22. my $url = 'https://api.pinboard.in/v1/tags/get?format=json&auth_token=' . $token;
  23. my $pinboard_json = `curl -s '${url}'`;
  24. my $JSON = JSON->new->utf8->pretty;
  25. my $pinboard_hashref = $JSON->decode($pinboard_json);
  26. my %tags = %{ $pinboard_hashref };
  27. my $output = '';
  28. foreach my $tag (keys %tags) {
  29. $output .= "$tag\n";
  30. }
  31. return $output;
  32. #
  33. # my @output;
  34. # foreach my $tag (@tags)
  35. # {
  36. # push @output, Dumper($tag);
  37. # }
  38. #
  39. # if (@output > 0) {
  40. # return join "\n", @output;
  41. # }
  42. # return;
  43. }
  44. sub recent {
  45. my ($day) = @_;
  46. # See the bit about auth here:
  47. # https://pinboard.in/api/
  48. my $home = File::HomeDir->my_home;
  49. my $token = file_get_contents("${home}/.pinboardrc");
  50. chomp($token);
  51. my $url = $day->strftime(
  52. 'https://api.pinboard.in/v1/posts/get?dt=%Y-%m-%d&meta=yes&format=json&auth_token='
  53. . $token
  54. );
  55. my $pinboard_json = `curl -s '${url}'`;
  56. my $JSON = JSON->new->utf8->pretty;
  57. my $pinboard_hashref = $JSON->decode($pinboard_json);
  58. my @posts = @{ $pinboard_hashref->{posts} };
  59. my @output;
  60. foreach my $post (@posts)
  61. {
  62. push @output, Dumper($post);
  63. }
  64. if (@output > 0) {
  65. return join "\n", @output;
  66. }
  67. return;
  68. }
  69. # PHP-style file-content grabbing:
  70. sub file_get_contents {
  71. my ($file) = @_;
  72. open my $fh, '<', $file
  73. or croak "Couldn't open $file: $!\n";
  74. my $contents;
  75. {
  76. # line separator:
  77. local $/ = undef;
  78. $contents = <$fh>;
  79. }
  80. close $fh or croak "Couldn't close $file: $!";
  81. return $contents;
  82. }