Skip to content

Instantly share code, notes, and snippets.

@Browne
Created October 25, 2011 14:57
Show Gist options
  • Select an option

  • Save Browne/1313027 to your computer and use it in GitHub Desktop.

Select an option

Save Browne/1313027 to your computer and use it in GitHub Desktop.
Open source perl auto-updater for <lastmod> in XML sitemaps.
#!/usr/bin/perl
#######################################################################
#Basic perl script to update <lastmod> in an XML sitemap. #
#Works by seaching for .html files nested in <loc> and then comparing #
#to the file. It then inserts the date in the <lastmod> tag. #
# #
#This code is not subject to copyright and is free to use #
# #
#Eliot Brown 25/10/2011 #
#Version 1.05 #
#######################################################################
use warnings;
use strict;
use POSIX;
chdir('/home/eliot/Ubuntu One/YEAR 1 University/Programming/XHTML') or die "$!";
my $lastmod;
my @html_files;
foreach (<*\.html>) {
push @html_files, $_.strftime("%Y-%m-%dT%H:%M:%S+00:00", localtime((stat($_))[9]));
}
my $old = "sitemap.xml";
my $old_orig = "sitemap_orig.xml";
my $new = "sitemap_new.xml";
open (OLD, "< $old") or die "Can't open $old: $!";
open (NEW, "> $new") or die "Can't open $new: $!";
select(NEW); # new default filehandle
while (<OLD>) {
# process line, change $_ if required and then print to NEW filehandle
if (/<loc>(.*?)<\/loc>/ and grep {/^$1/} @html_files) {
$lastmod = (grep {/^$1/} @html_files)[0];
$lastmod =~ s/^$1//;
} elsif (/<lastmod>(.*?)<\/lastmod>/ and $lastmod ne "") {
$_ = "<lastmod>$lastmod<\/lastmod>\n";
$lastmod = "";
}
print NEW $_;
}
close (OLD) or die "Can't close $old: $!";
close (NEW) or die "Can't close $new: $!";
rename($old, $old_orig) or die "Can't rename $old to $old_orig: $!";
rename($new, $old) or die "Can't rename $new to $old: $!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment