Subversion Repositories SmartDukaan

Rev

Rev 15027 | Rev 15031 | 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(){
63
        String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+"';ele.onchange();}catch(error){Android.onError(error.message);}";
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
            supcMap.put(supc, color);
87
        }
88
        else{
89
            supcMap.put(supc, color);
90
        }
91
        memcachedClient.set(KEY, supcMap);
92
    }
15014 kshitij.so 93
 
15027 kshitij.so 94
    private boolean checkCache(){
95
        if (supc!=null){
96
            Object cacheValue = memcachedClient.get(KEY);
97
            if (cacheValue != null) {
98
                System.out.println("cache value is not null");
99
                HashMap<String, String> supcMap = new HashMap<String, String>();
100
                supcMap=(HashMap<String, String>)cacheValue;
101
                System.out.println("supc map "+supcMap);
102
                color = supcMap.get(supc);
103
                System.out.println("color "+color);
104
                if (!(color==null || color.isEmpty())){
105
                    return true;
106
                }
107
            }
108
        }
109
        return false;
110
    }
111
 
15014 kshitij.so 112
    public void setUrl(String url) {
15027 kshitij.so 113
        byte[] decoded = Base64.decode(url);
114
        this.url = new String(decoded);
15014 kshitij.so 115
    }
116
 
117
    public String getUrl() {
118
        return url;
119
    }
120
 
121
    public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
15018 kshitij.so 122
        SnapdealProductPageParserController s = new SnapdealProductPageParserController();
15027 kshitij.so 123
        String url = "http://m.snapdeal.com/product/samsung-galaxy-grand-max/647437791381?supc=SDL021483758&aff_id=1&bhalala=0";
124
        s.setUrl(Base64.encode(url.getBytes()));
125
        s.getJavaScriptToEmbedd();
15014 kshitij.so 126
    }
127
 
128
}