root/sbm-xpost/trunk/sbm-xpost.cgi

Revision 1666 (checked in by miyagawa, 15 years ago)

import initial revision

  • Property svn:mime-type set to text/script
  • Property svn:keywords set to Id Revision
Line 
1 #!/ur/local/bin/perl
2 use strict;
3 use CGI;
4 use DateTime;
5 use HTTP::Request::Common;
6 use LWP::UserAgent;
7 use Template;
8 use XML::Atom::Entry;
9 use XML::Atom::Client;
10 use YAML;
11
12 (my $config = $ENV{SCRIPT_FILENAME}) =~ s/\.cgi$/.yaml/;
13 our $conf = YAML::LoadFile($config);
14
15 my $q = CGI->new;
16 if ($q->request_method eq 'POST') {
17     do_post($q);
18 } else {
19     do_form($q);
20 }
21
22 sub do_form {
23     my $q = shift;
24     print $q->header('text/html; charset=utf-8');
25     binmode STDOUT, ":utf8";
26     my $tt = Template->new;
27     $tt->process(\<<TEMPLATE, { q => $q });
28 <html>
29 <head>
30 <title>del.icio.us and Hatena cross-poster</title>
31 <style>body { font-family: trebuchet MS, Arial; font-size: 13px }</style>
32 <body>
33 <h1>del.icio.us and Hatena cross-poster</h1>
34 <form action="[% q.url('-query'=>0) %]" method="post">
35 <table>
36 <tr><td style="text-align:right">URL:</td><td><input size="64" type="text" name="url" value="[% q.param('url')|html %]" /></td></tr>
37 <tr><td style="text-align:right">Title:</td><td><input size="64" type="text" name="title" value="[% q.param('title') | html %]" /></td></tr>
38 <tr><td style="text-align:right">Comment:</td><td><input size="64" type="text" name="comment" /></td></tr>
39 <tr><td style="text-align:right">Tags:</td><td><input size="64" type="text" name="tags" /></td></tr>
40 </table>
41 <input type="submit" value=" Save " />
42 </form>
43 <div><a href="javascript:location.href='[% q.url %]?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)">bookmarklet</a></div>
44 </body>
45 TEMPLATE
46     ;
47 }
48
49 sub do_post {
50     my $q = shift;
51     post_delicious($q);
52     post_hatena($q);
53     print $q->redirect($q->param('url'));
54 }
55
56 sub post_delicious {
57     my $q = shift;
58     my $url = URI->new("http://del.icio.us/api/posts/add");
59     $url->query_form(
60         url => $q->param('url'),
61         description => $q->param('title'),
62         extended => $q->param('comment'),
63         tags => $q->param('tags'),
64         dt   => DateTime->now,
65     );
66     my $ua = LWP::UserAgent->new;
67     $ua->credentials("del.icio.us:80", "del.icio.us API", $conf->{delicious}->{username}, $conf->{delicious}->{password});
68     my $res = $ua->get($url);
69     warn $res->status_line unless $res->is_success;
70 }
71
72 sub post_hatena {
73     my $q = shift;
74     my $summary = join '', map "[$_]", split /\s+/, $q->param('tags');
75     $summary .= " " . $q->param('comment') if $q->param('comment');
76
77     my $entry = XML::Atom::Entry->new;
78     my $link  = XML::Atom::Link->new;
79     $link->rel('related');
80     $link->type('text/html');
81     $link->href($q->param('url'));
82     $entry->add_link($link);
83     $entry->summary($summary);
84
85     my $client = XML::Atom::Client->new;
86     $client->username($conf->{hatena}->{username});
87     $client->password($conf->{hatena}->{password});
88     $client->createEntry("http://b.hatena.ne.jp/atom/post", $entry)
89         or warn $client->errstr;
90 }
Note: See TracBrowser for help on using the browser.