/* * [FindRecentlyDied.java] * * Summary: Find authors who have recently died. * * Copyright: (c) 2012-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 2012-12-23 initial release */ package com.mindprod.repair; import com.mindprod.common18.EIO; import com.mindprod.csv.CSVReader; import com.mindprod.entities.DeEntifyStrings; import com.mindprod.http.Get; import java.io.BufferedReader; import java.io.EOFException; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import static java.lang.System.*; /** * Find authors who have recently died. *

* This does not work. Google turns off service. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2012-12-23 initial release * @since 2012-12-23 */ public class FindRecentlyDied { public static void main( final String[] args ) throws IOException { final BufferedReader br = EIO.getBufferedReader( new File( "E:/com/mindprod/repair/authors.csv" ), 64 * 1204, EIO.UTF8 ); final CSVReader r = new CSVReader( br, ',', '\"', "#", true /* hide comments */, true /* trim */, true /* trim */, true /* multiline */ ); try { while ( true ) { final String[] fields = r.getAllFieldsInLine(); if ( fields.length != 2 ) { continue; } final String author = DeEntifyStrings.deEntifyHTML( fields[ 0 ], ' ' ); final String born = fields[ 1 ]; // do GET query like this to google. // GET /search?q=%22Mitch+Kapor%22+born+1950-11-01+died final Get get = new Get(); get.setInstanceFollowRedirects( true ); get.setConnectTimeout( ( int ) TimeUnit.SECONDS.toMillis( 40 ) ); get.setParms( "q", "\"" + author + "\" born " + born ); final String response = get.send( "www.google.ca", -1, "/search", Get.UTF8 ); int responseCode = get.getResponseCode(); if ( responseCode != 200 || response == null || response.length() == 0 ) { err.println( responseCode + " " + get.getResponseMessage() + " no response for " + author ); continue; } final String lc = response.toLowerCase() .replace( '\n', ' ' ) .replace( '\t', ' ' ) .replace( '<', ' ' ) .replace( '>', ' ' ) .replace( '.', ' ' ) .replace( ',', ' ' ); final int where = lc.indexOf( " died " ); if ( where >= 0 ) { out.println( "-------------------------------------" ); out.println( author + " " + born ); final int start = Math.max( 0, where - 100 ); final int end = Math.min( where + 100, response.length() ); out.println( response.substring( start, where ) ); out.println( response.substring( where, end ) ); out.println( "-------------------------------------" ); } else { out.println( author + " " + born + " is still alive." ); } } } catch ( EOFException e ) { } } }