/* * WPAPIView.java */ import java.io.*; import java.net.URL; public class WPAPISearch { 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); } byte [] crlf = new byte[]{13,10}; String testStr = ""; 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=ANumberYouGetByRegisteringWithTheWhitepages"; // http://developer.whitepages.com/ String phone = null; OutputStream People = null; try { People = new FileOutputStream("people.txt"); //store information about people in this file } catch (IOException e) { System.out.println("Failed to create People.txt\n"); } 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 String urlStr = urlStrFront + phone + urlStrEnd; URL url = null; try { url = new URL(urlStr); //call to WhitePAges API } catch (IOException IOe) { continue; } try { 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); 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); if (f.exists()) { f.delete(); } } else { String firstname = ""; String middlename = ""; String lastname = ""; int indexOpen = 0; int indexClose = 0; indexOpen = testStr.indexOf(""); if (indexOpen != -1) { indexClose = testStr.indexOf(""); firstname = testStr.substring(indexOpen + 14, indexClose); System.out.println(firstname); } indexOpen = testStr.indexOf(""); if (indexOpen != -1) { indexClose = testStr.indexOf(""); middlename = testStr.substring(indexOpen + 15, indexClose); } indexOpen = testStr.indexOf(""); if (indexOpen != -1) { indexClose = testStr.indexOf(""); lastname = testStr.substring(indexOpen + 13, indexClose); } if ((firstname.length() > 0) && (lastname.length() > 0)) { String fullName = firstname + " " + lastname; byte[] nameArray = fullName.getBytes(); People.write(nameArray); //write name to people.txt People.write(crlf); } } try { Thread.sleep(1000); } catch (Exception e) { System.out.println("Failed to sleep for 1 second\n"); } } catch (IOException IOe) { continue; //skip to next phone number } } try { People.close(); } catch (IOException e) { System.out.println("People.txt is still in use at the end of the program\n"); } } } }