/*
* WPAPISearchVoters.java
*/
import java.io.*;
import java.net.*;
import java.lang.Integer;
import javax.script.*;
import java.util.regex.*;
public class WPAPISearchVoters
{
public static void main(String [] args)
{
if (args.length < 3)
{
System.out.println("Usage: java WPAPISearch Areacode Exchange LineNumber");
System.out.println("Where Areacode is a 3 digit number");
System.out.println("Where Exchange is a 3 digit number");
System.out.println("...and LineNumber is a 4 digit number");
System.out.println("Enter spaces between Areacode and Exchange and LineNumber");
System.exit(0);
}
//create the JavaScript Engine for this program
//Not that we use JavaScript functions in this program
//The Clerk's website returns an error, JavaScript not enabled, if we don't "enable" JS
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// javax.script.Invocable is an optional interface.
// Check whether your script engine implements or not!
// Note that the JavaScript engine implements Invocable interface.
Invocable inv = (Invocable) engine;
int hitCntr = 0;
String firstname = "";
String middlename = "";
String middleInit = "";
String lastname = "";
String buffy = "";
String bday = "";
byte [] crlf = new byte[]{13,10}; //I was using primitive file handling- leaving this here
String testStr = "";
String htmlStr = ""; //this is the name of the final result of the main loop - a html file
int AreaCodeInt = Integer.parseInt(args[0]);
int ExchangeInt = Integer.parseInt(args[1]);
int LineInt = Integer.parseInt(args[2]);
InputStream whiteIn = null; //input stream from WhitePages API page
OutputStream whiteOut = null; //output stream to capture returned XML
String urlStrFront = "http://api.whitepages.com/reverse_phone/1.0/?phone=";
String urlStrEnd = ";api_key=akeyyougetfromthe whitepagesapiwebsite";
String phone = null;
for (int exNum = ExchangeInt; exNum > 99; exNum--)
{
for (int lineNum = LineInt; lineNum >= 0; lineNum--)
{
String exNumStr;
String lineNumStr;
if (exNum < 10)
{
exNumStr = "00" + exNum;
}
else if (exNum < 100)
{
exNumStr = "0" + exNum;
}
else
{
exNumStr = Integer.toString(exNum);
}
if (lineNum < 10)
{
lineNumStr = "000" + lineNum;
}
else if (lineNum < 100)
{
lineNumStr = "00" + lineNum;
}
else if (lineNum < 1000)
{
lineNumStr = "0" + lineNum;
}
else
{
lineNumStr = Integer.toString(lineNum);
}
String npa = args[0];
phone = npa + exNumStr;
phone = phone + lineNumStr; //phone number to check
//System.out.println("Checking on phone #: " + phone);
String urlStr = urlStrFront + phone + urlStrEnd;
URL url = null;
try
{
//System.out.println("Create XML file");
url = new URL(urlStr); //call to WhitePAges API
//System.out.println("Check on how many hits we have");
if (++hitCntr > 200) break; //we only get 200 queries/day
}
catch (IOException IOe)
{
continue;
}
try
{
//System.out.println("Analyzing data in XML file");
boolean noname = true; //set to false if we find a name
whiteIn = url.openStream(); //create a stream to it
whiteOut = new FileOutputStream(phone); //name XML file the same as the phone number being tested
byte[] buffer = new byte[4096]; //will hold XML output
int bytes_read;
while((bytes_read = whiteIn.read(buffer)) != -1)
{
testStr = new String(buffer);
//System.out.println("Checking out: " + testStr);
if (testStr.indexOf("name") != -1)
{
noname = false;
}
whiteOut.write(buffer,0,bytes_read); //write XML
}
whiteIn.close();
whiteOut.close();
//if there is no string "name" in this file, delete it
if (noname)
{
File f = new File(phone);
//System.out.println("No name in this string");
if (f.exists())
{
f.delete();
}
}
else
{
firstname = "";
middlename = "";
middleInit = "";
lastname = "";
int indexOpen = 0;
int indexClose = 0;
indexOpen = testStr.indexOf("