dbiesecke
5/14/2015 - 2:38 AM

Will lookup the manga title and try to download all covers from mcd.iosphe.re

Will lookup the manga title and try to download all covers from mcd.iosphe.re

#!/usr/bin/perl -w
use JSON;
use LWP::Simple;
use Data::Dumper;


my $mudb;
my $home = "$ENV{'HOME'}";
my $mudbfile = $home."/.mudb.json";

sub loadUp(){

    if (-f $mudbfile) {
	    open(FILE,'<',$mudbfile);
		  while (<FILE>) {
		    $mudb .= $_;
		  }
	    close(FILE);
    }else {
	    print "[X] mudb.json not found, downloading it...\n";
	    $mudb = get('http://mcd.iosphe.re/api/v1/database/');
	    open(my $fh,'>',$mudbfile);
		    print $fh $mudb;
	    close($fh);	
    }
    return decode_json($mudb);
}


$db = loadUp();


sub searchManga(){
	my $search = $_[0];
	
	foreach $id (keys %{ $db } ){
		foreach $title (@{ $db->{$id} }) {
			#print $title."\n" if ( $title=~ /$search/ );
			next if not ($title =~/^$search$/i);
			return decode_json(get('http://mcd.iosphe.re/api/v1/series/'.$id.'/'));
		}
	}
	return -1;
}

sub downloadCover(){
    my $manga = $_[0];
    die("no cover in hash -> downloadCover\n") if !($manga->{Covers}->{a});
   

    foreach $cover (@{ $manga->{Covers}->{a} } ){
	  next if !($cover->{Side} eq 'front');
	  getstore($cover->{Normal}, $manga->{Title}.".v".$cover->{Volume}.".jpg");

# 	   print Dumper($cover)."\n";
    }
    
}

sub printInfo(){
  my $manga = $_[0];
  die("no MUid hash where given -> printInfo\n") if !($manga->{MUid});
  print "".$manga->{Title}."(".$manga->{ReleaseYear}.")\tAuthor:".$manga->{Authors}[0]."";
  print "\nStatus:\t";  
  print "\tCOMPLETE" if ( $manga->{StatusTags}->{Completed} );
  print "\t [".$manga->{VolumesAvailable}."/".$manga->{Volumes}."] (UID:".$manga->{MUid}.")\n";
  print "Tags\t\t".join(",",@{ $manga->{Tags} })."\n";
#   foreach $title (@{ $manga->{Tags} }) {
#   
#   }
}

my $id = &searchManga($ARGV[0]);
print Dumper($ARGV[0])."\n";
&printInfo($id);
&downloadCover($id);
exit;