Subversion Repositories SmartDukaan

Rev

Rev 15020 | Rev 15030 | 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
            System.out.println(checkCache());
41
            if (!checkCache()){
42
                System.out.println(productUrl);
43
                Document doc = Jsoup.connect(productUrl).get();
44
                doc.outputSettings().charset("UTF-8");
45
                JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
46
                for (int element=0; element<jsonArray.length();element++){
47
                    if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
48
                        color = jsonArray.getJSONObject(element).getString("value");
49
                        populateCache(supc, color);
50
                    }
15014 kshitij.so 51
                }
52
            }
15017 kshitij.so 53
            setResultJson(getJavaScriptCode());
15014 kshitij.so 54
        }
55
        catch (Exception e){
56
            log.error("Error while getting product details " +e);
15017 kshitij.so 57
            setResultJson(getJavaScriptCode());
15014 kshitij.so 58
        }
15017 kshitij.so 59
        return INDEX;
15027 kshitij.so 60
 
15014 kshitij.so 61
    }
62
 
63
    private String getJavaScriptCode(){
64
        String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+"';ele.onchange();}catch(error){Android.onError(error.message);}";
65
        return jsCode;
66
    }
67
 
68
    private void parseUrl() throws URISyntaxException{
15027 kshitij.so 69
        List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
70
        for (NameValuePair param : params){
71
            if (param.getName().equalsIgnoreCase("supc")){
72
                supc = param.getValue();
73
            }
74
        }
15014 kshitij.so 75
        productUrl = new URI(url).getHost()+new URI(url).getPath();
76
        if (!productUrl.startsWith("http")){
77
            productUrl = "http://"+productUrl;
78
        }
79
    }
15027 kshitij.so 80
 
81
    private void populateCache(String supc, String color){
82
        System.out.println("populating cache");
83
        Object cacheValue = memcachedClient.get(KEY);
84
        HashMap<String, String> supcMap = new HashMap<String, String>();
85
        if (cacheValue != null) {
86
            supcMap = new HashMap<String, String>();
87
            supcMap=(HashMap<String, String>)cacheValue;
88
            supcMap.put(supc, color);
89
        }
90
        else{
91
            supcMap = new HashMap<String, String>();
92
            supcMap.put(supc, color);
93
        }
94
        memcachedClient.set(KEY, supcMap);
95
    }
15014 kshitij.so 96
 
15027 kshitij.so 97
    private boolean checkCache(){
98
        if (supc!=null){
99
            Object cacheValue = memcachedClient.get(KEY);
100
            if (cacheValue != null) {
101
                System.out.println("cache value is not null");
102
                HashMap<String, String> supcMap = new HashMap<String, String>();
103
                supcMap=(HashMap<String, String>)cacheValue;
104
                System.out.println("supc map "+supcMap);
105
                color = supcMap.get(supc);
106
                System.out.println("color "+color);
107
                if (!(color==null || color.isEmpty())){
108
                    return true;
109
                }
110
            }
111
        }
112
        return false;
113
    }
114
 
15014 kshitij.so 115
    public void setUrl(String url) {
15027 kshitij.so 116
        byte[] decoded = Base64.decode(url);
117
        this.url = new String(decoded);
15014 kshitij.so 118
    }
119
 
120
    public String getUrl() {
121
        return url;
122
    }
123
 
124
    public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
15018 kshitij.so 125
        SnapdealProductPageParserController s = new SnapdealProductPageParserController();
15027 kshitij.so 126
        String url = "http://m.snapdeal.com/product/samsung-galaxy-grand-max/647437791381?supc=SDL021483758&aff_id=1&bhalala=0";
127
        s.setUrl(Base64.encode(url.getBytes()));
128
        s.getJavaScriptToEmbedd();
15014 kshitij.so 129
    }
130
 
131
}