| 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 |
|
| 15018 |
kshitij.so |
16 |
public class SnapdealProductPageParserController extends BaseController{
|
| 15014 |
kshitij.so |
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 = "";
|
| 15017 |
kshitij.so |
25 |
private final String INDEX = "index";
|
| 15014 |
kshitij.so |
26 |
|
|
|
27 |
public String getJavaScriptToEmbedd() throws IOException, JSONException, URISyntaxException{
|
|
|
28 |
try{
|
|
|
29 |
parseUrl();
|
|
|
30 |
System.out.println(productUrl);
|
|
|
31 |
Document doc = Jsoup.connect(productUrl).get();
|
|
|
32 |
doc.outputSettings().charset("UTF-8");
|
|
|
33 |
JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
|
|
|
34 |
for (int element=0; element<jsonArray.length();element++){
|
|
|
35 |
if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
|
|
|
36 |
color = jsonArray.getJSONObject(element).getString("value");
|
|
|
37 |
}
|
|
|
38 |
}
|
| 15017 |
kshitij.so |
39 |
setResultJson(getJavaScriptCode());
|
| 15014 |
kshitij.so |
40 |
}
|
|
|
41 |
catch (Exception e){
|
|
|
42 |
log.error("Error while getting product details " +e);
|
| 15017 |
kshitij.so |
43 |
setResultJson(getJavaScriptCode());
|
| 15014 |
kshitij.so |
44 |
}
|
| 15017 |
kshitij.so |
45 |
return INDEX;
|
| 15014 |
kshitij.so |
46 |
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
private String getJavaScriptCode(){
|
|
|
50 |
String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+"';ele.onchange();}catch(error){Android.onError(error.message);}";
|
|
|
51 |
return jsCode;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
private void parseUrl() throws URISyntaxException{
|
| 15020 |
kshitij.so |
55 |
supc = request.getParameter("supc");
|
| 15014 |
kshitij.so |
56 |
productUrl = new URI(url).getHost()+new URI(url).getPath();
|
|
|
57 |
if (!productUrl.startsWith("http")){
|
|
|
58 |
productUrl = "http://"+productUrl;
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public void setUrl(String url) {
|
|
|
63 |
this.url = url;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public String getUrl() {
|
|
|
67 |
return url;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
|
| 15018 |
kshitij.so |
71 |
SnapdealProductPageParserController s = new SnapdealProductPageParserController();
|
| 15014 |
kshitij.so |
72 |
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");
|
|
|
73 |
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
}
|