Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

White House Petition Parser 1.0

By: a guest on Nov 15th, 2012  |  syntax: Java  |  size: 2.84 KB  |  views: 51  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.datadreamer.whitehouse.petition;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.DataInputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileWriter;
  9. import java.io.InputStreamReader;
  10.  
  11. import org.jsoup.Jsoup;
  12. import org.jsoup.nodes.Document;
  13. import org.jsoup.nodes.Element;
  14. import org.jsoup.select.Elements;
  15.  
  16. public class PetitionParser {
  17.    
  18.    public void loadAndParse(String filename){
  19.       try{
  20.          // grab html from file
  21.          FileInputStream fstream = new FileInputStream(filename);
  22.          DataInputStream in = new DataInputStream(fstream);
  23.          BufferedReader br = new BufferedReader(new InputStreamReader(in));
  24.          String strLine;
  25.          StringBuffer buffer = new StringBuffer();
  26.          while((strLine = br.readLine()) != null){
  27.             buffer.append(strLine);
  28.          }
  29.          in.close();
  30.          // parse html for signature data
  31.          parse(buffer.toString());
  32.       } catch(Exception e){
  33.          e.printStackTrace();
  34.       }
  35.    }
  36.    
  37.    private void parse(String html){
  38.       try{
  39.          Document doc = Jsoup.parse(html);
  40.          Elements signatureDivs = doc.select("div[class^=entry]");
  41.          int sigCount = 0;
  42.          BufferedWriter dataFile = new BufferedWriter(new FileWriter(new File("data/signatures.txt"), true));
  43.          // iterate over every signature div
  44.          for(Element sigDiv : signatureDivs){
  45.             String name = sigDiv.getElementsByClass("name").html().trim();
  46.             String details = sigDiv.getElementsByClass("details").html();
  47.             String[] detailLines = details.split("\n");
  48.             String place = "";
  49.             String date = "";
  50.             String sigNum = "";
  51.             // grab the content from the signature divs
  52.             if(detailLines.length == 2){
  53.                date = detailLines[0].replace("<br />", "").trim();
  54.                sigNum = detailLines[1].split("# ")[1].trim();
  55.                sigCount++;
  56.             } else if (detailLines.length == 3){
  57.                place = detailLines[0];
  58.                date = detailLines[1].replace("<br />", "").trim();
  59.                sigNum = detailLines[2].split("# ")[1].trim();
  60.                sigCount++;
  61.             }
  62.             // format the data for each line using tab seperated values
  63.             String signatureData = sigNum +"\t"+ name +"\t"+ place +"\t"+ date;
  64.             //System.out.println(signatureData);
  65.             dataFile.write(signatureData);
  66.             dataFile.newLine();
  67.          }
  68.          dataFile.close();
  69.          System.out.println(sigCount +" signatures");
  70.       } catch(Exception e){
  71.          e.printStackTrace();
  72.       }
  73.    }
  74.    
  75.    static public void main(String[] args){
  76.       PetitionParser pp = new PetitionParser();
  77.       pp.loadAndParse("data/sample.html");
  78.    }
  79.  
  80. }