Guest User

Untitled

a guest
Jun 1st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. # はてブのwebhookでdeliciousにPOSTして同期する
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use Plack::Request;
  7. use Config::Pit;
  8. use Net::Delicious;
  9.  
  10. my $conf = pit_get('hatena_hook', require => {
  11. key => '',
  12. delicious_user => '',
  13. delicious_password => '',
  14. });
  15.  
  16. my $app = sub {
  17. my $env = shift;
  18.  
  19. my $req = Plack::Request->new($env);
  20.  
  21. return [403, [], []] if $conf->{key} ne $req->param('key');
  22.  
  23. my $d = Net::Delicious->new({
  24. user => $conf->{delicious_user},
  25. pswd => $conf->{delicious_password},
  26. });
  27.  
  28. my $res;
  29. if ($req->param('status') =~ m/^(add|update)$/) {
  30. my $comment = $req->param('comment');
  31. my @tags;
  32. while ($comment =~ s!\[([^\:\[\]]+)\]!!) {
  33. push @tags, $1;
  34. }
  35. $comment =~ s/^\s*(.*?)\s*$/$1/;
  36.  
  37. $res = $d->add_post({
  38. url => $req->param('url'),
  39. description => $req->param('title'),
  40. extended => $comment,
  41. tags => join(' ', @tags),
  42. dt => $req->param('timestamp'),
  43. });
  44. }
  45. elsif ($req->param('status') eq 'delete') {
  46. $res = $d->delete_post({ url => $req->param('url') });
  47. }
  48.  
  49. return [$res ? 200 : 400, [], []];
  50. };
Add Comment
Please, Sign In to add comment