| 15014 |
kshitij.so |
1 |
package in.shop2020.dtrapi.controllers;
|
|
|
2 |
|
|
|
3 |
import java.io.IOException;
|
|
|
4 |
import java.net.URI;
|
|
|
5 |
import java.net.URISyntaxException;
|
|
|
6 |
import java.util.List;
|
|
|
7 |
|
|
|
8 |
import org.apache.http.NameValuePair;
|
|
|
9 |
import org.apache.http.client.utils.URLEncodedUtils;
|
|
|
10 |
import org.apache.log4j.Logger;
|
|
|
11 |
import org.json.JSONArray;
|
|
|
12 |
import org.json.JSONException;
|
|
|
13 |
import org.jsoup.Jsoup;
|
|
|
14 |
import org.jsoup.nodes.Document;
|
|
|
15 |
|
|
|
16 |
public class SnapdealProductPageParser extends BaseController{
|
|
|
17 |
|
|
|
18 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
19 |
private static final long serialVersionUID = 1L;
|
|
|
20 |
|
|
|
21 |
private String url;
|
|
|
22 |
private String supc = "";
|
|
|
23 |
private String productUrl = "";
|
|
|
24 |
private String color = "";
|
|
|
25 |
|
|
|
26 |
public String getJavaScriptToEmbedd() throws IOException, JSONException, URISyntaxException{
|
|
|
27 |
try{
|
|
|
28 |
parseUrl();
|
|
|
29 |
System.out.println(productUrl);
|
|
|
30 |
Document doc = Jsoup.connect(productUrl).get();
|
|
|
31 |
doc.outputSettings().charset("UTF-8");
|
|
|
32 |
JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
|
|
|
33 |
for (int element=0; element<jsonArray.length();element++){
|
|
|
34 |
if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
|
|
|
35 |
color = jsonArray.getJSONObject(element).getString("value");
|
|
|
36 |
}
|
|
|
37 |
}
|
|
|
38 |
return getJavaScriptCode();
|
|
|
39 |
}
|
|
|
40 |
catch (Exception e){
|
|
|
41 |
log.error("Error while getting product details " +e);
|
|
|
42 |
return "";
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
private String getJavaScriptCode(){
|
|
|
48 |
String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+"';ele.onchange();}catch(error){Android.onError(error.message);}";
|
|
|
49 |
return jsCode;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
private void parseUrl() throws URISyntaxException{
|
|
|
53 |
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
|
|
|
54 |
for (NameValuePair param : params){
|
|
|
55 |
if (param.getName().equalsIgnoreCase("supc")){
|
|
|
56 |
supc = param.getValue();
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
productUrl = new URI(url).getHost()+new URI(url).getPath();
|
|
|
60 |
if (!productUrl.startsWith("http")){
|
|
|
61 |
productUrl = "http://"+productUrl;
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
public void setUrl(String url) {
|
|
|
66 |
this.url = url;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public String getUrl() {
|
|
|
70 |
return url;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
|
|
|
74 |
SnapdealProductPageParser s = new SnapdealProductPageParser();
|
|
|
75 |
s.setUrl("http://m.snapdeal.com/product/htc-desire-620-g/2140463474?supc=SDL872629472&utm_source=aff_prog&utm_campaign=afts&offer_id=17&aff_id=33550&aff_sub=SHA31430302193");
|
|
|
76 |
System.out.println(s.getJavaScriptToEmbedd());
|
|
|
77 |
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
}
|