logo

ap-client

CLI-based client / toolbox for ActivityPub Client-to-Servergit clone https://hacktivis.me/git/ap-client.git

ap-fetch.pl (1673B)


  1. #!/usr/bin/env perl
  2. # AP-Client: CLI-based client / toolbox for ActivityPub
  3. # Copyright © 2020-2021 AP-Client Authors <https://hacktivis.me/git/ap-client/>
  4. # SPDX-License-Identifier: BSD-3-Clause
  5. use strict;
  6. use utf8;
  7. no warnings; # Wide Character…
  8. use Getopt::Std;
  9. use LWP::UserAgent;
  10. use HTTP::Request::Common;
  11. my %options=();
  12. my $ua = LWP::UserAgent->new;
  13. getopts("rju:", \%options);
  14. if($#ARGV != 0) {
  15. print "usage: ap-fetch.pl [-r|-j|-u user:pass] <url>\n";
  16. print " -j Pipe into jq(1)\n";
  17. print " -r Raw output\n";
  18. print " -u user:pass HTTP Basic Auth credentials\n";
  19. print "By default, when -j and -r are absent it pipes the data into ap-represent.pl.\n";
  20. exit 1;
  21. }
  22. $ua->agent("AP-Client fetch <https://hacktivis.me/git/ap-client/>");
  23. my $req = HTTP::Request->new(GET => $ARGV[0]);
  24. $req->header('Accept' => 'application/activity+json');
  25. if(defined $options{u}) {
  26. my ($user, $password) = split(/:/, $options{u});
  27. $req->authorization_basic($user, $password);
  28. }
  29. my $res = $ua->request($req);
  30. if($res->is_success) {
  31. my $content_type = $res->header("Content-Type");
  32. my $content_match = /application\/([^+]*+)?json(; .*)?/;
  33. if($content_type =~ $content_match) {
  34. if(defined $options{r}) {
  35. print $res->content;
  36. } elsif(defined $options{j}) {
  37. open(my $pipe_out, '|-', 'jq .') or die "Couldn't open a pipe into jq: $!";
  38. print $pipe_out $res->content;
  39. } else {
  40. open(my $pipe_out, '|-', 'ap-represent.pl') or die "Couldn't open a pipe into ap-represent.pl: $!";
  41. print $pipe_out $res->content;
  42. }
  43. } else {
  44. print "Got $content_type instead of $content_match";
  45. }
  46. } else {
  47. print "Got ", $res->status_line, " instead of 2xx\n";
  48. }