-
-
Save zerodogg/18a712c71c7177083071335486a06b37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
=item Copyright | |
Copyright 2016 Joseph R. Nosie | |
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, see <http://www.gnu.org/licenses/>. | |
=cut | |
use strict; | |
use warnings; | |
use File::MMagic; | |
use LWP::Simple qw/get head/; | |
my %files; | |
sub getMetaInfo | |
{ | |
my $url = shift; | |
my $mimetype = shift; | |
my $allowFallback = shift; | |
my $outDir = 'Images'; | |
my $ext = 'png'; | |
if ($mimetype =~ /^image\/png$/) { | |
$ext = 'png'; | |
} elsif ($mimetype =~ /^image\/jpeg$/) { | |
$ext = 'jpg'; | |
} elsif ($mimetype =~ /^text\/plain$/) { | |
$outDir = 'Models'; | |
$ext = 'obj'; | |
} elsif($allowFallback) { | |
if ($url =~ /\.jpe?g/) { | |
$ext = 'jpg'; | |
} elsif ($url =~ /\.png/) { | |
$ext = 'png'; | |
} else { | |
die("Unkown type for: $url\n"); | |
} | |
} | |
else | |
{ | |
return; | |
} | |
return($ext,$outDir); | |
} | |
my $total = 0; | |
while(my $l = <>) { | |
next if $l !~ /URL/; | |
$l =~ s/".*URL":\s*"(.*)",$/$1/; | |
$l =~ s/^\s+//; | |
$l =~ s/\s+$//; | |
if(length($l) == 0 || $l !~ /^(https?|ftp)/) | |
{ | |
next; | |
} | |
if (!$files{$l}) | |
{ | |
my $fname = $l; | |
$fname =~ s#[.:=?%&/-]##g; | |
$files{$l} = $fname; | |
$total++; | |
} | |
} | |
my $ft = new File::MMagic; | |
mkdir 'Images'; | |
mkdir 'Models'; | |
$| = 1; | |
my $no = 0; | |
foreach my $url (sort keys %files) { | |
my $filteredName = $files{$url}; | |
$no++; | |
printf '[%3d/%3d] %s', $no,$total,'Fetching "' . $url . '"...'; | |
my $mimetype; | |
my($ext,$outDir); | |
my ($content,undef) = head($url); | |
$content =~ s/;.*//; | |
($ext,$outDir) = getMetaInfo($url,$content,0); | |
if ($ext && $outDir) | |
{ | |
my $out = $outDir.'/'.$filteredName.'.'.$ext; | |
if (-e $out) | |
{ | |
print '(existed - skipped)'."\n"; | |
next; | |
} | |
} | |
my $fileContents = get($url); | |
my $fh; | |
if (!$ext || !$outDir) | |
{ | |
my $mimetype = $ft->checktype_contents($fileContents); | |
($ext,$outDir) = getMetaInfo($url,$mimetype,1); | |
} | |
my $out = $outDir.'/'.$filteredName.'.'.$ext; | |
open($fh, '>',$out); | |
print $fh $fileContents; | |
close($fh); | |
print "done\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment