mirror_pad.pl 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Config::YAML;
  5. use Data::Dumper;
  6. use Encode ();
  7. use FindBin;
  8. use URI::Encode ();
  9. my $config_file = "$FindBin::Bin/mirror_pad.yml";
  10. die "'$config_file' introuvable\n" unless -r $config_file;
  11. my $config = Config::YAML->new(config => $config_file);
  12. my $pad_base = $config->get_apiurl or die "apiurl is missing in config file ?";
  13. my $pad_url = URI::Encode::uri_decode(shift);
  14. warn "PAD_URL=$pad_url";
  15. my $pad_content;
  16. if($pad_url =~ m{\Q$pad_base/group.html}) {
  17. my $api_key = $config->get_apikey or die "apikey is missing in config file ?";
  18. $pad_content = get_private_pad($pad_url, $pad_base, $api_key);
  19. exit 1 if not defined $pad_content;
  20. }
  21. else {
  22. $pad_content = get_public_pad($pad_url);
  23. }
  24. print $pad_content;
  25. sub get_private_pad {
  26. require Etherpad;
  27. my ($pad_url, $api_url, $api_key) = @_;
  28. my $ep = Etherpad->new(
  29. url => $api_url,
  30. apikey => $api_key,
  31. );
  32. my $pad_base = quotemeta $api_url;
  33. warn "PAD_BASE=$pad_base";
  34. # Extraction de l'id du pad à partir de l'url du pad
  35. my $pad_id;
  36. ($pad_id) = $pad_url =~ m{^$pad_base/.+?/(g\.[^/]+)}o;
  37. warn "PADID=$pad_id\n";
  38. if(my($pad_text) = $ep->get_text($pad_id)) {
  39. $pad_text = Encode::encode('UTF-8', $pad_text);
  40. return $pad_text;
  41. }
  42. else {
  43. return;
  44. }
  45. }
  46. sub get_public_pad {
  47. require LWP::UserAgent;
  48. my ($pad_url) = @_;
  49. my $ua = LWP::UserAgent->new;
  50. my $resp = $ua->get($pad_url);
  51. if($resp->is_success) {
  52. return $resp->decoded_content;
  53. }
  54. else {
  55. warn $resp->status_line;
  56. return
  57. }
  58. }