#!/usr/bin/perl use warnings; use strict; use DB::Anything; use Books::Utils; use Data::Dumper; use Getopt::Long; use Storable 'nstore', 'retrieve'; $| = 1; my $dsn = ''; my $testing = 0; my $result = GetOptions( "dsn=s" => \$dsn, "testing" => \$testing, ); print "$dsn, $testing\n"; if(!$result or !$dsn) { print "Usage: find_others.pl \n"; print " --dsn \n"; print " --testing\n"; # print " --user \n"; # print " --pass \n"; exit; } DB::Anything->load_classes({ 'Anything::Model::Anything' => ['Type', 'Items', 'Relationships', 'Admin', 'MajorMinor', 'TypeFields', 'ItemValues', 'TypeAttributes' ] }); my $dbschema = DB::Anything->connect($dsn); my %foundbooks = (); if(-f './otherbooks.cache') { %foundbooks = %{retrieve('./otherbooks.cache') || {}}; } my $authors = $dbschema->resultset('Items')->search({ 'TypeID.Name' => 'Author', }, { join => 'TypeID', order_by => 'ID' }); my $countfull = 0; foreach my $author ($authors->all) # foreach my $author ($dbschema->resultset('Items')->find({Name => 'Feist, Raymond E'})) { next if($foundbooks{$author->ID}{done} && !$testing); sleep 2; $foundbooks{$author->ID}{done} = 0 if($testing); my $name = $author->Name; $name =~ s/,//g; $foundbooks{$author->ID}{data} ||= Books::Utils::lookup_bl_uk('author', $name); if (!$foundbooks{$author->ID}{data} || !@{$foundbooks{$author->ID}{data}}) { $foundbooks{$author->ID}{done}=1; next; } # print Dumper($foundbooks{$author->ID}{data}); my $n=0; my $count = 0+@{$foundbooks{$author->ID}{data}}; foreach my $book (@{$foundbooks{$author->ID}{data}}) { $n++; print "Inserting book for '".$author->Name."': $n of $count\n"; my $parsed_data = Books::Utils::parse_sutrs($book); # print Dumper($parsed_data); if($parsed_data && $parsed_data->{_title} && $parsed_data->{_authors} && $parsed_data->{_isbn}) { next unless(@{$parsed_data->{'Dewey DC Class. No.'}}); # (American?) fiction in any language next unless($parsed_data->{'Dewey DC Class. No.'}[0] =~ /^8\d3/); Books::Utils::create_from_record($dbschema, $parsed_data, undef, 0); $countfull++; next; } } $foundbooks{$author->ID}{done}=1; system(qw(cp ./otherbooks.cache ./otherbooks.cache.back)); nstore(\%foundbooks, './otherbooks.cache'); # exit; } nstore(\%foundbooks, './otherbooks.cache');