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