#!/usr/bin/perl
#
# Require Perl5
#
# rss2twitter -- RSS to Twitter
# rss2twitter is freeware
# If you like it at http://www.pivari.com/rss2twitter.html
# you can make a donation
#
# by Pivari.com <info@pivari.com> 25th April 2007
#
# This is version 0.2
#
# blogger to twitter
use strict;
use Getopt::Long;
use HTTP::Lite;
use Net::Twitter;
use XML::RSS;
use utf8;
# use Encode;
# GUI
# conversione caratteri accentati

my $version="0.2";
my $producer="rss2twitter";
my $rss2twitterHome="http://www.pivari.com/$producer.html";
my $configure=$producer.".cfg";
my $help=0; my $verbose=0; my $Version;
my $debug=0; my $match=0;
my $companyname="Pivari.com";
my $PIVARImail="mailto:info\@pivari.com";
my @lines;

my $elem; my @option; my @elem; my $temp; my $newtemp;
my $phrase; my $result;

&GetOptions("configure=s"  => \$configure,
            "help"         => \$help,
            "version"      => \$Version,
            "verbose"      => \$verbose,
            "debug"        => \$debug) || printusage() ;
my @elem=("user","password","feed");
my %option=(user                  => '',
            password              => '',
            feed                  => '');

$debug and $verbose=1;
if($Version) {print "$producer $version\n";exit;}
$help and printusage();

if (-e $configure) {
  $verbose and print "Processing $configure configuration file\n";
  open (CNF, "$configure");
  while (<CNF>) {
    s/\t/ /g;        #replace tabs by space
    s/ *$//g;        #delete blanks at the end of the line
    next if /^ *\#/; #ignore comment lines
    next if /^ *$/;  #ignore empty lines
    foreach $elem (@elem) {
      if (/ *$elem *: *(.*)/i) {
        $option{$elem}=$1;
	$verbose and print "$elem is set\t\t$option{$elem}\n";
        }
      }
    }
  close(CNF);
  } else {
  &Warning("to set your $producer configuration file you can use:\n$producer.cfg or -configure your_$producer.cfg\nElse the program will use the default parameters\n")
  }

my $http = new HTTP::Lite;
my $req = $http->request("$option{'feed'}") 
             or die "Unable to get document: $!";
$debug and print "Downloading $option{'feed'}\n"; 
$debug and print $http->body();
my $rss = new XML::RSS;
$rss->parse($http->body());

my $file= $option{'user'}.".r2t";
my $twit = Net::Twitter->new(username=>$option{'user'}, password=>$option{'password'});
   foreach my $item (@{$rss->{'items'}}) {
     next unless defined($item->{'title'}) && defined($item->{'link'});
     if (-e $file) {
       $match=0;
       open (FILE, "$file") ||
         die "$producer: couldn't read file $file\n";
       while (<FILE>) {
         $temp=$item->{'title'};
#         $temp=&SpecChars($item->{'title'});
#	 if (/$item->{'link'}: $temp/) {$match=1}
	 if (/$item->{'link'}/) {$match=1}
         }
       close(FILE);
       if($match eq 0) {
         $phrase="$item->{'link'} $item->{'title'} ";
         push @lines,$phrase;
         }
     } else {
       $phrase="$item->{'link'} $item->{'title'}";
       push @lines,$phrase;
     } 
   }
   for (my $i=0;$i<$#lines+1;$i++){
     $verbose and print "$lines[$#lines-$i]\n";
#       $temp=&UTFiso($temp);
#       $newtemp = decode("utf8", $temp), 
#       $temp = encode("cp1250", $newtemp);
#       $temp=&SpecChars($temp);
     $result = $twit->update($lines[$#lines-$i]);
     }
   open (FILE, ">$file") ||
     die "$producer: couldn't create file $file\n";
   foreach my $item (@{$rss->{'items'}}) {
     next unless defined($item->{'title'}) && defined($item->{'link'});
     $temp=$item->{'title'};
     print FILE "$item->{'link'}: $temp\n";
     }
   close(FILE);



sub SpecChars {
  my $string = shift(@_);
  $string=~s/&/&amp;/g;
  $string=~s/'/&#39;/g;
  $string=~s/"/&#34;/g;
  $string=~s//&#241;/g;
  $string=~s//&#224;/g;
  $string=~s//&#232;/g;
  $string=~s//&#236;/g;
  $string=~s//&#242;/g;
  $string=~s//&#249;/g;
  $string=~s//&#225;/g;
  $string=~s//&#233;/g;
  $string=~s//&#237;/g;
  $string=~s//&#243;/g;
  $string=~s//&#250;/g;
  $string=~s//&#226;/g;
  $string=~s//&#234;/g;
  $string=~s//&#238;/g;
  $string=~s//&#244;/g;
  $string=~s//&#251;/g;
  $string=~s//&#228;/g;
  $string=~s//&#235;/g;
  $string=~s//&#239;/g;
  $string=~s//&#246;/g;
  $string=~s//&#252;/g;
  $string=~s//&#227;/g;
  $string=~s//&#245;/g;
  $string=~s//&#8220;/g;
  $string=~s//&#8221;/g;
  $string=~s//&#8212;/g;
  $string=~s//&#8217;/g;
  $string=~s//&#174;/g;
  return $string;
  }

# UTF-8 to iso-8859-1
sub UTFiso {
  my $string = shift(@_);
  $string=~s/à//g;
  $string=~s/è//g;
  $string=~s/é//g;
  $string=~s/ò//g;
  $string=~s/ù//g;
  $string=~s/ì//g;
  $string=~s/’//g;
  $string=~s/‘//g;
  $string=~s/°//g;
  $string=~s/®//g;
  $string=~s/™//g;
  return $string;
  }

sub printusage {
    print <<USAGEDESC;

usage:
        $producer [-options ...] 

where options include:
    -help               print out this message
    -configure file     default $producer.cfg
    -version            the program version
    -verbose            verbose

Home: $rss2twitterHome

USAGEDESC
    exit(1);
}

exit 0;

# __END__

=head1 NAME

RSS2Twitter - Version 1.0 25th April 2007

=head1 SYNOPSIS

Syntax : rss2twitter [-options]

=head1 DESCRIPTION

rss2twitter
To introduce the title and the link of the posts in a RSS into Twitter

=head1 Features

RSS2TWITTER

=head1 Options

where options include:

    -help               print out this message
    -configure file     default txt2pdf.cfg
    -version            the program version
    -verbose            verbose

=cut
