spkdev / diaspora-rss-bot

A little RSS/Atom feed aggregator for Diaspora*
8 stars 2 forks source link

Put Diaspora* interface in own module #3

Open spkdev opened 12 years ago

spkdev commented 12 years ago

Currently, the script is rather hacked together. I think, one design change that I should do in order to get more structure, is to write a module that provides a Diaspora class that can be used to interact with Diaspora. That is login/logout, making posts, etc. I think this especially makes sense, since there is no API available for Diaspora yet (though it is currently worked on). The Diaspora class abstracts away all the ugly GET/POST stuff and can later be refactored to use the API when it becomes available.

TLINDEN commented 12 years ago

Hi,

I took your script and did exactly this. I put together Diaspora::Bot, which is generic and Diaspora::Bot::Feed which does the feed stuff and inherits from Diaspora::Bot. The daemon script diaspora-rss now uses this module.

I also put HTML::WikiConverter::Diaspora into its own module.

You can find them both here:

http://www.daemon.de/idisk/diaspora-bot-0.01.tar.gz http://www.daemon.de/idisk/html-wikiconverter-diaspora-0.01.tar.gz

Beside this I changed a lot of things as well. Here's an unordered list:

Both tarballs are organized as standard perl modules. One can untar it, and install by:

perl Makefile.PL make make install

I urge you to create a CPAN account on https://pause.cpan.org/ and upload the modules to CPAN. You might consider to add unit tests as well, I left this out because a diaspora pod is required to run such tests, which might be impossible for cpan-testers.

best regards, TLINDEN@cpan.org (or diaspora: claitu@nomde.de)

spkdev commented 12 years ago

That's great stuff, I though about using sqlite myself :)

Anyway, I am out of time right now, but I will definitely take a closer look on your changes tomorrow. I hope my last commit did not interfere too much with your changes.

TLINDEN commented 12 years ago

Oh, I forgot to add a commandline option for the pidfile (and a config option as well).

spkdev commented 12 years ago

I am just checking out your changes and I absolutely like it.

One question about the sqlite database though: As I see it, old entries are never deleted. Previously, when I stored the guids directly in the config file, I replaced the old guids with the ones from the new iteration, so the file would not grow infinitely. I think I will handle it the same with the database, or is there a specific reason to not delete them?

TLINDEN commented 12 years ago

Well, I was not sure when it's ok to delete old entries. For example if I restart the daemon (or the server), it could happen that it deletes the old ones, the server/daemon comes up again, and fetches them again so that I have duplicate posts. But I'm not sure how realistic this might be. Perhaps it would be an idea to add timestamps to the entries and delete them after a period of time (one day or configurable).

TLINDEN commented 12 years ago

An addition. The attached patch adds the following:

The patch:

*** ../../../diaspora-bot-0.01/lib/Diaspora/Bot.pm      2012-02-04 14:48:02.000000000 +0000
--- Bot.pm  2012-02-06 12:30:25.000000000 +0000
***************
*** 96,102 ****
    my $post = join '&', map { join '=', ($_, $plist{$_}) } keys %plist;

    $request->content( $post );
!   my $r = $self->ua->request( $request ) or croak "Could not login to " . $self->pod . ": $!" ;
    $self->loggedin(1);
  }

--- 96,105 ----
    my $post = join '&', map { join '=', ($_, $plist{$_}) } keys %plist;

    $request->content( $post );
!   my $res = $self->ua->request( $request ) or croak "Could not login to " . $self->pod . ": $!" ;
!   if(! $res->is_success) {
!     croak "Could not login to " . $self->pod . ": " . $res->status_line ;
!   }
    $self->loggedin(1);
  }

***************
*** 104,110 ****
    my $self = shift;
    my $request = HTTP::Request->new( 'GET', $self->pod.'/users/sign_out' );
    $request->header( 'Connection' => 'keep-alive' );
!   $self->ua->request( $request ) or croak "Could not logout from " . $self->pod . ": $!" ;  
    $self->loggedin(0);
  }

--- 107,116 ----
    my $self = shift;
    my $request = HTTP::Request->new( 'GET', $self->pod.'/users/sign_out' );
    $request->header( 'Connection' => 'keep-alive' );
!   my $res = $self->ua->request( $request ) or croak "Could not logout from " . $self->pod . ": $!" ;  
!   if(! $res->is_success) {
!     croak "Could not logout from " . $self->pod . ": " . $res->status_line ;
!   }
    $self->loggedin(0);
  }

***************
*** 140,145 ****
--- 146,172 ----
    $self->ua->request( $request ) or die "Could not post message to " . $self->pod . "$arg{uri}: $!";
  }

+ sub get {
+   my $self = shift;
+   my %arg  = @_;
+ 
+   $self->_login();
+ 
+   my $request = HTTP::Request->new( 'GET', $self->pod . $arg{uri} );
+   $request->header( 'Content-Type' => 'application/json; charset=UTF-8' );
+   $request->header( 'Connection'   => 'keep-alive' );
+   $request->header( 'X-CSRF-Token' => $self->csrftoken );
+   my $res = $self->ua->request( $request ) or die "Could not post message to " . $self->pod . "$arg{uri}: $!";
+    
+   if(! $res->is_success) {
+     croak "Could not get $arg{uri} from " . $self->pod . ": " . $res->status_line ;
+   }
+ 
+   my $json = JSON->new->allow_nonref;
+ 
+   return $json->decode( $res->content );
+ }
+ 
  sub _escapeString {
    my $self   = shift;
    my $string = shift;
TLINDEN commented 12 years ago

args ... incomplete post..

The idea of the get() method was, to make it possible to "talk" to the bot from diaspora directly. For example a user could send the bot a private message requesting a new rss feed. The bot then could check regularly for such requests, check them and if ok, subscribing to the feed and responding to the user with the created tag for the feed, which the user then can follow. This way the pod operator would not have to edit the config file for every feed someone requests.

TLINDEN commented 12 years ago

ok, I've setup a fork and sent you a pull request. Seems to be easier this way. Why else do we use github? :)

ilv commented 10 years ago

Hi. What happened to Diaspora/Bot.pm?

jaywink commented 10 years ago

Try this maybe? http://paperbod.com/

ilv commented 10 years ago

Thanks jaywink, but I'm interested in the bot implementation rather than the rss feeds. I just found out about diaspy and I think it's what I was looking for.