#!/usr/bin/perl -W # # list2atom.pl - A script which generates an Atom channel XML file from the given files and directories. # # Copyright (C) 2012 Bogdan 'bogdro' Drozdowski, http://bogdro.evai.pl/soft/ # (bogdro /AT\ users . sourceforge . net) # # Licence: # GNU General Public Licence v3+ # # Last modified : 2021-10-20 # # Syntax: # ./list2atom.pl -url [-out file.xml] [-author name] # [-title title] [-limit N] [-exc ] # [--help] [--license] file1 [file2...] # # -url : The base URL of the files to put in the resulting file # -out : The resulting file's name (standard output if not present) # -author : The author's name to put in the file # -title : The feed's title to put in the file # -limit : Limit the number of the feed's entries # -exc : Exclude the given files # --help: Display the syntax # --license: Display the license # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # # as published by the Free Software Foundation; either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foudation: # Free Software Foundation # 51 Franklin Street, Fifth Floor # Boston, MA 02110-1301 # USA use strict; use warnings; use Getopt::Long; my $help_msg = "$0: A script which generates an Atom channel XML file from the given files and directories.\n". "Author: Bogdan 'bogdro' Drozdowski, http://bogdro.evai.pl/soft/\n". "Syntax: $0 -url [-out file.xml] [-author name]\n". "\t\t\t[-title title] [-limit N] [-exc ]\n". "\t\t\t[--help] [--license] file1 [file2...]\n\n". "-url \t\t\tThe base URL of the files to put in the resulting file\n". "-out \t\t\tThe resulting file's name (standard output if not present)\n". "-author \t\t\tThe author's name to put in the file\n". "-title \t\t\tThe feed's title to put in the file\n". "-limit \t\t\tLimit the number of the feed's entries\n". "-exc \t\tExclude the given files\n". "--help\t\t\t\tDisplay the syntax.\n". "--license\t\t\tDisplay the license.\n"; my $lic_msg = "$0: A script which generates an Atom channel XML file from the given files and directories.\n". "Author: Bogdan 'bogdro' Drozdowski, http://bogdro.evai.pl/soft/\n\n This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foudation: Free Software Foundation 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA\n"; Getopt::Long::Configure('ignore_case', 'ignore_case_always'); my $help; my $url; my $lic; my $outfile; my $author_name = ''; my $title = ''; my $limit = undef; my $exc; if ( ! GetOptions( 'h|help|?' => \$help, 'url=s' => \$url, 'license|licence|L' => \$lic, 'out=s' => \$outfile, 'author=s' => \$author_name, 'title=s' => \$title, 'exc=s' => \$exc, 'limit=i' => \$limit ) ) { print $help_msg; exit 1; } if ( $lic ) { print $lic_msg; exit 0; } if ( $help ) { print $help_msg; exit 0; } if ( ! $url ) { print $help_msg; exit 0; } if ( ! $outfile ) { $outfile = '-'; } my %excluded_files; if ( $exc ) { foreach (split /,/, $exc) { $excluded_files{$_} = 1; } } ############################# processing my %filenames_dates; foreach (@ARGV) { process_object ($_, \%filenames_dates); } my @filenames_dates_sorted = sort { $filenames_dates{$b} <=> $filenames_dates{$a} } keys %filenames_dates; if ( @filenames_dates_sorted > 0 ) { my $outh; if ( !open ( $outh, "> $outfile" ) ) { print "$0: $outfile: $!\n"; exit 2; } print $outh "\n". "\n\n". "\t$title\n". "\t$url\n". "\t\n". "\t$author_name\n". "\t". get_formatted_date (time) . "\n\n"; my $counter = 0; foreach my $entry (@filenames_dates_sorted) { next if defined $excluded_files{$entry}; print $outh "\t\n". "\t\t$entry\n". "\t\t\n". "\t\t$url/$entry-$filenames_dates{$entry}\n". "\t\t" . get_formatted_date ($filenames_dates{$entry}) . "\n". "\t\t$entry\n". "\t\n\n"; $counter++; last if defined ($limit) && $counter >= $limit; } print $outh "\n"; close $outh; } ############################# subroutines sub process_object { my $object_name = shift; my $filenames_dates_hash = shift; if ( -d $object_name ) { my $hdir; if ( ! opendir ($hdir, $object_name) ) { print "$0: $object_name: $!\n"; return; } while ( my $dirent = readdir ($hdir) ) { next if $dirent eq '.' or $dirent eq '..'; process_object ("$object_name/$dirent", $filenames_dates_hash); } closedir ($hdir); } else { my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat ($object_name); ${$filenames_dates_hash}{$object_name} = $mtime; } } sub get_formatted_date { my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime (shift); $sec = '0' . $sec if $sec < 10; $min = '0' . $min if $min < 10; $hour = '0' . $hour if $hour < 10; $mday = '0' . $mday if $mday < 10; $mon += 1; $mon = '0' . $mon if $mon < 10; return "" . ($year + 1900) . "-$mon-${mday}T$hour:$min:${sec}Z"; }