Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.serving.services;

import in.shop2020.config.ConfigException;
import in.shop2020.serving.model.ShipmentUpdate;
import in.shop2020.thrift.clients.config.ConfigClient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;

public class FirstFlightTrackingService {
        private static Logger log = Logger.getLogger(Class.class);
        
        private static String FIRSTFLIGHT_URL;
        
        static{
                try {
                        FIRSTFLIGHT_URL = ConfigClient.getClient().get("firstflight_update_url");
                } catch (ConfigException e) {
                    log.error("Unable to get the tracking url from the config service", e);
                    FIRSTFLIGHT_URL = "http://www.firstflight.net/n_contrac_new.asp?tracking1=";
                }
        }
        
        public List<ShipmentUpdate> getUpdates(String awbNumber){
                 try {
                         String uri = FIRSTFLIGHT_URL + awbNumber;
                         log.info("First Flight Update URL: " + uri);
                     Document doc = Jsoup.connect(uri).get();  
                     Elements tables = doc.select("table");
                     List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
                     ShipmentUpdate update=null;
                     for(Element table : tables) {
                         Elements tableData = table.getElementsByTag("td");
                         if(tableData.text().matches("Date Serving Location Status.*")) {
                            for(int i=0;i<3;i++) {
                                tableData.remove(0);
                            }
                            int size = (tableData.size());
                            for (int i=0;i<size;i+=3){
                                update= new ShipmentUpdate();
                                update.date=tableData.get(i).text();
                                update.time="-";
                                update.city=tableData.get(i+1).text();
                                update.description=tableData.get(i+2).text();
                                updates.add(update);
                            }
                         return updates;
                         }
                  }
                   }
                        catch (IOException e) {
                                return null;
                            }
                return null;
         }
        
        
          public static void main(String[] args){
        System.out.println("Tracking ID " + "B59845966");
        FirstFlightTrackingService service = new FirstFlightTrackingService();
        List<ShipmentUpdate> updates = service.getUpdates("B59845966");
                for(ShipmentUpdate update: updates){
                        System.out.println(update.toString());
                }
    }
}