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