| 15014 |
kshitij.so |
1 |
package in.shop2020.dtrapi.controllers;
|
|
|
2 |
|
|
|
3 |
import java.io.IOException;
|
| 20383 |
kshitij.so |
4 |
import java.net.MalformedURLException;
|
| 15014 |
kshitij.so |
5 |
import java.net.URI;
|
|
|
6 |
import java.net.URISyntaxException;
|
| 20383 |
kshitij.so |
7 |
import java.net.URL;
|
| 15027 |
kshitij.so |
8 |
import java.util.HashMap;
|
| 15014 |
kshitij.so |
9 |
import java.util.List;
|
|
|
10 |
|
| 15027 |
kshitij.so |
11 |
import org.apache.axis.encoding.Base64;
|
| 15014 |
kshitij.so |
12 |
import org.apache.http.NameValuePair;
|
|
|
13 |
import org.apache.http.client.utils.URLEncodedUtils;
|
|
|
14 |
import org.apache.log4j.Logger;
|
|
|
15 |
import org.json.JSONArray;
|
|
|
16 |
import org.json.JSONException;
|
|
|
17 |
import org.jsoup.Jsoup;
|
|
|
18 |
import org.jsoup.nodes.Document;
|
|
|
19 |
|
| 15027 |
kshitij.so |
20 |
import com.whalin.MemCached.MemCachedClient;
|
|
|
21 |
import in.shop2020.dtrapi.services.MemCache;
|
| 15183 |
kshitij.so |
22 |
import in.shop2020.dtrapi.services.UserMessagePojo;
|
| 15027 |
kshitij.so |
23 |
|
| 15018 |
kshitij.so |
24 |
public class SnapdealProductPageParserController extends BaseController{
|
| 15014 |
kshitij.so |
25 |
|
| 15237 |
kshitij.so |
26 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
27 |
private static final long serialVersionUID = 1L;
|
| 15014 |
kshitij.so |
28 |
|
| 15237 |
kshitij.so |
29 |
private String url;
|
|
|
30 |
private String supc = "";
|
|
|
31 |
private String productUrl = "";
|
|
|
32 |
private String color = "";
|
| 20383 |
kshitij.so |
33 |
private String memory = "";
|
| 15237 |
kshitij.so |
34 |
private static final String INDEX = "index";
|
|
|
35 |
private static final String KEY = "snapdealSupcToColorMapping";
|
|
|
36 |
private static MemCache memCache = new MemCache();
|
|
|
37 |
private static MemCachedClient memcachedClient = memCache.getClient();
|
| 15014 |
kshitij.so |
38 |
|
| 15237 |
kshitij.so |
39 |
|
|
|
40 |
|
|
|
41 |
public UserMessagePojo getColorMessage() throws IOException, JSONException, URISyntaxException{
|
|
|
42 |
try{
|
|
|
43 |
parseUrl();
|
|
|
44 |
if (!checkCache()){
|
| 20383 |
kshitij.so |
45 |
Document doc = Jsoup.connect(productUrl).userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36")
|
|
|
46 |
.get();
|
| 15237 |
kshitij.so |
47 |
doc.outputSettings().charset("UTF-8");
|
| 18169 |
kshitij.so |
48 |
//System.out.println(doc);
|
|
|
49 |
JSONArray jsonArray = new JSONArray(doc.getElementById("attributesJson").text());
|
| 15237 |
kshitij.so |
50 |
for (int element=0; element<jsonArray.length();element++){
|
| 20383 |
kshitij.so |
51 |
if (jsonArray.getJSONObject(element).getString("subAttributeType").equalsIgnoreCase("memory")){
|
|
|
52 |
try{
|
|
|
53 |
JSONArray subAttributeArray = jsonArray.getJSONObject(element).getJSONArray("subAttributes");
|
|
|
54 |
for (int innerElement=0; innerElement< subAttributeArray.length(); innerElement++){
|
|
|
55 |
if (supc.equalsIgnoreCase(subAttributeArray.getJSONObject(innerElement).getString("supc"))){
|
|
|
56 |
System.out.println(subAttributeArray.getJSONObject(element).getString("value"));
|
|
|
57 |
memory = subAttributeArray.getJSONObject(element).getString("value");
|
|
|
58 |
color = jsonArray.getJSONObject(element).getString("value");
|
|
|
59 |
break;
|
|
|
60 |
}
|
| 15237 |
kshitij.so |
61 |
}
|
|
|
62 |
}
|
| 20383 |
kshitij.so |
63 |
catch(Exception e1){
|
|
|
64 |
e1.printStackTrace();
|
|
|
65 |
}
|
| 15237 |
kshitij.so |
66 |
}
|
|
|
67 |
if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
|
|
|
68 |
color = jsonArray.getJSONObject(element).getString("value");
|
|
|
69 |
}
|
| 15527 |
kshitij.so |
70 |
if (color!=null){
|
|
|
71 |
if (!color.isEmpty()){
|
|
|
72 |
populateCache(supc, color);
|
|
|
73 |
break;
|
|
|
74 |
}
|
|
|
75 |
}
|
| 15237 |
kshitij.so |
76 |
}
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
catch (Exception e){
|
| 20383 |
kshitij.so |
80 |
e.printStackTrace();
|
| 15237 |
kshitij.so |
81 |
}
|
|
|
82 |
return getMsg();
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
private UserMessagePojo getMsg(){
|
|
|
86 |
UserMessagePojo ump = new UserMessagePojo();
|
| 18169 |
kshitij.so |
87 |
//System.out.println("color is "+color);
|
|
|
88 |
if (color==null || color.isEmpty()){
|
| 15237 |
kshitij.so |
89 |
ump.setResult(false);
|
|
|
90 |
ump.setMessage("");
|
|
|
91 |
}
|
|
|
92 |
else{
|
|
|
93 |
ump.setResult(true);
|
| 20383 |
kshitij.so |
94 |
ump.setMessage("Please select "+color.trim()+" color to get this price.");
|
|
|
95 |
if (memory !=null && !memory.isEmpty()){
|
|
|
96 |
ump.setMessage("Please select "+color.trim()+" " +memory.trim()+" color to get this price.");
|
|
|
97 |
}
|
| 15237 |
kshitij.so |
98 |
}
|
| 15183 |
kshitij.so |
99 |
return ump;
|
| 15014 |
kshitij.so |
100 |
|
| 15237 |
kshitij.so |
101 |
}
|
| 15014 |
kshitij.so |
102 |
|
| 15237 |
kshitij.so |
103 |
private String getJavaScriptCode(){
|
|
|
104 |
String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+";ele.onchange();}catch(error){Android.onError(error.message);}";
|
|
|
105 |
return jsCode;
|
|
|
106 |
}
|
| 15027 |
kshitij.so |
107 |
|
| 20383 |
kshitij.so |
108 |
private void parseUrl() throws URISyntaxException, MalformedURLException{
|
| 15237 |
kshitij.so |
109 |
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
|
|
|
110 |
for (NameValuePair param : params){
|
|
|
111 |
if (param.getName().equalsIgnoreCase("supc")){
|
|
|
112 |
supc = param.getValue();
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
productUrl = new URI(url).getHost()+new URI(url).getPath();
|
|
|
116 |
if (!productUrl.startsWith("http")){
|
|
|
117 |
productUrl = "http://"+productUrl;
|
|
|
118 |
}
|
| 20383 |
kshitij.so |
119 |
// URL url = new URL(productUrl);
|
|
|
120 |
// String protocol = url.getProtocol();
|
|
|
121 |
// if (protocol ==null || protocol == "http"){
|
|
|
122 |
// productUrl.replace("http", "https");
|
|
|
123 |
// }
|
| 15237 |
kshitij.so |
124 |
}
|
| 15014 |
kshitij.so |
125 |
|
| 15237 |
kshitij.so |
126 |
private void populateCache(String supc, String color){
|
|
|
127 |
Object cacheValue = memcachedClient.get(KEY);
|
| 20383 |
kshitij.so |
128 |
HashMap<String, HashMap<String, String>> supcMap = new HashMap<String, HashMap<String, String>>();
|
| 15237 |
kshitij.so |
129 |
if (cacheValue != null) {
|
| 20383 |
kshitij.so |
130 |
supcMap=(HashMap<String, HashMap<String, String>>)cacheValue;
|
| 15237 |
kshitij.so |
131 |
}
|
| 20383 |
kshitij.so |
132 |
HashMap<String, String> productDetailMap = new HashMap<String, String>();
|
|
|
133 |
productDetailMap.put("color", color);
|
|
|
134 |
productDetailMap.put("memory", memory);
|
|
|
135 |
supcMap.put(supc, productDetailMap);
|
| 15237 |
kshitij.so |
136 |
memcachedClient.set(KEY, supcMap);
|
|
|
137 |
}
|
| 15014 |
kshitij.so |
138 |
|
| 15237 |
kshitij.so |
139 |
private boolean checkCache(){
|
|
|
140 |
if (supc!=null){
|
|
|
141 |
Object cacheValue = memcachedClient.get(KEY);
|
|
|
142 |
if (cacheValue != null) {
|
|
|
143 |
System.out.println("cache value is not null");
|
| 20383 |
kshitij.so |
144 |
HashMap<String, HashMap<String, String>> supcMap = new HashMap<String, HashMap<String, String>>();
|
|
|
145 |
supcMap=(HashMap<String, HashMap<String, String>>)cacheValue;
|
| 15237 |
kshitij.so |
146 |
System.out.println("supc map "+supcMap);
|
| 20383 |
kshitij.so |
147 |
HashMap<String, String> details = supcMap.get(supc);
|
|
|
148 |
if (details ==null){
|
|
|
149 |
return false;
|
|
|
150 |
}
|
|
|
151 |
if (!(details.get("color")==null || details.get("color").isEmpty())){
|
|
|
152 |
color = details.get("color");
|
|
|
153 |
memory = details.get("memory");
|
| 18169 |
kshitij.so |
154 |
System.out.println("Returning true");
|
| 15237 |
kshitij.so |
155 |
return true;
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
}
|
| 18169 |
kshitij.so |
159 |
System.out.println("Returning false");
|
| 15237 |
kshitij.so |
160 |
return false;
|
|
|
161 |
}
|
| 15014 |
kshitij.so |
162 |
|
| 15237 |
kshitij.so |
163 |
public void setUrl(String url) {
|
|
|
164 |
byte[] decoded = Base64.decode(url);
|
|
|
165 |
this.url = new String(decoded);
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
public String getUrl() {
|
|
|
169 |
return url;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
|
|
|
173 |
SnapdealProductPageParserController s = new SnapdealProductPageParserController();
|
| 20383 |
kshitij.so |
174 |
// //String url = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3QvaHRjLWRlc2lyZS01MjYtZy82ODE1MDk5MjQyNDg/c3VwYz1TREw1MjU0MDI1OTYmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MzM1NTAmYWZmX3N1Yj1TSEEzMTQzMjEwODk3MA==";
|
|
|
175 |
String url = "aHR0cHM6Ly93d3cuc25hcGRlYWwuY29tL3Byb2R1Y3QvaXBob25lLTZzLTY0Z2IvNjczMzYyNjQyNTUwP3N1cGM9U0RMNzcwODU3MDUx";
|
| 15237 |
kshitij.so |
176 |
byte[] decoded = Base64.decode(url);
|
|
|
177 |
System.out.println(new String(decoded));
|
|
|
178 |
s.setUrl(url);
|
|
|
179 |
System.out.println(s.getColorMessage().getMessage());
|
| 18169 |
kshitij.so |
180 |
//memcachedClient.set("java", "bawaaaa");
|
|
|
181 |
//System.out.println(memcachedClient.get("hi"));
|
| 15237 |
kshitij.so |
182 |
}
|
|
|
183 |
|
| 15014 |
kshitij.so |
184 |
}
|