Ignore:
Timestamp:
Sep 20, 2011, 11:15:31 PM (13 years ago)
Author:
Edward Z. Yang <ezyang@mit.edu>
Branches:
master, release-1.10, release-1.9
Children:
eb497a9
Parents:
3199d8e
git-author:
Edward Z. Yang <ezyang@mit.edu> (06/20/11 11:41:44)
git-committer:
Edward Z. Yang <ezyang@mit.edu> (09/20/11 23:15:31)
Message:
Implement automatic instance selection, with no de-dup.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/modules/Facebook/lib/BarnOwl/Module/Facebook/Handle.pm

    r3199d8e r99f0a77  
    1515
    1616use Facebook::Graph;
    17 use Data::Dumper;
     17
     18use Lingua::EN::Keywords;
     19
    1820use JSON;
    1921use Date::Parse;
     
    8082        'login_url' => 'http://goo.gl/yA42G',
    8183
    82         'logged_in' => 0
     84        'logged_in' => 0,
     85
     86        # would need another hash for topic de-dup
     87        'topics' => {},
    8388    };
    8489
     
    144149    # Ideally, we should have some worker thread for polling facebook.
    145150    # But BarnOwl is probably not thread-safe >_<
     151
     152    my $old_topics = $self->{topics};
     153    $self->{topics} = {};
    146154
    147155    my $updates = eval {
     
    158166    $self->die_on_error($@);
    159167
    160     #warn Dumper($updates);
    161 
    162168    my $new_last_poll = $self->{last_poll};
    163169    for my $post ( reverse @{$updates->{data}} ) {
     
    173179        my $name    = $post->{to}{data}[0]{name} || $post->{from}{name};
    174180        my $name_id = $post->{to}{data}[0]{id} || $post->{from}{id};
     181        my $postid  = $post->{id};
    175182
    176183        # Only handle post if it's new
    177184        my $created_time = str2time($post->{created_time});
    178185        if ($created_time >= $self->{last_poll}) {
     186            my @keywords = keywords($post->{name} || $post->{message});
     187            my $topic = $keywords[0] || 'personal';
     188            $self->{topics}->{$postid} = $topic;
    179189            # XXX indexing is fragile
    180190            my $msg = BarnOwl::Message->new(
     
    186196                direction => 'in',
    187197                body      => $self->format_body($post),
    188                 postid    => $post->{id},
     198                postid    => $postid,
     199                topic     => $topic,
    189200                time      => asctime(localtime $created_time),
    190201                # XXX The intent is to get the 'Comment' link, which also
     
    194205               );
    195206            BarnOwl::queue_message($msg);
     207        } else {
     208            $self->{topics}->{$postid} = $old_topics->{$postid} || 'personal';
    196209        }
    197210
     
    214227                    direction => 'in',
    215228                    body      => $comment->{message},
    216                     postid    => $post->{id},
     229                    postid    => $postid,
     230                    topic     => $self->get_topic($postid),
    217231                    time      => asctime(localtime $comment_time),
    218232                   );
     
    224238        }
    225239    }
     240    # old_topics gets GC'd
    226241
    227242    $self->{last_poll} = $new_last_poll;
     
    308323}
    309324
     325sub get_topic {
     326    my $self = shift;
     327
     328    my $postid = shift;
     329
     330    return $self->{topics}->{$postid} || 'personal';
     331}
     332
    3103331;
Note: See TracChangeset for help on using the changeset viewer.