Subversion Repositories SmartDukaan

Rev

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