Changeset 11427 for lang/perl/plagger

Show
Ignore:
Timestamp:
05/12/08 12:40:41 (6 months ago)
Author:
charsbar
Message:

Plagger::Plugin::CustomFeed::MxiiScraper?: I didn't noticed this was imported here. reluctantly sync-ed with the latest version in the plagger's main repo.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/plagger/lib/Plagger/Plugin/CustomFeed/MixiScraper.pm

    r330 r11427  
    4141        get_detail => 'view_event', 
    4242    }, 
     43    BBS => { 
     44        title      => 'コミュニティ最新書き込み', 
     45        get_list   => 'new_bbs', 
     46        get_detail => 'view_bbs', 
     47    }, 
    4348}; 
    4449 
     
    8186    for my $type (@{$self->conf->{feed_type} || ['FriendDiary']}) { 
    8287        $context->error("$type not found") unless $MAP->{$type}; 
    83         $self->aggregate_feed($context, $type, $args); 
    84     } 
    85 } 
     88        if ($type eq 'BBS' and $self->conf->{split_bbs_feed}) { 
     89            $self->aggregate_bbs_feed($context, $type, $args); 
     90        } 
     91        else { 
     92            $self->aggregate_feed($context, $type, $args); 
     93        } 
     94    } 
     95} 
     96 
    8697sub aggregate_feed { 
    8798    my($self, $context, $type, $args) = @_; 
     
    90101    $feed->type('mixi'); 
    91102    $feed->title($MAP->{$type}->{title}); 
    92  
    93     my $format = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %H:%M'); 
    94103 
    95104    my $meth = $MAP->{$type}->{get_list}; 
     
    101110 
    102111    my $i = 0; 
    103     my $blocked = 0; 
     112    $self->{blocked} = 0; 
    104113    for my $msg (@msgs) { 
    105114        next if $type eq 'FriendDiary' and $msg->{link}->query_param('url'); # external blog 
    106115        last if $i++ >= $items; 
    107116 
    108         my $entry = Plagger::Entry->new; 
    109         $entry->title($msg->{subject}); 
    110         $entry->link($msg->{link}); 
    111         $entry->author($msg->{name}); 
    112         $entry->date( Plagger::Date->parse($format, $msg->{time}) ); 
    113  
    114         if ($self->conf->{show_icon} && !$blocked && defined $MAP->{$type}->{icon}) { 
    115             my $owner_id = $msg->{link}->query_param($MAP->{$type}->{icon}); 
    116             $context->log(info => "Fetch icon of id=$owner_id"); 
    117  
    118             my $item = $self->cache->get_callback( 
    119                 "outline-$owner_id", 
    120                 sub { 
    121                     Time::HiRes::sleep( $self->conf->{fetch_body_interval} || 1.5 ); 
    122                     my $item = $self->{mixi}->show_friend->parse(id => $owner_id)->{outline}; 
    123                     $item; 
    124                 }, 
    125                 '12 hours', 
    126             ); 
    127             if ($item && $item->{image} !~ /no_photo/) { 
    128                 # prefer smaller image 
    129                 my $image = $item->{image}; 
    130                    $image =~ s/\.jpg$/s.jpg/; 
    131                 $entry->icon({ 
    132                     title => $item->{name}, 
    133                     url   => $image, 
    134                     link  => $item->{link}, 
    135                 }); 
     117        $self->add_entry( $context, $type, $feed, $msg ); 
     118    } 
     119 
     120    $context->update->add($feed); 
     121} 
     122 
     123sub aggregate_bbs_feed { 
     124    my($self, $context, $type, $args) = @_; 
     125 
     126    my $meth = $MAP->{$type}->{get_list}; 
     127    my @msgs = $self->{mixi}->$meth->parse; 
     128    my $items = $self->conf->{fetch_items} || 20; 
     129    $self->log(info => 'fetch ' . scalar(@msgs) . ' entries'); 
     130 
     131    my $i = 0; 
     132    $self->{blocked} = 0; 
     133    for my $msg (@msgs) { 
     134        next if $type eq 'FriendDiary' and $msg->{link}->query_param('url'); # external blog 
     135        last if $i++ >= $items; 
     136 
     137        my $feed = Plagger::Feed->new; 
     138        $feed->type('mixi'); 
     139        (my $subject = $msg->{subject}) =~ s/\(\d+\)$//; 
     140        (my $link = $msg->{link}) =~ s/&comment_count=\d*//; 
     141        $feed->title($subject); 
     142        $feed->description($MAP->{$type}->{title}.': '.$msg->{name}); 
     143        $feed->link($link); 
     144 
     145        $self->add_entry( $context, $type, $feed, $msg ); 
     146 
     147        $context->update->add($feed); 
     148    } 
     149} 
     150 
     151my $format = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %H:%M'); 
     152 
     153sub add_entry { 
     154    my ($self, $context, $type, $feed, $msg) = @_; 
     155 
     156    my $entry = Plagger::Entry->new; 
     157    $entry->title($msg->{subject}); 
     158    $entry->link($msg->{link}); 
     159    $entry->author($msg->{name}); 
     160    $entry->date( Plagger::Date->parse($format, $msg->{time}) ); 
     161    $entry->date->set_time_zone('Asia/Tokyo') if $entry->date; 
     162 
     163    if ($self->conf->{show_icon} && !$self->{blocked} && defined $MAP->{$type}->{icon}) { 
     164        my $owner_id = $msg->{link}->query_param($MAP->{$type}->{icon}); 
     165        $context->log(info => "Fetch icon of id=$owner_id"); 
     166 
     167        my $item = $self->cache->get_callback( 
     168            "outline-$owner_id", 
     169            sub { 
     170                Time::HiRes::sleep( $self->conf->{fetch_body_interval} || 1.5 ); 
     171                my $item = $self->{mixi}->show_friend->parse(id => $owner_id)->{outline}; 
     172                $item; 
     173            }, 
     174            '12 hours', 
     175        ); 
     176        if ($item && $item->{image} !~ /no_photo/) { 
     177            # prefer smaller image 
     178            my $image = $item->{image}; 
     179               $image =~ s/\.jpg$/s.jpg/; 
     180            $entry->icon({ 
     181                title => $item->{name}, 
     182                url   => $image, 
     183                link  => $item->{link}, 
     184            }); 
     185        } 
     186    } 
     187 
     188    my @comments; 
     189    if ($self->conf->{fetch_body} && !$self->{blocked} && $msg->{link} =~ /view_/ && defined $MAP->{$type}->{get_detail}) { 
     190        # view_enquete is not implemented and probably 
     191        # won't be implemented as it seems redirected to 
     192        # reply_enquete 
     193        return if $msg->{link} =~ /view_enquete/; 
     194        $context->log(info => "Fetch body from $msg->{link}"); 
     195        my $item = $self->cache->get_callback( 
     196            "item-".$msg->{link}, 
     197            sub { 
     198                Time::HiRes::sleep( $self->conf->{fetch_body_interval} || 1.5 ); 
     199                my $item = $self->{mixi}->parse($msg->{link}); 
     200                $item; 
     201            }, 
     202            '12 hours', 
     203        ); 
     204        if ($item) { 
     205            my $body = $item->{description}; 
     206               $body =~ s!(\r\n?|\n)!<br />!g; 
     207            for my $image (@{ $item->{images} || [] }) { 
     208                $body .= qq(<div><a href="$image->{link}"><img src="$image->{thumb_link}" style="border:0" /></a></div>); 
     209                my $enclosure = Plagger::Enclosure->new; 
     210                $enclosure->url($image->{thumb_link}); 
     211                $enclosure->auto_set_type; 
     212                $enclosure->is_inline(1); 
     213                $entry->add_enclosure($enclosure); 
    136214            } 
     215            $entry->body($body); 
     216 
     217            $entry->date( Plagger::Date->parse($format, $item->{time}) ); 
     218            $entry->date->set_time_zone('Asia/Tokyo') if $entry->date; 
     219            if ($self->conf->{fetch_comment}) { 
     220              for my $comment (@{ $item->{comments} || [] }) { 
     221                  my $c = Plagger::Entry->new; 
     222                     $c->title($entry->title . ': '. $comment->{subject}); 
     223                     $c->body($comment->{description}); 
     224                     $c->link($comment->{link}); 
     225                     $c->author($comment->{name}); 
     226                     $c->date( Plagger::Date->parse($format, $comment->{time}) ); 
     227                     $c->date->set_time_zone('Asia/Tokyo') if $c->date; 
     228                  push @comments, $c; 
     229              } 
     230            } 
     231        } else { 
     232            $context->log(warn => "Fetch body failed. You might be blocked?"); 
     233            $self->{blocked}++; 
    137234        } 
    138  
    139         if ($self->conf->{fetch_body} && !$blocked && $msg->{link} =~ /view_/ && defined $MAP->{$type}->{get_detail}) { 
    140             $context->log(info => "Fetch body from $msg->{link}"); 
    141             my $item = $self->cache->get_callback( 
    142                 "item-$msg->{link}", 
    143                 sub { 
    144                     Time::HiRes::sleep( $self->conf->{fetch_body_interval} || 1.5 ); 
    145                     my $item = $self->{mixi}->parse($msg->{link}); 
    146                     $item; 
    147                 }, 
    148                 '12 hours', 
    149             ); 
    150             if ($item) { 
    151                 my $body = $item->{description}; 
    152                    $body =~ s!(\r\n?|\n)!<br />!g; 
    153                 for my $image (@{ $item->{images} || [] }) { 
    154                     $body .= qq(<div><a href="$image->{link}"><img src="$image->{thumb_link}" style="border:0" /></a></div>); 
    155                     my $enclosure = Plagger::Enclosure->new; 
    156                     $enclosure->url($image->{thumb_link}); 
    157                     $enclosure->auto_set_type; 
    158                     $enclosure->is_inline(1); 
    159                     $entry->add_enclosure($enclosure); 
    160                 } 
    161                 $entry->body($body); 
    162  
    163                 $entry->date( Plagger::Date->parse($format, $item->{time}) ); 
    164             } else { 
    165                 $context->log(warn => "Fetch body failed. You might be blocked?"); 
    166                 $blocked++; 
    167             } 
    168         } 
    169  
    170         $feed->add_entry($entry); 
    171     } 
    172  
    173     $context->update->add($feed); 
     235    } 
     236 
     237    $feed->add_entry($entry); 
     238    for my $comment ( @comments ) { 
     239        $feed->add_entry($comment); 
     240    } 
    174241} 
    175242 
     
    189256        password: password 
    190257        fetch_body: 1 
     258        fetch_comment: 0 
    191259        show_icon: 1 
    192260        feed_type: 
     
    223291link to the entry. Defaults to 0. 
    224292 
     293=item fetch_comment 
     294 
     295With this option set, this plugin fetches entry's comments as well 
     296(meaningless when C<fetch_body> is not set). Defaults to 0. 
     297 
    225298=item fetch_body_interval 
    226299 
     
    233306mixi.jp site, which makes the output HTML very user-friendly. 
    234307 
     308=item split_bbs_feed 
     309 
     310With this option set, BBS feed will be split up. Defaults to 0. 
     311 
    235312=item feed_type 
    236313