Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6627 kshitij.so 1
package in.shop2020.serving.services;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.serving.model.ShipmentUpdate;
5
import in.shop2020.thrift.clients.config.ConfigClient;
6
 
7
import java.io.IOException;
8
import java.util.ArrayList;
9
import java.util.List;
10
import org.apache.log4j.Logger;
11
import org.jsoup.Jsoup;
12
import org.jsoup.nodes.Document;
13
import org.jsoup.select.Elements;
14
import org.jsoup.nodes.Element;
15
 
16
public class FirstFlightTrackingService {
17
	private static Logger log = Logger.getLogger(Class.class);
18
 
19
	private static String FIRSTFLIGHT_URL;
20
 
21
	static{
22
		try {
23
			FIRSTFLIGHT_URL = ConfigClient.getClient().get("firstflight_update_url");
24
		} catch (ConfigException e) {
25
		    log.error("Unable to get the tracking url from the config service", e);
26
		    FIRSTFLIGHT_URL = "http://www.firstflight.net/n_contrac_new.asp?tracking1=";
27
		}
28
	}
29
 
30
	public List<ShipmentUpdate> getUpdates(String awbNumber){
31
		 try {
32
			 String uri = FIRSTFLIGHT_URL + awbNumber;
33
			 log.info("First Flight Update URL: " + uri);
34
		     Document doc = Jsoup.connect(uri).get();  
35
		     Elements tables = doc.select("table");
36
		     List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
37
		     ShipmentUpdate update=null;
38
		     for(Element table : tables) {
39
		         Elements tableData = table.getElementsByTag("td");
40
		         if(tableData.text().matches("Date Serving Location Status.*")) {
41
		            for(int i=0;i<3;i++) {
42
		            	tableData.remove(0);
43
		            }
44
		            int size = (tableData.size());
45
		            for (int i=0;i<size;i+=3){
46
		            	update= new ShipmentUpdate();
47
		            	update.date=tableData.get(i).text();
48
		            	update.time="-";
49
		            	update.city=tableData.get(i+1).text();
50
		            	update.description=tableData.get(i+2).text();
51
		            	updates.add(update);
52
		            }
53
		         return updates;
54
		         }
55
	          }
56
		   }
57
		        catch (IOException e) {
58
			        return null;
59
			    }
60
		return null;
61
	 }
62
 
63
 
64
	  public static void main(String[] args){
65
        System.out.println("Tracking ID " + "B59845966");
66
        FirstFlightTrackingService service = new FirstFlightTrackingService();
67
        List<ShipmentUpdate> updates = service.getUpdates("B59845966");
68
		for(ShipmentUpdate update: updates){
69
			System.out.println(update.toString());
70
		}
71
    }
72
}
73