Subversion Repositories SmartDukaan

Rev

Rev 15033 | Rev 15185 | 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;
15183 kshitij.so 20
import in.shop2020.dtrapi.services.UserMessagePojo;
15027 kshitij.so 21
 
15018 kshitij.so 22
public class SnapdealProductPageParserController extends BaseController{
15014 kshitij.so 23
 
24
    private static Logger log = Logger.getLogger(Class.class);
25
    private static final long serialVersionUID = 1L;
26
 
27
    private String url;
28
    private String supc = "";
29
    private String productUrl = "";
30
    private String color = "";
15027 kshitij.so 31
    private static final String INDEX = "index";
32
    private static final String KEY = "snapdealSupcToColorMapping";
33
    private static MemCache memCache = new MemCache();
34
    private static MemCachedClient memcachedClient = memCache.getClient();
35
 
36
 
15014 kshitij.so 37
 
15183 kshitij.so 38
    public String getColorMessage() throws IOException, JSONException, URISyntaxException{
15014 kshitij.so 39
        try{
40
            parseUrl();
15027 kshitij.so 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
            }
53
        }
54
        catch (Exception e){
55
            log.error("Error while getting product details " +e);
56
        }
15183 kshitij.so 57
        setResultJson(getMsg());
15017 kshitij.so 58
        return INDEX;
15027 kshitij.so 59
 
15014 kshitij.so 60
    }
61
 
15183 kshitij.so 62
    private UserMessagePojo getMsg(){
63
    	UserMessagePojo ump = new UserMessagePojo();
64
    	if (color.isEmpty()){
65
    		ump.setResult(false);
66
    		ump.setMessage("");
67
    	}
68
    	else{
69
    		ump.setResult(true);
70
    		ump.setMessage("Please select "+color+" to get this price");
71
    	}
72
		return ump;
73
 
74
    }
75
 
15014 kshitij.so 76
    private String getJavaScriptCode(){
15033 kshitij.so 77
        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 78
        return jsCode;
79
    }
80
 
81
    private void parseUrl() throws URISyntaxException{
15027 kshitij.so 82
        List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
83
        for (NameValuePair param : params){
84
            if (param.getName().equalsIgnoreCase("supc")){
85
                supc = param.getValue();
86
            }
87
        }
15014 kshitij.so 88
        productUrl = new URI(url).getHost()+new URI(url).getPath();
89
        if (!productUrl.startsWith("http")){
90
            productUrl = "http://"+productUrl;
91
        }
92
    }
15027 kshitij.so 93
 
94
    private void populateCache(String supc, String color){
95
        System.out.println("populating cache");
96
        Object cacheValue = memcachedClient.get(KEY);
97
        HashMap<String, String> supcMap = new HashMap<String, String>();
98
        if (cacheValue != null) {
99
            supcMap=(HashMap<String, String>)cacheValue;
100
        }
15031 kshitij.so 101
        supcMap.put(supc, color);
15027 kshitij.so 102
        memcachedClient.set(KEY, supcMap);
103
    }
15014 kshitij.so 104
 
15027 kshitij.so 105
    private boolean checkCache(){
106
        if (supc!=null){
107
            Object cacheValue = memcachedClient.get(KEY);
108
            if (cacheValue != null) {
109
                System.out.println("cache value is not null");
110
                HashMap<String, String> supcMap = new HashMap<String, String>();
111
                supcMap=(HashMap<String, String>)cacheValue;
112
                System.out.println("supc map "+supcMap);
113
                color = supcMap.get(supc);
114
                System.out.println("color "+color);
115
                if (!(color==null || color.isEmpty())){
116
                    return true;
117
                }
118
            }
119
        }
120
        return false;
121
    }
122
 
15014 kshitij.so 123
    public void setUrl(String url) {
15027 kshitij.so 124
        byte[] decoded = Base64.decode(url);
125
        this.url = new String(decoded);
15014 kshitij.so 126
    }
127
 
128
    public String getUrl() {
129
        return url;
130
    }
131
 
132
    public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
15018 kshitij.so 133
        SnapdealProductPageParserController s = new SnapdealProductPageParserController();
15027 kshitij.so 134
        String url = "http://m.snapdeal.com/product/samsung-galaxy-grand-max/647437791381?supc=SDL021483758&aff_id=1&bhalala=0";
135
        s.setUrl(Base64.encode(url.getBytes()));
15183 kshitij.so 136
        s.getColorMessage();
137
        System.out.println(s.getResultJson());
15014 kshitij.so 138
    }
139
 
140
}