| Line 1... |
Line 1... |
| 1 |
package in.shop2020.dtrapi.controllers;
|
1 |
package in.shop2020.dtrapi.controllers;
|
| 2 |
|
2 |
|
| 3 |
import java.io.IOException;
|
3 |
import java.io.IOException;
|
| - |
|
4 |
import java.net.MalformedURLException;
|
| 4 |
import java.net.URI;
|
5 |
import java.net.URI;
|
| 5 |
import java.net.URISyntaxException;
|
6 |
import java.net.URISyntaxException;
|
| - |
|
7 |
import java.net.URL;
|
| 6 |
import java.util.HashMap;
|
8 |
import java.util.HashMap;
|
| 7 |
import java.util.List;
|
9 |
import java.util.List;
|
| 8 |
|
10 |
|
| 9 |
import org.apache.axis.encoding.Base64;
|
11 |
import org.apache.axis.encoding.Base64;
|
| 10 |
import org.apache.http.NameValuePair;
|
12 |
import org.apache.http.NameValuePair;
|
| Line 26... |
Line 28... |
| 26 |
|
28 |
|
| 27 |
private String url;
|
29 |
private String url;
|
| 28 |
private String supc = "";
|
30 |
private String supc = "";
|
| 29 |
private String productUrl = "";
|
31 |
private String productUrl = "";
|
| 30 |
private String color = "";
|
32 |
private String color = "";
|
| - |
|
33 |
private String memory = "";
|
| 31 |
private static final String INDEX = "index";
|
34 |
private static final String INDEX = "index";
|
| 32 |
private static final String KEY = "snapdealSupcToColorMapping";
|
35 |
private static final String KEY = "snapdealSupcToColorMapping";
|
| 33 |
private static MemCache memCache = new MemCache();
|
36 |
private static MemCache memCache = new MemCache();
|
| 34 |
private static MemCachedClient memcachedClient = memCache.getClient();
|
37 |
private static MemCachedClient memcachedClient = memCache.getClient();
|
| 35 |
|
38 |
|
| Line 37... |
Line 40... |
| 37 |
|
40 |
|
| 38 |
public UserMessagePojo getColorMessage() throws IOException, JSONException, URISyntaxException{
|
41 |
public UserMessagePojo getColorMessage() throws IOException, JSONException, URISyntaxException{
|
| 39 |
try{
|
42 |
try{
|
| 40 |
parseUrl();
|
43 |
parseUrl();
|
| 41 |
if (!checkCache()){
|
44 |
if (!checkCache()){
|
| 42 |
Document doc = Jsoup.connect(productUrl).get();
|
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();
|
| 43 |
doc.outputSettings().charset("UTF-8");
|
47 |
doc.outputSettings().charset("UTF-8");
|
| 44 |
//System.out.println(doc);
|
48 |
//System.out.println(doc);
|
| 45 |
JSONArray jsonArray = new JSONArray(doc.getElementById("attributesJson").text());
|
49 |
JSONArray jsonArray = new JSONArray(doc.getElementById("attributesJson").text());
|
| 46 |
//System.out.println(jsonArray);
|
- |
|
| 47 |
for (int element=0; element<jsonArray.length();element++){
|
50 |
for (int element=0; element<jsonArray.length();element++){
|
| - |
|
51 |
if (jsonArray.getJSONObject(element).getString("subAttributeType").equalsIgnoreCase("memory")){
|
| 48 |
try{
|
52 |
try{
|
| 49 |
JSONArray subAttributeArray = jsonArray.getJSONObject(element).getJSONArray("subAttributes");
|
53 |
JSONArray subAttributeArray = jsonArray.getJSONObject(element).getJSONArray("subAttributes");
|
| 50 |
for (int innerElement=0; innerElement< subAttributeArray.length(); innerElement++){
|
54 |
for (int innerElement=0; innerElement< subAttributeArray.length(); innerElement++){
|
| 51 |
if (supc.equalsIgnoreCase(subAttributeArray.getJSONObject(innerElement).getString("supc"))){
|
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");
|
| 52 |
color = jsonArray.getJSONObject(element).getString("value");
|
58 |
color = jsonArray.getJSONObject(element).getString("value");
|
| - |
|
59 |
break;
|
| - |
|
60 |
}
|
| 53 |
}
|
61 |
}
|
| 54 |
}
|
62 |
}
|
| 55 |
}
|
- |
|
| 56 |
catch(Exception e1){
|
63 |
catch(Exception e1){
|
| 57 |
e1.printStackTrace();
|
64 |
e1.printStackTrace();
|
| - |
|
65 |
}
|
| 58 |
}
|
66 |
}
|
| 59 |
if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
|
67 |
if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
|
| 60 |
color = jsonArray.getJSONObject(element).getString("value");
|
68 |
color = jsonArray.getJSONObject(element).getString("value");
|
| 61 |
}
|
69 |
}
|
| 62 |
if (color!=null){
|
70 |
if (color!=null){
|
| Line 67... |
Line 75... |
| 67 |
}
|
75 |
}
|
| 68 |
}
|
76 |
}
|
| 69 |
}
|
77 |
}
|
| 70 |
}
|
78 |
}
|
| 71 |
catch (Exception e){
|
79 |
catch (Exception e){
|
| 72 |
log.error("Error while getting product details " +e);
|
80 |
e.printStackTrace();
|
| 73 |
}
|
81 |
}
|
| 74 |
return getMsg();
|
82 |
return getMsg();
|
| 75 |
}
|
83 |
}
|
| 76 |
|
84 |
|
| 77 |
private UserMessagePojo getMsg(){
|
85 |
private UserMessagePojo getMsg(){
|
| Line 81... |
Line 89... |
| 81 |
ump.setResult(false);
|
89 |
ump.setResult(false);
|
| 82 |
ump.setMessage("");
|
90 |
ump.setMessage("");
|
| 83 |
}
|
91 |
}
|
| 84 |
else{
|
92 |
else{
|
| 85 |
ump.setResult(true);
|
93 |
ump.setResult(true);
|
| 86 |
ump.setMessage("Please select "+color+" color to get this price.");
|
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 |
}
|
| 87 |
}
|
98 |
}
|
| 88 |
return ump;
|
99 |
return ump;
|
| 89 |
|
100 |
|
| 90 |
}
|
101 |
}
|
| 91 |
|
102 |
|
| 92 |
private String getJavaScriptCode(){
|
103 |
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);}";
|
104 |
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;
|
105 |
return jsCode;
|
| 95 |
}
|
106 |
}
|
| 96 |
|
107 |
|
| 97 |
private void parseUrl() throws URISyntaxException{
|
108 |
private void parseUrl() throws URISyntaxException, MalformedURLException{
|
| 98 |
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
|
109 |
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
|
| 99 |
for (NameValuePair param : params){
|
110 |
for (NameValuePair param : params){
|
| 100 |
if (param.getName().equalsIgnoreCase("supc")){
|
111 |
if (param.getName().equalsIgnoreCase("supc")){
|
| 101 |
supc = param.getValue();
|
112 |
supc = param.getValue();
|
| 102 |
}
|
113 |
}
|
| 103 |
}
|
114 |
}
|
| 104 |
productUrl = new URI(url).getHost()+new URI(url).getPath();
|
115 |
productUrl = new URI(url).getHost()+new URI(url).getPath();
|
| 105 |
if (!productUrl.startsWith("http")){
|
116 |
if (!productUrl.startsWith("http")){
|
| 106 |
productUrl = "http://"+productUrl;
|
117 |
productUrl = "http://"+productUrl;
|
| 107 |
}
|
118 |
}
|
| - |
|
119 |
// URL url = new URL(productUrl);
|
| - |
|
120 |
// String protocol = url.getProtocol();
|
| - |
|
121 |
// if (protocol ==null || protocol == "http"){
|
| - |
|
122 |
// productUrl.replace("http", "https");
|
| - |
|
123 |
// }
|
| 108 |
}
|
124 |
}
|
| 109 |
|
125 |
|
| 110 |
private void populateCache(String supc, String color){
|
126 |
private void populateCache(String supc, String color){
|
| 111 |
Object cacheValue = memcachedClient.get(KEY);
|
127 |
Object cacheValue = memcachedClient.get(KEY);
|
| 112 |
HashMap<String, String> supcMap = new HashMap<String, String>();
|
128 |
HashMap<String, HashMap<String, String>> supcMap = new HashMap<String, HashMap<String, String>>();
|
| 113 |
if (cacheValue != null) {
|
129 |
if (cacheValue != null) {
|
| 114 |
supcMap=(HashMap<String, String>)cacheValue;
|
130 |
supcMap=(HashMap<String, HashMap<String, String>>)cacheValue;
|
| 115 |
}
|
131 |
}
|
| - |
|
132 |
HashMap<String, String> productDetailMap = new HashMap<String, String>();
|
| - |
|
133 |
productDetailMap.put("color", color);
|
| - |
|
134 |
productDetailMap.put("memory", memory);
|
| 116 |
supcMap.put(supc, color);
|
135 |
supcMap.put(supc, productDetailMap);
|
| 117 |
memcachedClient.set(KEY, supcMap);
|
136 |
memcachedClient.set(KEY, supcMap);
|
| 118 |
}
|
137 |
}
|
| 119 |
|
138 |
|
| 120 |
private boolean checkCache(){
|
139 |
private boolean checkCache(){
|
| 121 |
if (supc!=null){
|
140 |
if (supc!=null){
|
| 122 |
Object cacheValue = memcachedClient.get(KEY);
|
141 |
Object cacheValue = memcachedClient.get(KEY);
|
| 123 |
if (cacheValue != null) {
|
142 |
if (cacheValue != null) {
|
| 124 |
System.out.println("cache value is not null");
|
143 |
System.out.println("cache value is not null");
|
| 125 |
HashMap<String, String> supcMap = new HashMap<String, String>();
|
144 |
HashMap<String, HashMap<String, String>> supcMap = new HashMap<String, HashMap<String, String>>();
|
| 126 |
supcMap=(HashMap<String, String>)cacheValue;
|
145 |
supcMap=(HashMap<String, HashMap<String, String>>)cacheValue;
|
| 127 |
System.out.println("supc map "+supcMap);
|
146 |
System.out.println("supc map "+supcMap);
|
| 128 |
color = supcMap.get(supc);
|
147 |
HashMap<String, String> details = supcMap.get(supc);
|
| 129 |
System.out.println("color "+color);
|
148 |
if (details ==null){
|
| - |
|
149 |
return false;
|
| - |
|
150 |
}
|
| 130 |
if (!(color==null || color.isEmpty())){
|
151 |
if (!(details.get("color")==null || details.get("color").isEmpty())){
|
| - |
|
152 |
color = details.get("color");
|
| - |
|
153 |
memory = details.get("memory");
|
| 131 |
System.out.println("Returning true");
|
154 |
System.out.println("Returning true");
|
| 132 |
return true;
|
155 |
return true;
|
| 133 |
}
|
156 |
}
|
| 134 |
}
|
157 |
}
|
| 135 |
}
|
158 |
}
|
| Line 146... |
Line 169... |
| 146 |
return url;
|
169 |
return url;
|
| 147 |
}
|
170 |
}
|
| 148 |
|
171 |
|
| 149 |
public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
|
172 |
public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
|
| 150 |
SnapdealProductPageParserController s = new SnapdealProductPageParserController();
|
173 |
SnapdealProductPageParserController s = new SnapdealProductPageParserController();
|
| 151 |
// //String url = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3QvaHRjLWRlc2lyZS01MjYtZy82ODE1MDk5MjQyNDg/c3VwYz1TREw1MjU0MDI1OTYmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MzM1NTAmYWZmX3N1Yj1TSEEzMTQzMjEwODk3MA==";
|
174 |
// //String url = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3QvaHRjLWRlc2lyZS01MjYtZy82ODE1MDk5MjQyNDg/c3VwYz1TREw1MjU0MDI1OTYmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MzM1NTAmYWZmX3N1Yj1TSEEzMTQzMjEwODk3MA==";
|
| 152 |
String url = "aHR0cDovL3d3dy5zbmFwZGVhbC5jb20vcHJvZHVjdC9zb255LXhwZXJpYS10Mi11bHRyYS04Z2IvMjE0Njc5OTEyLz9zdXBjPVNETDI4OTYwOTM5NA==";
|
175 |
String url = "aHR0cHM6Ly93d3cuc25hcGRlYWwuY29tL3Byb2R1Y3QvaXBob25lLTZzLTY0Z2IvNjczMzYyNjQyNTUwP3N1cGM9U0RMNzcwODU3MDUx";
|
| 153 |
byte[] decoded = Base64.decode(url);
|
176 |
byte[] decoded = Base64.decode(url);
|
| 154 |
System.out.println(new String(decoded));
|
177 |
System.out.println(new String(decoded));
|
| 155 |
s.setUrl(url);
|
178 |
s.setUrl(url);
|
| 156 |
System.out.println(s.getColorMessage().getMessage());
|
179 |
System.out.println(s.getColorMessage().getMessage());
|
| 157 |
//memcachedClient.set("java", "bawaaaa");
|
180 |
//memcachedClient.set("java", "bawaaaa");
|