Skip to content

Instantly share code, notes, and snippets.

@admiral0
Created May 9, 2013 11:20
Show Gist options
  • Save admiral0/5546915 to your computer and use it in GitHub Desktop.
Save admiral0/5546915 to your computer and use it in GitHub Desktop.
import.pl for solder. Imports all mods and versions into the solder DB. License: MIT
#!/usr/bin/perl
use warnings;
use strict;
use DBI();
##EDIT HERE##
my $c_database = "database";
my $c_host = "localhost";
my $c_dbuser="dbuser";
my $c_dbpass="dbpass";
my $repodir="/path/to/mcrepo/"; # It should contain a mods directory
## END ##
my $dsn="DBI:mysql:database=".$c_database.";host=".$c_host.";";
my $argc = @ARGV;
my $db=DBI->connect($dsn, $c_dbuser, $c_dbpass,
{'RaiseError' => 1});
if($argc>0 and $ARGV[0] eq "help"){
print "Usage: import.pl [directory]\n";
exit 0;
}
my $tmpdir = "$repodir/mods/";
if($argc>0){
$tmpdir=$ARGV[0];
}
sub addmod {
my $slug=shift;
$db->do("INSERT INTO mods(name,description,author,link,created_at,updated_at,pretty_name) VALUES(?,\"UPDATEME\",\"UPDATEME\",\"UPDATEME\",NOW(),NOW(),?);",undef,$slug,$slug);
my $sth = $db->prepare("SELECT id FROM mods WHERE name='$slug'");
$sth->execute();
my $modid=$sth->fetchrow_hashref()->{id};
$sth->finish();
my @files=glob("$repodir/mods/$slug/$slug-*.zip");
for my $modver (@files){
$modver =~ /$slug\-(.*).zip$/;
my $md5=`md5sum "$modver"`;
chomp($md5);
(my $checksum, my $rem)=split(/ /,$md5,2);
$db->do("INSERT INTO modversions(mod_id,version,md5,created_at,updated_at) VALUES(?,?,?,NOW(),NOW());",undef,$modid,$1,$checksum);
}
}
opendir(my $temph, $tmpdir) or die "Cannot open $tmpdir\n";
while(readdir $temph){
next if $_ eq ".";
next if $_ eq "..";
addmod($_);
}
$db->disconnect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment