no-flickr.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #! /usr/bin/perl -w
  2. # Copyright (C) 2005-2016 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. use Modern::Perl;
  17. use LWP::UserAgent;
  18. use utf8;
  19. binmode(STDOUT, ":utf8");
  20. my $ua = LWP::UserAgent->new;
  21. sub url_encode {
  22. my $str = shift;
  23. return '' unless $str;
  24. utf8::encode($str); # turn to byte string
  25. my @letters = split(//, $str);
  26. my %safe = map {$_ => 1} ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '-', '_', '.', '!', '~', '*', "'", '(', ')', '#');
  27. foreach my $letter (@letters) {
  28. $letter = sprintf("%%%02x", ord($letter)) unless $safe{$letter};
  29. }
  30. return join('', @letters);
  31. }
  32. sub get_raw {
  33. my $uri = shift;
  34. my $response = $ua->get($uri);
  35. return $response->content if $response->is_success;
  36. }
  37. sub get_wiki_page {
  38. my ($wiki, $id, $password) = @_;
  39. my $parameters = [
  40. pwd => $password,
  41. action => 'browse',
  42. id => $id,
  43. raw => 1,
  44. ];
  45. my $response = $ua->post($wiki, $parameters);
  46. return $response->decoded_content if $response->is_success;
  47. die "Getting $id returned " . $response->status_line;
  48. }
  49. sub get_wiki_index {
  50. my $wiki = shift;
  51. my $parameters = [
  52. search => "flickr.com",
  53. context => 0,
  54. raw => 1,
  55. ];
  56. my $response = $ua->post($wiki, $parameters);
  57. return $response->decoded_content if $response->is_success;
  58. die "Getting the index returned " . $response->status_line;
  59. }
  60. sub post_wiki_page {
  61. my ($wiki, $id, $username, $password, $text) = @_;
  62. my $parameters = [
  63. username => $username,
  64. pwd => $password,
  65. recent_edit => 'on',
  66. text => $text,
  67. title => $id,
  68. ];
  69. my $response = $ua->post($wiki, $parameters);
  70. die "Posting to $id returned " . $response->status_line unless $response->code == 302;
  71. }
  72. my %seen = ();
  73. sub write_flickr {
  74. my ($id, $flickr, $dir, $file) = @_;
  75. say "Found $flickr";
  76. warn "$file was seen before: " . $seen{$file} if $seen{$file};
  77. die "$file contains unknown characters" if $file =~ /[^a-z0-9_.]/;
  78. $seen{$file} = "$id used $flickr";
  79. my $bytes = get_raw($flickr) or die("No data for $id");
  80. open(my $fh, '>', "$dir/$file") or die "Cannot write $dir/$file";
  81. binmode($fh);
  82. print $fh $bytes;
  83. close($fh);
  84. }
  85. sub convert_page {
  86. my ($wiki, $pics, $dir, $username, $password, $id) = @_;
  87. say $id;
  88. my $text = get_wiki_page($wiki, $id, $password);
  89. my $is_changed = 0;
  90. while ($text =~ m!(https://[a-z0-9.]+.flickr.com/(?:[a-z0-9.]+/)?([a-z0-9_]+\.(?:jpg|png)))!) {
  91. my $flickr = $1;
  92. my $file = $2;
  93. write_flickr($id, $flickr, $dir, $file);
  94. $is_changed = 1;
  95. my $re = quotemeta($flickr);
  96. $text =~ s!$flickr!$pics/$file!g;
  97. }
  98. if ($is_changed) {
  99. post_wiki_page($wiki, $id, $username, $password, $text);
  100. } else {
  101. # die "$id has no flickr matches?\n$text";
  102. }
  103. sleep(5);
  104. }
  105. sub convert_site {
  106. my ($wiki, $pics, $dir, $username, $password) = @_;
  107. my @ids = split(/\n/, get_wiki_index($wiki));
  108. for my $id (@ids) {
  109. convert_page($wiki, $pics, $dir, $username, $password, $id);
  110. }
  111. }
  112. our $AdminPass;
  113. do "/home/alex/password.pl";
  114. convert_site('https://alexschroeder.ch/wiki',
  115. 'https://alexschroeder.ch/pics',
  116. '/home/alex/alexschroeder.ch/pics',
  117. 'Alex Schroeder',
  118. $AdminPass);