#!/usr/bin/perl -w ####################################################################### ### $Id: bloglines-0.01.txt,v 1.1 2004/02/04 09:28:25 mate Exp $ ### package Bloglines; ####################################################################### # CONFIGURATION $DATA_DIR = './data'; $PLUGINS_DIR = './plugins'; $LOGIN_EMAIL = ''; $LOGIN_PASS = ''; # $DISPLAY_URL = 'http://www.bloglines.com/myblogs_display'; # $LOGIN_URL = 'http://www.bloglines.com/login'; # $UA_PROXY = 'http://my.proxy.server.domain.name:port/'; # $UA_PROXY_BYPASS = ['localhost','127.0.0.1']; # $UA_TIMEOUT = 60; # $UA_NAME = ""; # $DEBUG = 0; # CONFIGURATION ####################################################################### $|=1; use strict; use URI; use LWP::UserAgent; use HTTP::Request::Common; use HTTP::Cookies; use Getopt::Long; use vars qw($DATA_DIR $PLUGINS_DIR $LOGIN_EMAIL $LOGIN_PASS $DISPLAY_URL $LOGIN_URL $UA $UA_PROXY $UA_PROXY_BYPASS $UA_TIMEOUT $UA_NAME $DEBUG @OPTIONS %OPTCTL); my $VERSION = '0.01'; my ($plugin, $sub, %saw); my @plugins = _load_plugins($PLUGINS_DIR); exit unless GetOptions(\%OPTCTL, "folder=i@", "local", @OPTIONS); $DISPLAY_URL ||= 'http://www.bloglines.com/myblogs_display'; $LOGIN_URL ||= 'http://www.bloglines.com/login'; $UA = LWP::UserAgent->new; $UA->agent(defined($UA_NAME) ? $UA_NAME : ""); $UA->timeout(defined($UA_TIMEOUT) ? $UA_TIMEOUT : 60); $UA->proxy(['http'], $UA_PROXY) if (defined $UA_PROXY); $UA->no_proxy($UA_PROXY_BYPASS) if (defined $UA_PROXY_BYPASS); $UA->env_proxy unless (defined($UA_PROXY) || defined($UA_PROXY_BYPASS)); $UA->cookie_jar(HTTP::Cookies->new(file => "$DATA_DIR/cookies", autosave => 1)); # plugin: display my $display = \&display; foreach $plugin (@plugins) { $plugin->can('display') and defined ($sub = $plugin->display) and $display = $sub and last; } # plugin: login my $login = \&login; foreach $plugin (@plugins) { $plugin->can('login') and defined ($sub = $plugin->login) and $login = $sub and last; } # plugin: parse_file my $parse_file = \&parse_file; foreach $plugin (@plugins) { $plugin->can('parse_file') and defined ($sub = $plugin->parse_file) and $parse_file = $sub and last; } # plugin: folder my (@folder_plugins); foreach $plugin (@plugins) { $plugin->can('folder') and push @folder_plugins, $plugin; } # retrieve the entries foreach my $folder (grep(!$saw{$_}++, @{$OPTCTL{folder}})) { my ($file, $list, $site) = &$display($folder) or next; foreach $plugin (@folder_plugins) { $plugin->folder($file, $list, $site); } } # plugin: teardown foreach $plugin (@plugins) { $plugin->can('teardown') and $plugin->teardown; } exit; # End of Main ####################################################################### sub display { my ($folder) = @_; my ($list, $site); my $file = "$DATA_DIR/display$folder"; if (exists $OPTCTL{local}) { ($list, $site) = &$parse_file($file) or return; return ($file, $list, $site); } my ($res, $title); my $retry = 4; my $uri = "$DISPLAY_URL?folder=$folder"; my $login_succeeded = 0; while ($retry-- > 0) { $res = request($uri, $file) or warn("Can't request: $uri"), return; $title = $res->header('Title') || ""; if ($title =~ /My Blogs/) { $login_succeeded = 1; last; } &$login(); sleep(1); } $login_succeeded or warn("Can't login: $uri"), return; ($list, $site) = &$parse_file($file); return ($file, $list, $site); } sub request { my ($uri, $file) = @_; my $req = GET($uri); my $res = $UA->request($req, $file); warn("ua->request: $uri\n".$res->headers->as_string."\n") if $DEBUG; unless ($res->is_success) { warn($res->message."\n") if $DEBUG; return; } return $res; } sub login { my $uri = $LOGIN_URL; my $email = $LOGIN_EMAIL; my $password = $LOGIN_PASS; my $file = "$DATA_DIR/login"; my $req = POST($uri, [ email => $email, password => $password, r => '/myblogs' ], Referer => $uri ); my $res = $UA->request($req, $file); warn("ua->request: $uri\n".$res->headers->as_string."\n") if $DEBUG; return $res->header('Set-Cookie') ? 1 : 0; } sub parse_file { my ($file) = @_; my ($siteList, $list, $site, %sitehead); open(F, $file) or warn("Can't open $file: $!"), return; while () { s/\r//g if /\r/; next if /^$/; # # # if (/^