Subversion Repositories SmartDukaan

Rev

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

Rev 15192 Rev 15237
Line 19... Line 19...
19
import in.shop2020.dtrapi.services.MemCache;
19
import in.shop2020.dtrapi.services.MemCache;
20
import in.shop2020.dtrapi.services.UserMessagePojo;
20
import in.shop2020.dtrapi.services.UserMessagePojo;
21
 
21
 
22
public class SnapdealProductPageParserController extends BaseController{
22
public class SnapdealProductPageParserController extends BaseController{
23
 
23
 
24
    private static Logger log = Logger.getLogger(Class.class);
24
	private static Logger log = Logger.getLogger(Class.class);
25
    private static final long serialVersionUID = 1L;
25
	private static final long serialVersionUID = 1L;
26
 
26
 
27
    private String url;
27
	private String url;
28
    private String supc = "";
28
	private String supc = "";
29
    private String productUrl = "";
29
	private String productUrl = "";
30
    private String color = "";
30
	private String color = "";
31
    private static final String INDEX = "index";
31
	private static final String INDEX = "index";
32
    private static final String KEY = "snapdealSupcToColorMapping";
32
	private static final String KEY = "snapdealSupcToColorMapping";
33
    private static MemCache memCache = new MemCache();
33
	private static MemCache memCache = new MemCache();
34
    private static MemCachedClient memcachedClient = memCache.getClient();
34
	private static MemCachedClient memcachedClient = memCache.getClient();
35
    
35
 
36
    
36
 
37
 
37
 
38
    public UserMessagePojo getColorMessage() throws IOException, JSONException, URISyntaxException{
38
	public UserMessagePojo getColorMessage() throws IOException, JSONException, URISyntaxException{
39
        try{
39
		try{
40
            parseUrl();
40
			parseUrl();
41
            if (!checkCache()){
41
			if (!checkCache()){
42
                System.out.println(productUrl);
-
 
43
                Document doc = Jsoup.connect(productUrl).get();
42
				Document doc = Jsoup.connect(productUrl).get();
44
                doc.outputSettings().charset("UTF-8");
43
				doc.outputSettings().charset("UTF-8");
45
                JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
44
				JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
46
                for (int element=0; element<jsonArray.length();element++){
45
				for (int element=0; element<jsonArray.length();element++){
-
 
46
					if (!color.isEmpty()){
-
 
47
						break;
-
 
48
					}
-
 
49
					try{
-
 
50
						JSONArray subAttributeArray = jsonArray.getJSONObject(element).getJSONArray("subAttributes");
-
 
51
						for (int innerElement=0; innerElement< subAttributeArray.length(); innerElement++){
47
                    if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
52
							if (supc.equalsIgnoreCase(subAttributeArray.getJSONObject(innerElement).getString("supc"))){
48
                        color = jsonArray.getJSONObject(element).getString("value");
53
								color = jsonArray.getJSONObject(element).getString("value");
-
 
54
							}
-
 
55
						}
-
 
56
					}
49
                    }
57
					catch(Exception e1){
50
                }
58
						e1.printStackTrace();
51
            }
59
					}
-
 
60
					if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
-
 
61
						color = jsonArray.getJSONObject(element).getString("value");
52
        }
62
					}
-
 
63
				}
-
 
64
			}
-
 
65
		}
53
        catch (Exception e){
66
		catch (Exception e){
54
            log.error("Error while getting product details " +e);
67
			log.error("Error while getting product details " +e);
55
        }
68
		}
56
        return getMsg();
69
		return getMsg();
57
    }
70
	}
58
    
71
 
59
    private UserMessagePojo getMsg(){
72
	private UserMessagePojo getMsg(){
60
    	UserMessagePojo ump = new UserMessagePojo();
73
		UserMessagePojo ump = new UserMessagePojo();
61
    	if (color.isEmpty()){
74
		if (color.isEmpty()){
62
    		ump.setResult(false);
75
			ump.setResult(false);
63
    		ump.setMessage("");
76
			ump.setMessage("");
64
    	}
77
		}
65
    	else{
78
		else{
66
    		ump.setResult(true);
79
			ump.setResult(true);
67
    		ump.setMessage("Please select "+color+" color to get this price");
80
			ump.setMessage("Please select "+color+" color to get this price");
68
    	}
81
		}
69
		return ump;
82
		return ump;
70
    	
83
 
71
    }
84
	}
72
    
85
 
73
    private String getJavaScriptCode(){
86
	private String getJavaScriptCode(){
74
        String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+";ele.onchange();}catch(error){Android.onError(error.message);}";
87
		String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+";ele.onchange();}catch(error){Android.onError(error.message);}";
75
        return jsCode;
88
		return jsCode;
76
    }
89
	}
77
 
90
 
78
    private void parseUrl() throws URISyntaxException{
91
	private void parseUrl() throws URISyntaxException{
79
        List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
92
		List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
80
        for (NameValuePair param : params){
93
		for (NameValuePair param : params){
81
            if (param.getName().equalsIgnoreCase("supc")){
94
			if (param.getName().equalsIgnoreCase("supc")){
82
                supc = param.getValue();
95
				supc = param.getValue();
83
            }
96
			}
84
        }
97
		}
85
        productUrl = new URI(url).getHost()+new URI(url).getPath();
98
		productUrl = new URI(url).getHost()+new URI(url).getPath();
86
        if (!productUrl.startsWith("http")){
99
		if (!productUrl.startsWith("http")){
87
            productUrl = "http://"+productUrl;
100
			productUrl = "http://"+productUrl;
88
        }
101
		}
89
    }
102
	}
90
    
103
 
91
    private void populateCache(String supc, String color){
104
	private void populateCache(String supc, String color){
92
        System.out.println("populating cache");
105
		System.out.println("populating cache");
93
        Object cacheValue = memcachedClient.get(KEY);
106
		Object cacheValue = memcachedClient.get(KEY);
94
        HashMap<String, String> supcMap = new HashMap<String, String>();
107
		HashMap<String, String> supcMap = new HashMap<String, String>();
95
        if (cacheValue != null) {
108
		if (cacheValue != null) {
96
            supcMap=(HashMap<String, String>)cacheValue;
109
			supcMap=(HashMap<String, String>)cacheValue;
97
        }
110
		}
98
        supcMap.put(supc, color);
111
		supcMap.put(supc, color);
99
        memcachedClient.set(KEY, supcMap);
112
		memcachedClient.set(KEY, supcMap);
100
    }
113
	}
101
 
114
 
102
    private boolean checkCache(){
115
	private boolean checkCache(){
103
        if (supc!=null){
116
		if (supc!=null){
104
            Object cacheValue = memcachedClient.get(KEY);
117
			Object cacheValue = memcachedClient.get(KEY);
105
            if (cacheValue != null) {
118
			if (cacheValue != null) {
106
                System.out.println("cache value is not null");
119
				System.out.println("cache value is not null");
107
                HashMap<String, String> supcMap = new HashMap<String, String>();
120
				HashMap<String, String> supcMap = new HashMap<String, String>();
108
                supcMap=(HashMap<String, String>)cacheValue;
121
				supcMap=(HashMap<String, String>)cacheValue;
109
                System.out.println("supc map "+supcMap);
122
				System.out.println("supc map "+supcMap);
110
                color = supcMap.get(supc);
123
				color = supcMap.get(supc);
111
                System.out.println("color "+color);
124
				System.out.println("color "+color);
112
                if (!(color==null || color.isEmpty())){
125
				if (!(color==null || color.isEmpty())){
113
                    return true;
126
					return true;
114
                }
127
				}
115
            }
128
			}
116
        }
129
		}
117
        return false;
130
		return false;
118
    }
131
	}
119
 
132
 
120
    public void setUrl(String url) {
133
	public void setUrl(String url) {
121
        byte[] decoded = Base64.decode(url);
134
		byte[] decoded = Base64.decode(url);
122
        this.url = new String(decoded);
135
		this.url = new String(decoded);
123
    }
136
	}
124
 
137
 
125
    public String getUrl() {
138
	public String getUrl() {
126
        return url;
139
		return url;
127
    }
140
	}
128
 
141
 
129
    public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
142
	public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
130
        SnapdealProductPageParserController s = new SnapdealProductPageParserController();
143
		SnapdealProductPageParserController s = new SnapdealProductPageParserController();
131
        String url = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3Qvc3BpY2UtYm9zcy1lbnRlcnRhaW5lci0zLW01NDA2LzEwNTA2ODk2MjM/c3VwYz1TREw3MDA0MzYzMTUmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MTc5MTUmYWZmX3N1Yj1TSEEzMTQzMTY3NzM0NQ==";
144
		//String url = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3QvaHRjLWRlc2lyZS01MjYtZy82ODE1MDk5MjQyNDg/c3VwYz1TREw1MjU0MDI1OTYmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MzM1NTAmYWZmX3N1Yj1TSEEzMTQzMjEwODk3MA==";
-
 
145
		String url = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3QvaHRjLWRlc2lyZS02MjYtZy82NDAwMzA4NDA3MTU/c3VwYz1TREw5MzU5MzI1NzgmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MzM1NTAmYWZmX3N1Yj1TSEEzMTQzMjExMzkxNQ==";
132
        byte[] decoded = Base64.decode(url);
146
		byte[] decoded = Base64.decode(url);
133
        System.out.println(new String(decoded));
147
		System.out.println(new String(decoded));
134
        s.setUrl(url);
148
		s.setUrl(url);
135
        s.getColorMessage();
-
 
136
        System.out.println(s.getResultJson());
149
		System.out.println(s.getColorMessage().getMessage());
137
    }
150
	}
138
 
151
 
139
}
152
}
140
153