com.astrientlabs.nyt.NYT.java Source code

Java tutorial

Introduction

Here is the source code for com.astrientlabs.nyt.NYT.java

Source

 /*******************************************************************************
  * Copyright (c) 2009 Licensed under the Apache License, 
  * Version 2.0 (the "License"); you may not use this file except in compliance 
  * with the License. You may obtain a copy of the License at 
  * 
  * http://www.apache.org/licenses/LICENSE-2.0 
  * 
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  * License for the specific language governing permissions and limitations under
  * the License.
  * 
  * Contributors:
  * 
  * Rashid Mayes 2009
  *******************************************************************************/
 package com.astrientlabs.nyt;

 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.URL;

 import org.apache.http.HttpResponse;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.codehaus.jackson.JsonGenerationException;
 import org.codehaus.jackson.JsonParseException;
 import org.codehaus.jackson.map.DeserializationConfig;
 import org.codehaus.jackson.map.JsonMappingException;
 import org.codehaus.jackson.map.ObjectMapper;
 import org.jsoup.Connection;
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;

 public class NYT {
public static final NYT instance = new NYT(<ENTER YOUR
     API KEY>);

     public enum Branch {
         Senate("senate"), House("house");

         String name;

         Branch(String name) {
             this.name = name;
         }
     }

     private String apiKey;
     private String baseUrl = "http://api.nytimes.com/svc/politics/v3/us/legislative/congress";
     private ObjectMapper mapper;

     private NYT(String apiKey) {
         this.apiKey = apiKey;
         mapper = new ObjectMapper();
         mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
     }

     public String getApiKey() {
         return apiKey;
     }

     public MembersResponse getMembers(int session, Branch branch, String state, String district)
             throws JsonParseException, JsonMappingException, IOException {
         String url = baseUrl + "/" + session + "/" + branch.name + "/members.json?api-key=" + apiKey;
         StringBuilder uparams = new StringBuilder(url);
         if (state != null) {
             uparams.append("&state=").append(state);
         }

         if (district != null) {
             uparams.append("&district=").append(district);
         }

         MembersResponse r = mapper.readValue(new URL(uparams.toString()), MembersResponse.class);
         if (branch != null) {
             Member[] items = r.getItems();
             if (items != null) {
                 for (Member item : items) {
                     item.setBranch(branch);
                 }
             }
         }
         return r;
     }

     public MemberResponse getMember(String id) throws JsonParseException, JsonMappingException, IOException {
         String url = baseUrl + "/members/" + id + ".json?api-key=" + apiKey;

         MemberResponse r = mapper.readValue(new URL(url), MemberResponse.class);
         return r;
     }

     public MemberResponse getMemberVotes(String id) throws JsonParseException, JsonMappingException, IOException {
         String url = baseUrl + "/members/" + id + "/votes.json?api-key=" + apiKey;

         MemberResponse r = mapper.readValue(new URL(url), MemberResponse.class);
         return r;
     }

     public void serialize(OutputStream os, Object value)
             throws JsonGenerationException, JsonMappingException, IOException {
         mapper.writeValue(os, value);
     }

     public String toString(Object value) throws JsonGenerationException, JsonMappingException, IOException {
         return mapper.writeValueAsString(value);
     }

     public void toFile(File file, Object value) throws JsonGenerationException, JsonMappingException, IOException {
         mapper.writeValue(file, value);
     }

     public <T> T parseString(String string, Class<T> valueType)
             throws JsonParseException, JsonMappingException, IOException {
         return mapper.readValue(string, valueType);
     }

     public <T> T parseFile(File file, Class<T> valueType)
             throws JsonParseException, JsonMappingException, IOException {
         return mapper.readValue(file, valueType);
     }

     public String extractImageURL(int session, Member member) throws IOException {
         String memberType = null;
         Branch branch = member.getBranch();
         if (branch == null) {
             memberType = "RP";
         } else if (branch == Branch.Senate) {
             memberType = "SR";
         } else if (branch == Branch.House) {
             memberType = "RP";
         }

         String name = member.getLast_name() + member.getFirst_name();
         if (member.getMiddle_name() != null) {
             name += member.getMiddle_name().replace('.', ' ').trim();
         }

         String url = extractImageURL(session, memberType, member.getLast_name().toUpperCase());
         if (url == null) {
             name = member.getLast_name();
             url = extractImageURL(session, memberType, name);
         }

         return url;
     }

     public String extractImageURL(int session, String memberType, String name) throws IOException {
         String url = "http://memberguide.gpo.gov/" + session + "/" + memberType + "/" + name;

         try {
             Connection c = Jsoup.connect(url);
             c.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1");

             Document doc = c.get();
             doc.normalise();

             Element content = doc.getElementById("ctl00_ContentPlaceHolder1_pic");
             if (content != null) {
                 String src = content.attr("src");
                 //System.out.println(src + " vs " + doc.baseUri());

                 if (src != null) {
                     URL u = new URL("http://memberguide.gpo.gov/" + session + "/" + memberType + "/" + src);
                     return u.toString();
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
         }

         return null;
     }

     public static void main(String[] args) {
         try {
             int session = 112;
             MembersResponse r = NYT.instance.getMembers(session, NYT.Branch.Senate, null, null);

             Member[] members = r.getItems();

             if (members != null) {
                 String imgUrl;
                 File dir = new File("/Users/rashidmayes/tmp/nyt/", String.valueOf(session));
                 dir.mkdirs();
                 File outDir;
                 File outFile;
                 for (Member member : members) {
                     System.out.println(member);
                     imgUrl = NYT.instance.extractImageURL(session, member);
                     System.out.println(imgUrl);
                     if (imgUrl != null) {
                         try {
                             HttpClient httpclient = new DefaultHttpClient();

                             HttpGet get = new HttpGet(imgUrl);
                             HttpResponse response = httpclient.execute(get);
                             if (response.getStatusLine().getStatusCode() == 200) {
                                 outDir = new File(dir, member.getId());
                                 outDir.mkdirs();
                                 outFile = new File(outDir,
                                         (member.getFirst_name() + "." + member.getLast_name() + ".jpg")
                                                 .toLowerCase());

                                 FileOutputStream fos = null;
                                 try {
                                     fos = new FileOutputStream(outFile);
                                     response.getEntity().writeTo(fos);
                                 } finally {
                                     if (fos != null) {
                                         fos.close();
                                     }
                                 }
                             }
                         } catch (Exception e) {
                             e.printStackTrace();
                         }
                     }
                 }
             }
         } catch (JsonParseException e) {
             e.printStackTrace();
         } catch (JsonMappingException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }

         System.exit(0);
     }
 }