pingback-client.pl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #! /usr/bin/perl -w
  2. # Copyright (C) 2013 Alex Schroeder <alex@gnu.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. =head1 Pingback from one site to another
  17. If you link from page A on your site to page B on some other site, you can
  18. invoke this script with the command-line arguments A and B. In theory, this will
  19. create a link back from B to A, letting them and all their visitors know that
  20. you wrote something in response.
  21. =cut
  22. use Modern::Perl;
  23. use RPC::XML;
  24. use RPC::XML::Client;
  25. use XML::LibXML;
  26. use LWP::UserAgent;
  27. use Data::Dumper;
  28. if (@ARGV != 2) {
  29. die "Usage: pingback-client FROM TO\n";
  30. }
  31. my ($from, $to) = @ARGV;
  32. my $ua = LWP::UserAgent->new;
  33. $ua->agent("OddmusePingbackClient/0.1");
  34. print "Getting $to\n";
  35. my $response = $ua->get($to);
  36. if (!$response->is_success) {
  37. die $response->status_line;
  38. }
  39. print "Parsing $to\n";
  40. my $data = $response->decoded_content;
  41. my $parser = XML::LibXML->new(recover => 2);
  42. my $dom = $parser->load_html(string => $data);
  43. my $pingback = $dom->findvalue('//link[@rel="pingback"]/@href');
  44. if (!$pingback) {
  45. die "Pingback URL not found in $to\n";
  46. }
  47. print "Pingback URL is $pingback\n";
  48. my $request = RPC::XML::request->new(
  49. 'pingback.ping', $from, $to);
  50. my $client = RPC::XML::Client->new($pingback);
  51. $response = $client->send_request($request);
  52. if (!ref($response)) {
  53. die $response;
  54. }
  55. print Dumper($response->value);