/* * [FindWebBooks.java] * * Summary: See if there are Safari Web-readable versions of books. * * Copyright: (c) 2014-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2014-05-22 initial version */ package com.mindprod.stores; import com.mindprod.common18.EIO; import com.mindprod.common18.Misc; import com.mindprod.common18.Progress; import com.mindprod.common18.ST; import com.mindprod.htmlmacros.macro.Global; import com.mindprod.htmlmacros.support.ConfigurationForMindprod; import com.mindprod.http.Get; import com.mindprod.hunkio.HunkIO; import com.mindprod.isbn.ISBNValidate; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.System.*; /** * See if there are Safari Web-readable versions of books. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2014-05-22 initial version * @since 2014-05-22 */ public class FindWebBooks extends ManipulateMacros { /** * how long to give B&N to respond in millis. */ private static final int CONNECT_TIMEOUT = ( int ) TimeUnit.SECONDS.toMillis( 70 ); /** * how many milliseconds to stall between probes */ private static final long STALL = 2000; /** * pattern marking an nook ean */ private static final Pattern WEBBOOK = Pattern.compile( "Web ISBN-13: ([0-9\\-]+)" ); /** * where Book files are kept */ private static File bookDir; /** * If there is no webbook, looks for ane at SAFARI using base isbn. * If it finds one, insert it into the Book macro in E:\mindprod\book\123456789012.html * * @param args not used. * * @throws java.io.IOException if problem with URS or reading Book macro files. */ public static void main( String[] args ) throws IOException { Global.installConfiguration( new ConfigurationForMindprod() ); final File webrootDir = new File( Global.configuration.getLocalWebrootWithSlashes() ); bookDir = new File( webrootDir, "book" ); // gets asin from filename , not asin= parm String[] files = bookDir.list( new BookMacroFileFilter() ); final Progress progress = new Progress( files.length ); for ( String filename : files ) { final File file = new File( bookDir, filename ); final String contents = HunkIO.readEntireFile( file, HunkIO.UTF8 ); try { final String oneMacro = findFirstMacroContents( contents, "Book" ); final String oldWebBook = parseValueByKey( oneMacro, "webbook" ); if ( ST.isEmpty( oldWebBook ) ) { try { Thread.sleep( STALL ); } catch ( InterruptedException e ) { } final String isbn13 = ST.chopTrailingString( filename, ".html" ); final Get get = new Get(); get.setInstanceFollowRedirects( true ); // chase moved urls. get.setConnectTimeout( CONNECT_TIMEOUT ); // http://my.safaribooksonline.com/9780596009205 final String response = get.send( new URL( "http://my.safaribooksonline.com/" + isbn13 ), Get.UTF8 ); if ( response == null || response.length() == 0 ) { err.println( get.getResponseMessage() + " for " + isbn13 ); progress.progress( out ); continue; } final Matcher m = WEBBOOK.matcher( response ); if ( m.find() ) { final String webisbn13 = ISBNValidate.tidyISBN10or13RemovingDashes( m.group( 1 ) ); final String newContents = appendToCommaList( contents, "Book", "webbook", webisbn13, file ); if ( !newContents.equals( contents ) ) { HunkIO.writeEntireFile( file, newContents, HunkIO.UTF8 ); } } } } catch ( IllegalArgumentException e ) { err.println( "Error in file " + EIO.getCanOrAbsPath( file ) + " : " + e.getMessage() ); } progress.progress( out ); } // end for Misc.trackLastThread(); System.exit( 0 ); } // end main }