﻿#/usr/bin/perl
#-----------------------------------------------------------
#Importe les modules
use XML::RSS;
#-----------------------------------------------------------
<<DOC; 
Votre Nom : FEREDJ Siham
2012
 usage : perl Script-rss.pl repertoire-a-parcourir
 Le programme prend en entrée le nom du répertoire contenant les fichiers à traiter
 Le programme construit en sortie un fichier structuré contenant sur chaque
 ligne le nom du fichier et le résultat du filtrage :
<FICHIER><NOM>du fichier</NOM></FICHIER><CONTENU>du filtrage</CONTENU></FICHIER>
DOC
#-----------------------------------------------------------

my $rss=new XML::RSS;
my $rep="$ARGV[0]";
# on s'assure que le nom du répertoire ne se termine pas par un "/"
$rep=~ s/[\/]$//;
# on initialise une variable contenant le flux de sortie 
my $DUMPFULL1="";
my $DUMPFULL2="";
my %PREDIC=();
#----------------------------------------
my $output1="SORTIE-RSS.xml";
my $output2="SORTIE-RSS.txt";
if (!open (XMLOUT,">:encoding(UTF-8)","$output1")) { die "Pb a l'ouverture du fichier $output1"};
if (!open (TXTOUT,">:encoding(UTF-8)","$output2")) { die "Pb a l'ouverture du fichier $output1"};
#----------------------------------------
&parcoursarborescencefichiers($rep);	#recurse!
#----------------------------------------
print XMLOUT "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
print XMLOUT "<PARCOURS>\n";
print XMLOUT "<NOM>FEREDJ Siham</NOM>\n"; # remplacer & par &amp; afin d'affichier correctement dans l'XML
print XMLOUT "<FILTRAGE>\n".$DUMPFULL2."</FILTRAGE>\n";
print XMLOUT "</PARCOURS>\n";
close(XMLOUT);
print TXTOUT $DUMPFULL1;
close(TXTOUT);
exit;
#----------------------------------------------
sub parcoursarborescencefichiers {
    my $path = shift(@_);
    opendir(DIR, $path) or die "can't open $path: $!\n";
    my @files = readdir(DIR);
    closedir(DIR);
    foreach my $file (@files) {
		next if $file =~ /^\.\.?$/;
		$file = $path."/".$file;
		if (-d $file) {
			&parcoursarborescencefichiers($file);	#recurse!
		}
		if ((-f $file)&&($file =~/0,57-0,64-823353,0\.xml$/)) { # on ne filtre que le fil rss de technologie.
			$DUMPFULL2.="<file name=\"$file\">\n"; #on ecrit le nom de fichier dans le fichier xml
#-----------------------------------------------------------
			$rss->parsefile($file); #ouvrir le fichier à traiter
			my $nombredechampdescription=0; # on initialise le compteur.
			#on cherche les contenus dans les <items>, et une boucle pour une sortie en forme.
			foreach my $item (@{$rss->{'items'}}) { 
			#Après avoir trouvé les contenus de items, on cherche les contenus dans les <desciption>
				my $description=$item->{'description'}; 
				$descriptionnet=&nettoyage($description);
				## filtrage les doublons
				if (not exists ($PREDIC{$descriptionnet})){
					$nombredechampdescription++;
					$DUMPFULL2.="<article nombre=\"$nombredechampdescription\">\n";
					$DUMPFULL1.=$descriptionnet."\n";
					$DUMPFULL2.="\$\t".&nettoyage_rss($descriptionnet)."\n"."<\/article>\n";
					$PREDIC{$descriptionnet}++;					
				}				
			}
			print $i++."\n";
			$DUMPFULL2.="<\/file>\n";
		}
    }
}

#----------------------------------------------
sub nettoyage_rss{
	my $symbole=shift(@_);
	$symbole=~ s/&/&amp;/g;
	$symbole=~ s/&/&amp;/g;
	$symbole=~ s/&/&amp;/g;
	return $symbole;
}

sub nettoyage{
	my $description= shift(@_);
	chomp($description);
	$description=~ s/&amp;|&#38;/&/g;
	$description=~ s/&apos;|&#39;/'/g;
	$description=~ s/&nbsp;|&#160;/﻿ /g;
	$description=~ s/&shy;|&#173;/­/g;
	$description=~ s/&quot;|&#34;/"/g;
	$description=~ s/&laquo;|&#171;/«/g;
	$description=~ s/&raquo;|&#187;/»/g;
	$description=~ s/&lsaquo;|&#8249;/‹/g;
	$description=~ s/&rsaquo;|&#8250;/›/g;
	$description=~ s/&ldquo;|&#8220;/“/g;
	$description=~ s/&rdquo;|&#8221;/”/g;
	$description=~ s/&bdquo;|&#8222;/„/g;
	$description=~ s/&lsquo;|&#8216;/‘/g;
	$description=~ s/&rsquo;|&#8217;/’/g;
	$description=~ s/&sbquo;|&#8218;/‚/g;
	$description=~ s/&hellip;|&#8230;/…/g;
	$description=~ s/!|&#33;/!/g;
	$description=~ s/&iexcl;|&#161;/¡/g;
	$description=~ s/\?|&#63;/\?/g;
	$description=~ s/&iquest;|&#191;/¿/g;
	$description=~ s/\(|&#40;/\(/g;
	$description=~ s/\)|&#41;/\)/g;
	$description=~ s/\[|&#91;/\[/g;
	$description=~ s/]|&#93;/]/g;
	$description=~ s/{|&#123;/{/g;
	$description=~ s/}|&#125;/}/g;
	$description=~ s/&uml;|&#168;/¨/g;
	$description=~ s/&acute;|&#180;/´/g;
	$description=~ s/`|&#96;/`/g;
	$description=~ s/\^|&#94;/\^/g;
	$description=~ s/&circ;|&#710;/ˆ/g;
	$description=~ s/~|&#126;/~/g;
	$description=~ s/&cedil;|&#184;/¸/g;
	$description=~ s/#|&#35;/#/g;
	$description=~ s/\*|&#42;/\*/g;
	$description=~ s/,|&#44;/,/g;
	$description=~ s/\.|&#46;/\./g;
	$description=~ s/:|&#58;/:/g;
	$description=~ s/;|&#59;/;/g;
	$description=~ s/&middot;|&#183;/·/g;
	$description=~ s/&bull;|&#8226;/•/g;
	$description=~ s/&macr;|&#175;/¯/g;
	$description=~ s/&oline;|&#8254;/‾/g;
	$description=~ s/-|&#45;/-/g;
	$description=~ s/&ndash;|&#8211;/–/g;
	$description=~ s/&mdash;|&#8212;/—/g;
	$description=~ s/_|&#95;/_/g;
	$description=~ s/\||&#124;/\|/g;
	$description=~ s/&brvbar;|&#166;/¦/g;
	$description=~ s/&zwnj;|&#8204;/‌/g;
	$description=~ s/&zwj;|&#8205;/‍/g;
	$description=~ s/&dagger;|&#8224;/†/g;
	$description=~ s/&Dagger;|&#8225;/‡/g;
	$description=~ s/&sect;|&#167;/§/g;
	$description=~ s/&para;|&#182;/¶/g;
	$description=~ s/&copy;|&#169;/©/g;
	$description=~ s/&reg;|&#174;/®/g;
	$description=~ s/&trade;|&#8482;/™/g;
	$description=~ s/@|&#64;/@/g;
	$description=~ s/\/|&#47;/\//g;
	$description=~ s/\\|&#92;/\\/g;
	$description=~ s/&loz;|&#9674;/◊/g;
	$description=~ s/&spades;|&#9824;/♠/g;
	$description=~ s/&clubs;|&#9827;/♣/g;
	$description=~ s/&hearts;|&#9829;/♥/g;
	$description=~ s/&diams;|&#9830;/♦/g;
	$description=~ s/&larr;|&#8592;/←/g;
	$description=~ s/&uarr;|&#8593;/↑/g;
	$description=~ s/&rarr;|&#8594;/→/g;
	$description=~ s/&darr;|&#8595;/↓/g;
	$description=~ s/&harr;|&#8596;/↔/g;
	$description=~ s/&aacute;|&#225;/á/g;
	$description=~ s/&Aacute;|&#193;/Á/g;
	$description=~ s/&acirc;|&#226;/â/g;
	$description=~ s/&Acirc;|&#194;/Â/g;
	$description=~ s/&agrave;|&#224;/à/g;
	$description=~ s/&Agrave;|&#192;/À/g;
	$description=~ s/&aring;|&#229;/å/g;
	$description=~ s/&Aring;|&#197;/Å/g;
	$description=~ s/&atilde;|&#227;/ã/g;
	$description=~ s/&Atilde;|&#195;/Ã/g;
	$description=~ s/&auml;|&#228;/ä/g;
	$description=~ s/&Auml;|&#196;/Ä/g;
	$description=~ s/&aelig;|&#230;/æ/g;
	$description=~ s/&AElig;|&#198;/Æ/g;
	$description=~ s/&ccedil;|&#231;/ç/g;
	$description=~ s/&Ccedil;|&#199;/Ç/g;
	$description=~ s/&eacute;|&#233;/é/g;
	$description=~ s/&Eacute;|&#201;/É/g;
	$description=~ s/&ecirc;|&#234;/ê/g;
	$description=~ s/&Ecirc;|&#202;/Ê/g;
	$description=~ s/&egrave;|&#232;/è/g;
	$description=~ s/&Egrave;|&#200;/È/g;
	$description=~ s/&euml;|&#235;/ë/g;
	$description=~ s/&Euml;|&#203;/Ë/g;
	$description=~ s/&iacute;|&#237;/í/g;
	$description=~ s/&Iacute;|&#205;/Í/g;
	$description=~ s/&icirc;|&#238;/î/g;
	$description=~ s/&Icirc;|&#206;/Î/g;
	$description=~ s/&igrave;|&#236;/ì/g;
	$description=~ s/&Igrave;|&#204;/Ì/g;
	$description=~ s/&iuml;|&#239;/ï/g;
	$description=~ s/&Iuml;|&#207;/Ï/g;
	$description=~ s/&ntilde;|&#241;/ñ/g;
	$description=~ s/&Ntilde;|&#209;/Ñ/g;
	$description=~ s/&oacute;|&#243;/ó/g;
	$description=~ s/&Oacute;|&#211;/Ó/g;
	$description=~ s/&ocirc;|&#244;/ô/g;
	$description=~ s/&Ocirc;|&#212;/Ô/g;
	$description=~ s/&ograve;|&#242;/ò/g;
	$description=~ s/&Ograve;|&#210;/Ò/g;
	$description=~ s/&oslash;|&#248;/ø/g;
	$description=~ s/&Oslash;|&#216;/Ø/g;
	$description=~ s/&otilde;|&#245;/õ/g;
	$description=~ s/&Otilde;|&#213;/Õ/g;
	$description=~ s/&ouml;|&#246;/ö/g;
	$description=~ s/&Ouml;|&#214;/Ö/g;
	$description=~ s/&oelig;|&#156;/œ/g;
	$description=~ s/&OElig;|&#140;/Œ/g;
	$description=~ s/&scaron;|&#353;/š/g;
	$description=~ s/&Scaron;|&#352;/Š/g;
	$description=~ s/&szlig;|&#223;/ß/g;
	$description=~ s/&eth;|&#240;/ð/g;
	$description=~ s/&ETH;|&#208;/Ð/g;
	$description=~ s/&thorn;|&#254;/þ/g;
	$description=~ s/&THORN;|&#222;/Þ/g;
	$description=~ s/&uacute;|&#250;/ú/g;
	$description=~ s/&Uacute;|&#218;/Ú/g;
	$description=~ s/&ucirc;|&#251;/û/g;
	$description=~ s/&Ucirc;|&#219;/Û/g;
	$description=~ s/&ugrave;|&#249;/ù/g;
	$description=~ s/&Ugrave;|&#217;/Ù/g;
	$description=~ s/&uuml;|&#252;/ü/g;
	$description=~ s/&Uuml;|&#220;/Ü/g;
	$description=~ s/&yacute;|&#253;/ý/g;
	$description=~ s/&Yacute;|&#221;/Ý/g;
	$description=~ s/&yuml;|&#255;/ÿ/g;
	$description=~ s/&Yuml;|&#376;/Ÿ/g;
	$description=~ s/&curren;|&#164;/¤/g;
	$description=~ s/&euro;|&#128;/€/g;
	$description=~ s/\$|&#36;/\$/g;
	$description=~ s/&cent;|&#162;/¢/g;
	$description=~ s/&pound;|&#163;/£/g;
	$description=~ s/&yen;|&#165;/¥/g;
	$description=~ s/&fnof;|&#402;/ƒ/g;
	$description=~ s/&alpha;|&#945;/α/g;
	$description=~ s/&Alpha;|&#913;/Α/g;
	$description=~ s/&beta;|&#946;/β/g;
	$description=~ s/&Beta;|&#914;/Β/g;
	$description=~ s/&gamma;|&#947;/γ/g;
	$description=~ s/&Gamma;|&#915;/Γ/g;
	$description=~ s/&delta;|&#948;/δ/g;
	$description=~ s/&Delta;|&#916;/Δ/g;
	$description=~ s/&epsilon;|&#949;/ε/g;
	$description=~ s/&Epsilon;|&#917;/Ε/g;
	$description=~ s/&zeta;|&#950;/ζ/g;
	$description=~ s/&Zeta;|&#918;/Ζ/g;
	$description=~ s/&eta;|&#951;/η/g;
	$description=~ s/&Eta;|&#919;/Η/g;
	$description=~ s/&theta;|&#952;/θ/g;
	$description=~ s/&Theta;|&#920;/Θ/g;
	$description=~ s/&iota;|&#953;/ι/g;
	$description=~ s/&Iota;|&#921;/Ι/g;
	$description=~ s/&kappa;|&#954;/κ/g;
	$description=~ s/&Kappa;|&#922;/Κ/g;
	$description=~ s/&lambda;|&#955;/λ/g;
	$description=~ s/&Lambda;|&#923;/Λ/g;
	$description=~ s/&mu;|&#956;/μ/g;
	$description=~ s/&Mu;|&#924;/Μ/g;
	$description=~ s/&nu;|&#957;/ν/g;
	$description=~ s/&Nu;|&#925;/Ν/g;
	$description=~ s/&xi;|&#958;/ξ/g;
	$description=~ s/&Xi;|&#926;/Ξ/g;
	$description=~ s/&omicron;|&#959;/ο/g;
	$description=~ s/&Omicron;|&#927;/Ο/g;
	$description=~ s/&pi;|&#960;/π/g;
	$description=~ s/&Pi;|&#928;/Π/g;
	$description=~ s/&rho;|&#961;/ρ/g;
	$description=~ s/&Rho;|&#929;/Ρ/g;
	$description=~ s/&sigma;|&#963;/σ/g;
	$description=~ s/&sigmaf;|&#962;/ς/g;
	$description=~ s/&Sigma;|&#931;/Σ/g;
	$description=~ s/&tau;|&#964;/τ/g;
	$description=~ s/&Tau;|&#932;/Τ/g;
	$description=~ s/&upsilon;|&#965;/υ/g;
	$description=~ s/&Upsilon;|&#933;/Υ/g;
	$description=~ s/&phi;|&#966;/φ/g;
	$description=~ s/&Phi;|&#934;/Φ/g;
	$description=~ s/&chi;|&#967;/χ/g;
	$description=~ s/&Chi;|&#935;/Χ/g;
	$description=~ s/&psi;|&#968;/ψ/g;
	$description=~ s/&Psi;|&#936;/Ψ/g;
	$description=~ s/&omega;|&#969;/ω/g;
	$description=~ s/&Omega;|&#937;/Ω/g;
	$description=~ s/&deg;|&#176;/°/g;
	$description=~ s/&micro;|&#181;/µ/g;
	$description=~ s/&lt;|&#60;/</g;
	$description=~ s/&gt;|&#62;/>/g;
	$description=~ s/&le;|&#8804;/≤/g;
	$description=~ s/&ge;|&#8805;/≥/g;
	$description=~ s/=|&#61;/=/g;
	$description=~ s/&asymp;|&#8776;/≈/g;
	$description=~ s/&ne;|&#8800;/≠/g;
	$description=~ s/&equiv;|&#8801;/≡/g;
	$description=~ s/&plusmn;|&#177;/±/g;
	$description=~ s/&minus;|&#8722;/−/g;
	$description=~ s/\+|&#43;/\+/g;
	$description=~ s/&times;|&#215;/×/g;
	$description=~ s/&divide;|&#247;/÷/g;
	$description=~ s/&frasl;|&#8260;/⁄/g;
	$description=~ s/%|&#37;/%/g;
	$description=~ s/&permil;|&#8240;/‰/g;
	$description=~ s/&frac14;|&#188;/¼/g;
	$description=~ s/&frac12;|&#188;/½/g;
	$description=~ s/&frac34;|&#188;/¾/g;
	$description=~ s/&sup1;|&#185;/¹/g;
	$description=~ s/&sup2;|&#178;/²/g;
	$description=~ s/&sup3;|&#179;/³/g;
	$description=~ s/&ordm;|&#186;/º/g;
	$description=~ s/&ordf;|&#170;/ª/g;
	$description=~ s/&fnof;|&#402;/ƒ/g;
	$description=~ s/&prime;|&#8242;/′/g;
	$description=~ s/&Prime;|&#8243;/″/g;
	$description=~ s/&part;|&#8706;/∂/g;
	$description=~ s/&prod;|&#8719;/∏/g;
	$description=~ s/&sum;|&#8721;/∑/g;
	$description=~ s/&radic;|&#8730;/√/g;
	$description=~ s/&infin;|&#8734;/∞/g;
	$description=~ s/&not;|&#172;/¬/g;
	$description=~ s/&cap;|&#8745;/∩/g;
	$description=~ s/&int;|&#8747;/∫/g;
	$description=~ s/<.+?>//g;
	return $description;
}
