Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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 = "";
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{
55
        List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
56
        for (NameValuePair param : params){
57
            if (param.getName().equalsIgnoreCase("supc")){
58
                supc = param.getValue();
59
            }
60
        }
61
        productUrl = new URI(url).getHost()+new URI(url).getPath();
62
        if (!productUrl.startsWith("http")){
63
            productUrl = "http://"+productUrl;
64
        }
65
    }
66
 
67
    public void setUrl(String url) {
68
        this.url = url;
69
    }
70
 
71
    public String getUrl() {
72
        return url;
73
    }
74
 
75
    public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
76
        SnapdealProductPageParser s = new SnapdealProductPageParser();
77
        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");
78
 
79
    }
80
 
81
 
82
 
83
}