Skip to content

Instantly share code, notes, and snippets.

@shenqi
Created March 1, 2011 22:29
Show Gist options
  • Save shenqi/850025 to your computer and use it in GitHub Desktop.
Save shenqi/850025 to your computer and use it in GitHub Desktop.
Plagger::Plugin::Publish::Tumblr
include:
- /Users/shenqi/.plagger/configs/base-config.yaml
plugins:
- module: Subscription::Config
config:
feed:
- http://api.flickr.com/services/feeds/photos_public.gne?id=32801589@N05&lang=en-us&format=rss_200
- module: Filter::Rule
rule:
module: Deduped
path: /Users/shenqi/.plagger/cache/flickr2tumblr.db
- module: Filter::Reverse
- module: Publish::Tumblr
config:
group: hotoshop.tumblr.com
type: photo
interval: 2
package Plagger::Plugin::Publish::Tumblr;
use strict;
use warnings;
use Config::Pit;
use base qw( Plagger::Plugin );
use Encode;
use Time::HiRes qw(sleep);
use WWW::Tumblr;
sub register {
my($self, $context) = @_;
$context->register_hook(
$self,
'publish.entry' => ¥&publish_entry,
'plugin.init' => ¥&initialize,
);
}
sub initialize {
my($self, $context) = @_;
my $config = pit_get("tumblr.com", require => {
"email" => "your email address used to log in tumblr",
"password" => "your password"
});
$self->{tumblr} = WWW::Tumblr->new;
$self->{tumblr}->email($config->{email});
$self->{tumblr}->password($config->{password});
if($self->conf->{group}){
my $group = $self->conf->{group};
$self->{tumblr}->group($group) ;
}
}
sub publish_entry {
my($self, $context, $args) = @_;
my $title = $args->{entry}->{title};
$title = encode_utf8($title);
my $body = $args->{entry}->{body};
$body = encode_utf8($body);
my $link = $args->{entry}->{link};
my $type = $self->conf->{type} || 'regular';
$context->log(info => "Tumblr($type) posting '$title'");
if($type eq 'text'){
my $post = $body . "<div><a href=¥"" . $link . "¥">" . $title . "</a></div>";
$self->{tumblr}->write(
type => 'regular',
title => $title,
body => $post,
);
}
elsif($type eq 'quote'){
my $source = "<a href=¥"" . $link . "¥">" . $title . "</a>";
$self->{tumblr}->write(
type => 'quote',
quote => $body,
source => $source,
);
}
elsif($type eq 'link'){
$self->{tumblr}->write(
type => 'link',
name => $title,
url => $link,
description => $body,
);
}
elsif($type eq 'photo'){
for my $enclosure ($args->{entry}->enclosures) {
$context->log(debug => "posting " . $enclosure->{url});
if($self->conf->{caption}){
$self->{tumblr}->write(
type => 'photo',
source => $enclosure->{url},
caption => $body,
'click-through-url' => $link,
);
}
else{
my $caption = "<a href=¥"" . $link . "¥">" . $title . "</a>";
$self->{tumblr}->write(
type => 'photo',
source => $enclosure->{url},
caption => $caption,
'click-through-url' => $link,
);
}
}
}
$context->log(debug => $self->{tumblr}->errstr());
my $sleeping_time = $self->conf->{interval} || 5;
$context->log(info => "sleep $sleeping_time.");
sleep( $sleeping_time );
}
1;
__END__
=head1 NAME
Plagger::Plugin::Publish::Tumblr - Post to Tumblr
=head1 SYNOPSIS
- module: Publish::Tumblr
config:
group: hoge.tumblr.com
type: photo
caption: 1
interval: 2
=head1 DESCRIPTION
This plugin automatically posts entries to Tumblr L<http://www.tumblr.com/>.
Based on L<https://github.com/riywo/plagger/blob/master/lib/Plagger/Plugin/Publish/Tumblr.pm> coded by riywo
The email address and password used to login are stored with Config::Pit.
For the initial run, do
perl -MConfig::Pit -e'Config::Pit::set('tumblr.com', data=>{ email => '[email protected]', password => 'barbaz' })'
The plugin posts either regular, quote, link or photo type entry.
Attributes handled vary with the post type as following:
=over 4
=item text
- title
- body with link added at the end
=item quote
- quote (the body of the entry)
- source url (the link of the entry)
=item link
- title/name
- link
- description (the body of the entry)
=item photo
- source url (the enclosure of the entry)
- caption (the body of the entry)
- click-through-url (the link of the entry)
=back
=head1 CONFIG
=over 4
=item group
This option allows to post to secondary tumblelog. Simply puts url of the blog.
=item caption
This option is only valid for 'photo' type posts.
By default, the plugin posts a caption like <a href="$link">$title</a>
however, by enabling this, it posts the body of the entry as the caption.
=back
=head1 AUTHOR
shenqi
riywo
=head1 SEE ALSO
L<Plagger>
=cut
--- Tumblr.pm.bk 2009-06-15 18:42:07.000000000 +0100
+++ Tumblr.pm 2011-03-02 00:24:47.000000000 +0000
@@ -114,6 +114,7 @@
email => $opts{email},
password => $opts{password},
url => $opts{url},
+ group => $opts{group},
ua => $ua,
}, $class;
}
@@ -198,6 +199,12 @@
return $self->{url};
}
+sub group {
+ my($self, $group) = @_;
+ $self->{group} = $group if $group;
+ $self->{group};
+}
+
=head2 read_json
read_json(
@@ -294,6 +301,7 @@
$opts{'email'} = $self->email;
$opts{'password'} = $self->password;
+ $opts{'group'} = $self->group if $self->group;
my $req;
my $res;
@shenqi
Copy link
Author

shenqi commented Mar 2, 2011

photoのポスト対応。
複数tumblelog対応。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment