Subversion Repositories SmartDukaan

Rev

Rev 15020 | Rev 15030 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 15020 Rev 15027
Line 1... Line 1...
1
package in.shop2020.dtrapi.controllers;
1
package in.shop2020.dtrapi.controllers;
2
 
2
 
3
import java.io.IOException;
3
import java.io.IOException;
4
import java.net.URI;
4
import java.net.URI;
5
import java.net.URISyntaxException;
5
import java.net.URISyntaxException;
-
 
6
import java.util.HashMap;
6
import java.util.List;
7
import java.util.List;
7
 
8
 
-
 
9
import org.apache.axis.encoding.Base64;
8
import org.apache.http.NameValuePair;
10
import org.apache.http.NameValuePair;
9
import org.apache.http.client.utils.URLEncodedUtils;
11
import org.apache.http.client.utils.URLEncodedUtils;
10
import org.apache.log4j.Logger;
12
import org.apache.log4j.Logger;
11
import org.json.JSONArray;
13
import org.json.JSONArray;
12
import org.json.JSONException;
14
import org.json.JSONException;
13
import org.jsoup.Jsoup;
15
import org.jsoup.Jsoup;
14
import org.jsoup.nodes.Document;
16
import org.jsoup.nodes.Document;
15
 
17
 
-
 
18
import com.whalin.MemCached.MemCachedClient;
-
 
19
import in.shop2020.dtrapi.services.MemCache;
-
 
20
 
16
public class SnapdealProductPageParserController extends BaseController{
21
public class SnapdealProductPageParserController extends BaseController{
17
 
22
 
18
    private static Logger log = Logger.getLogger(Class.class);
23
    private static Logger log = Logger.getLogger(Class.class);
19
    private static final long serialVersionUID = 1L;
24
    private static final long serialVersionUID = 1L;
20
 
25
 
21
    private String url;
26
    private String url;
22
    private String supc = "";
27
    private String supc = "";
23
    private String productUrl = "";
28
    private String productUrl = "";
24
    private String color = "";
29
    private String color = "";
25
    private final String INDEX = "index";
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
    
26
 
36
 
27
    public String getJavaScriptToEmbedd() throws IOException, JSONException, URISyntaxException{
37
    public String getJavaScriptToEmbedd() throws IOException, JSONException, URISyntaxException{
28
        try{
38
        try{
29
            parseUrl();
39
            parseUrl();
-
 
40
            System.out.println(checkCache());
-
 
41
            if (!checkCache()){
30
            System.out.println(productUrl);
42
                System.out.println(productUrl);
31
            Document doc = Jsoup.connect(productUrl).get();
43
                Document doc = Jsoup.connect(productUrl).get();
32
            doc.outputSettings().charset("UTF-8");
44
                doc.outputSettings().charset("UTF-8");
33
            JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
45
                JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
34
            for (int element=0; element<jsonArray.length();element++){
46
                for (int element=0; element<jsonArray.length();element++){
35
                if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
47
                    if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
36
                    color = jsonArray.getJSONObject(element).getString("value");
48
                        color = jsonArray.getJSONObject(element).getString("value");
-
 
49
                        populateCache(supc, color);
-
 
50
                    }
37
                }
51
                }
38
            }
52
            }
39
            setResultJson(getJavaScriptCode());
53
            setResultJson(getJavaScriptCode());
40
        }
54
        }
41
        catch (Exception e){
55
        catch (Exception e){
42
            log.error("Error while getting product details " +e);
56
            log.error("Error while getting product details " +e);
43
            setResultJson(getJavaScriptCode());
57
            setResultJson(getJavaScriptCode());
44
        }
58
        }
45
        return INDEX;
59
        return INDEX;
46
        
60
 
47
    }
61
    }
48
    
62
    
49
    private String getJavaScriptCode(){
63
    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);}";
64
        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;
65
        return jsCode;
52
    }
66
    }
53
 
67
 
54
    private void parseUrl() throws URISyntaxException{
68
    private void parseUrl() throws URISyntaxException{
-
 
69
        List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
-
 
70
        for (NameValuePair param : params){
-
 
71
            if (param.getName().equalsIgnoreCase("supc")){
55
        supc = request.getParameter("supc");
72
                supc = param.getValue();
-
 
73
            }
-
 
74
        }
56
        productUrl = new URI(url).getHost()+new URI(url).getPath();
75
        productUrl = new URI(url).getHost()+new URI(url).getPath();
57
        if (!productUrl.startsWith("http")){
76
        if (!productUrl.startsWith("http")){
58
            productUrl = "http://"+productUrl;
77
            productUrl = "http://"+productUrl;
59
        }
78
        }
60
    }
79
    }
-
 
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
    }
-
 
96
 
-
 
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
    }
61
 
114
 
62
    public void setUrl(String url) {
115
    public void setUrl(String url) {
-
 
116
        byte[] decoded = Base64.decode(url);
63
        this.url = url;
117
        this.url = new String(decoded);
64
    }
118
    }
65
 
119
 
66
    public String getUrl() {
120
    public String getUrl() {
67
        return url;
121
        return url;
68
    }
122
    }
69
 
123
 
70
    public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
124
    public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
71
        SnapdealProductPageParserController s = new SnapdealProductPageParserController();
125
        SnapdealProductPageParserController s = new SnapdealProductPageParserController();
72
        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");
126
        String url = "http://m.snapdeal.com/product/samsung-galaxy-grand-max/647437791381?supc=SDL021483758&aff_id=1&bhalala=0";
73
 
-
 
-
 
127
        s.setUrl(Base64.encode(url.getBytes()));
-
 
128
        s.getJavaScriptToEmbedd();
74
    }
129
    }
75
 
130
 
76
 
-
 
77
 
-
 
78
}
131
}
79
132