#!/usr/bin/env perl
use warnings;
use strict;
use feature ':5.10';
use File::Find;
use File::Basename;
use IO::Uncompress::AnyUncompress;
# Note: if this script isn't being run from the same directory as the
# python one was, chdir to it! Or convert everything to absolute paths
+.
my $home = $ARGV[0] // q{./FFCache/}; # 5.10 idiom
my $cachedata = q{../../Documents/cachedata/};
# The python script has some commented out stuff here, so I won't rewr
+ite it
find( { wanted => \&process_file, no_chdir => 1 }, $home );
sub process_file {
my $name = basename( $File::Find::name ); # Needed because we are
+running no_chdir
return if $name =~ /_CACHE_/;
my $content;
my $data;
my $gzip;
if ( $gzip = new IO::Uncompress::AnyUncompress( $File::Find::name
+ ) ) {
my $status = $gzip->read( $data );
return unless $status > 0;
}
else {
open my $file, '<', $File::Find::name or return;
$data = do { local $/; <$file> };
close $file;
}
# This seems like an odd way of doing the following, but it's
# how the python script does it, so who am I to argue?
if ( substr($data, 0, 10) =~ /PNG/ ) {
$content = 'png';
}
elsif ( substr($data, 0, 10) =~ /GIF89a/ ) {
$content = 'gif';
}
elsif ( substr($data, 0, 10) =~ /JFIF/ ) {
$content = 'jpeg';
}
elsif ( substr($data, 0, 20) =~ /HTML/i ) {
$content = 'html';
}
else {
return;
}
my $fname = $cachedata . $name . '.' . $content;
open my $f_out, '>', $fname or return;
print $f_out $data;
chmod 0777, $f_out;
close $f_out;
}
|