erik (elo_sf) wrote in lj_dev,
erik
elo_sf
lj_dev

  • Mood:

lj-rssfetch -- local feed access with handling of authentication

Ok, if you use firefox or other local RSS reader that doesn't handle authenticated feeds you can use this Perl based fetcher to get your/your friend's Atom feeds. The program is driven by a simple configuration file (name it .lj-rssfetch.ini in your home directory on UNIX)... you will need WWW::Mechanize and Config::IniFiles installed with your perl to make this work.


; comments begin with ';'
[lj-rssfetch]
;
; login to livejournal with your username, 'authuser':
authuser=elo_sf
;
; what's your passworrd, 'authpass':
authpass=youwish
;
; set a salt to randomize the file names
salt=abcdefg
;
; list out journals to fetch, e.g. user1 user2 user3 ... userN
fetchlist=elo_sf friend1 another_friend
; 
; Where should the feeds be saved?
;dirroot=/home/erik/public_html/adirectory






#!/usr/bin/perl -w
#
# Copyright (c) 2008 Erik Oliver
#

# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

use strict;

# CPAN Modules
use WWW::Mechanize;
use Config::IniFiles;
use Digest::MD5 qw(md5_hex); # use md5_hex(string)

#global variables

use vars qw($confile);

# userconfig
$confile = $ENV{'HOME'} . '/.lj-rssfetch.ini';

  # format for file is
  # [lj-rssfetch] 			# mandatory links prefs with lj-rssfetch
  # authuser=<<your username>>		# optional

  # authpass=<<your ljpass>>		# optional
  # salt=<<string of your choice>>	# optional, but recommended, e.g. salt=abc
  # fetchlist=<<space sep journals>>	# must have at least one or receive on command line

  # dirroot=<<feedloc>>			# optional, default to $HOME/public_html/hiddenfeeds/
  # You can put # or ; to indicate comments

# trim a string of leading/trailing spaces
sub trim($) {
	$_ = shift;
	s/^\s+//;
	s/\s+$//;
	return $_
}
  

sub Main(@) {
  # process the .INI file

  if (! (-r $confile) ) {
	die("lj-rssfetch: configuration file ($confile) missing or unreadable");
  }
  my $cfg = Config::IniFiles->new( -file => $confile);

  my $authuser   = trim($cfg->val('lj-rssfetch','authuser', ''));
  my $authpass   = trim($cfg->val('lj-rssfetch','authpass', ''));

  my $salt       = trim($cfg->val('lj-rssfetch','salt',     ''));
  my $fetchlist  = trim($cfg->val('lj-rssfetch','fetchlist',''));

  my $dirroot    = trim($cfg->val('lj-rssfetch','dirroot', 
	  			$ENV{'HOME'} . '/public_html/hiddenfeeds/'));
  $cfg->Delete(); # free the memory

  # add trailing /
  if ($dirroot !~ m/\/$/) { $dirroot .= '/' }

  # ok build the array of users to fetch and set up the loop
  my @users;

  # user didn't put in their config
  if ($fetchlist eq '') {
	  # if we did not get command line parameters, die
	  if ($#_ < 0) {
		  die "usage: lj-rssfetch [username1 username2 ... usernameN]\n";
	  } else {

		  # ok load our array from the command line
		  @users = @_;
	  }
  } else {
	  if ($#_ >= 0) {
		warn "lj-rssfetch command line arguments ignored in favor of config file\n";
          }
	@users = split(/\s/,$fetchlist)
  }
  

  # do the actual fetching work here
  my $ua = WWW::Mechanize->new( autocheck => 1);
  my $authorize = '';
  my $user = '';;

  my $outfile = '';

  # are we authenticating?
  if ($authuser ne '' && $authpass eq '') {

	  # trying to, but no password in INI file
	  die "lj-rssfetch configuration specifies an authuser, but not an authpass.\n";
  } elsif ($authuser ne '' ) {
	  # yes, set things up

	  $ua->agent("http://livejournal.com/users/$authuser ($authuser\@livejournal.com");
	  $ua->credentials($authuser => $authpass);
	  $authorize = "?auth=digest"

  } else {
	  $ua->agent("somebody running lj-rssfetch script without authenticating");
  }

  # do the fetching
  foreach  $user (@users) {
	# outfile is where the file goes

  	$outfile = $dirroot . $user . '-' . md5_hex($salt . $user) . '.xml';
	$ua->get("http://$user.livejournal.com/data/atom$authorize");
	$ua->save_content($outfile);
  }
}

Main(@ARGV);

exit 0;
Tags: client: authentication, client: export, code: perl, syndication
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 4 comments