Subversion Repositories SmartDukaan

Rev

Rev 15031 | Rev 15183 | Go to most recent revision | Details | Compare with Previous | 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;
15027 kshitij.so 6
import java.util.HashMap;
15014 kshitij.so 7
import java.util.List;
8
 
15027 kshitij.so 9
import org.apache.axis.encoding.Base64;
15014 kshitij.so 10
import org.apache.http.NameValuePair;
11
import org.apache.http.client.utils.URLEncodedUtils;
12
import org.apache.log4j.Logger;
13
import org.json.JSONArray;
14
import org.json.JSONException;
15
import org.jsoup.Jsoup;
16
import org.jsoup.nodes.Document;
17
 
15027 kshitij.so 18
import com.whalin.MemCached.MemCachedClient;
19
import in.shop2020.dtrapi.services.MemCache;
20
 
15018 kshitij.so 21
public class SnapdealProductPageParserController extends BaseController{
15014 kshitij.so 22
 
23
    private static Logger log = Logger.getLogger(Class.class);
24
    private static final long serialVersionUID = 1L;
25
 
26
    private String url;
27
    private String supc = "";
28
    private String productUrl = "";
29
    private String color = "";
15027 kshitij.so 30
    private static final String INDEX = "index";
31
    private static final String KEY = "snapdealSupcToColorMapping";
32
    private static MemCache memCache = new MemCache();
33
    private static MemCachedClient memcachedClient = memCache.getClient();
34
 
35
 
15014 kshitij.so 36
 
37
    public String getJavaScriptToEmbedd() throws IOException, JSONException, URISyntaxException{
38
        try{
39
            parseUrl();
15027 kshitij.so 40
            if (!checkCache()){
41
                System.out.println(productUrl);
42
                Document doc = Jsoup.connect(productUrl).get();
43
                doc.outputSettings().charset("UTF-8");
44
                JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
45
                for (int element=0; element<jsonArray.length();element++){
46
                    if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
47
                        color = jsonArray.getJSONObject(element).getString("value");
48
                        populateCache(supc, color);
49
                    }
15014 kshitij.so 50
                }
51
            }
15017 kshitij.so 52
            setResultJson(getJavaScriptCode());
15014 kshitij.so 53
        }
54
        catch (Exception e){
55
            log.error("Error while getting product details " +e);
15017 kshitij.so 56
            setResultJson(getJavaScriptCode());
15014 kshitij.so 57
        }
15017 kshitij.so 58
        return INDEX;
15027 kshitij.so 59
 
15014 kshitij.so 60
    }
61
 
62
    private String getJavaScriptCode(){
15033 kshitij.so 63
        String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+";ele.onchange();}catch(error){Android.onError(error.message);}";
15014 kshitij.so 64
        return jsCode;
65
    }
66
 
67
    private void parseUrl() throws URISyntaxException{
15027 kshitij.so 68
        List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
69
        for (NameValuePair param : params){
70
            if (param.getName().equalsIgnoreCase("supc")){
71
                supc = param.getValue();
72
            }
73
        }
15014 kshitij.so 74
        productUrl = new URI(url).getHost()+new URI(url).getPath();
75
        if (!productUrl.startsWith("http")){
76
            productUrl = "http://"+productUrl;
77
        }
78
    }
15027 kshitij.so 79
 
80
    private void populateCache(String supc, String color){
81
        System.out.println("populating cache");
82
        Object cacheValue = memcachedClient.get(KEY);
83
        HashMap<String, String> supcMap = new HashMap<String, String>();
84
        if (cacheValue != null) {
85
            supcMap=(HashMap<String, String>)cacheValue;
86
        }
15031 kshitij.so 87
        supcMap.put(supc, color);
15027 kshitij.so 88
        memcachedClient.set(KEY, supcMap);
89
    }
15014 kshitij.so 90
 
15027 kshitij.so 91
    private boolean checkCache(){
92
        if (supc!=null){
93
            Object cacheValue = memcachedClient.get(KEY);
94
            if (cacheValue != null) {
95
                System.out.println("cache value is not null");
96
                HashMap<String, String> supcMap = new HashMap<String, String>();
97
                supcMap=(HashMap<String, String>)cacheValue;
98
                System.out.println("supc map "+supcMap);
99
                color = supcMap.get(supc);
100
                System.out.println("color "+color);
101
                if (!(color==null || color.isEmpty())){
102
                    return true;
103
                }
104
            }
105
        }
106
        return false;
107
    }
108
 
15014 kshitij.so 109
    public void setUrl(String url) {
15027 kshitij.so 110
        byte[] decoded = Base64.decode(url);
111
        this.url = new String(decoded);
15014 kshitij.so 112
    }
113
 
114
    public String getUrl() {
115
        return url;
116
    }
117
 
118
    public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
15018 kshitij.so 119
        SnapdealProductPageParserController s = new SnapdealProductPageParserController();
15027 kshitij.so 120
        String url = "http://m.snapdeal.com/product/samsung-galaxy-grand-max/647437791381?supc=SDL021483758&aff_id=1&bhalala=0";
121
        s.setUrl(Base64.encode(url.getBytes()));
122
        s.getJavaScriptToEmbedd();
15014 kshitij.so 123
    }
124
 
125
}