Subversion Repositories SmartDukaan

Rev

Rev 18169 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.dtrapi.controllers;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

import org.apache.axis.encoding.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.whalin.MemCached.MemCachedClient;
import in.shop2020.dtrapi.services.MemCache;
import in.shop2020.dtrapi.services.UserMessagePojo;

public class SnapdealProductPageParserController extends BaseController{

        private static Logger log = Logger.getLogger(Class.class);
        private static final long serialVersionUID = 1L;

        private String url;
        private String supc = "";
        private String productUrl = "";
        private String color = "";
        private String memory = "";
        private static final String INDEX = "index";
        private static final String KEY = "snapdealSupcToColorMapping";
        private static MemCache memCache = new MemCache();
        private static MemCachedClient memcachedClient = memCache.getClient();



        public UserMessagePojo getColorMessage() throws IOException, JSONException, URISyntaxException{
                try{
                        parseUrl();
                        if (!checkCache()){
                                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")
                                .get();
                                doc.outputSettings().charset("UTF-8");
                                //System.out.println(doc);
                                JSONArray jsonArray = new JSONArray(doc.getElementById("attributesJson").text());
                                for (int element=0; element<jsonArray.length();element++){
                                        if (jsonArray.getJSONObject(element).getString("subAttributeType").equalsIgnoreCase("memory")){
                                                try{
                                                        JSONArray subAttributeArray = jsonArray.getJSONObject(element).getJSONArray("subAttributes");
                                                        for (int innerElement=0; innerElement< subAttributeArray.length(); innerElement++){
                                                                if (supc.equalsIgnoreCase(subAttributeArray.getJSONObject(innerElement).getString("supc"))){
                                                                        System.out.println(subAttributeArray.getJSONObject(element).getString("value"));
                                                                        memory = subAttributeArray.getJSONObject(element).getString("value");
                                                                        color = jsonArray.getJSONObject(element).getString("value");
                                                                        break;
                                                                }
                                                        }
                                                }
                                                catch(Exception e1){
                                                        e1.printStackTrace();
                                                }
                                        }
                                        if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
                                                color = jsonArray.getJSONObject(element).getString("value");
                                        }
                                        if (color!=null){
                                                if (!color.isEmpty()){
                                                        populateCache(supc, color);
                                                        break;
                                                }
                                        }
                                }
                        }
                }
                catch (Exception e){
                        e.printStackTrace();
                }
                return getMsg();
        }

        private UserMessagePojo getMsg(){
                UserMessagePojo ump = new UserMessagePojo();
                //System.out.println("color is "+color);
                if (color==null || color.isEmpty()){
                        ump.setResult(false);
                        ump.setMessage("");
                }
                else{
                        ump.setResult(true);
                        ump.setMessage("Please select "+color.trim()+" color to get this price.");
                        if (memory !=null && !memory.isEmpty()){
                                ump.setMessage("Please select "+color.trim()+" " +memory.trim()+" color to get this price.");
                        }
                }
                return ump;

        }

        private String getJavaScriptCode(){
                String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+";ele.onchange();}catch(error){Android.onError(error.message);}";
                return jsCode;
        }

        private void parseUrl() throws URISyntaxException, MalformedURLException{
                List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
                for (NameValuePair param : params){
                        if (param.getName().equalsIgnoreCase("supc")){
                                supc = param.getValue();
                        }
                }
                productUrl = new URI(url).getHost()+new URI(url).getPath();
                if (!productUrl.startsWith("http")){
                        productUrl = "http://"+productUrl;
                }
//              URL url = new URL(productUrl);
//              String protocol = url.getProtocol();
//              if (protocol ==null || protocol == "http"){
//                      productUrl.replace("http", "https");
//              }
        }

        private void populateCache(String supc, String color){
                Object cacheValue = memcachedClient.get(KEY);
                HashMap<String, HashMap<String, String>> supcMap = new HashMap<String,  HashMap<String, String>>();
                if (cacheValue != null) {
                        supcMap=(HashMap<String,  HashMap<String, String>>)cacheValue;
                }
                HashMap<String, String> productDetailMap = new HashMap<String, String>();
                productDetailMap.put("color", color);
                productDetailMap.put("memory", memory);
                supcMap.put(supc, productDetailMap);
                memcachedClient.set(KEY, supcMap);
        }

        private boolean checkCache(){
                if (supc!=null){
                        Object cacheValue = memcachedClient.get(KEY);
                        if (cacheValue != null) {
                                System.out.println("cache value is not null");
                                HashMap<String,  HashMap<String, String>> supcMap = new HashMap<String,  HashMap<String, String>>();
                                supcMap=(HashMap<String,  HashMap<String, String>>)cacheValue;
                                System.out.println("supc map "+supcMap);
                                HashMap<String, String> details = supcMap.get(supc);
                                if (details ==null){
                                        return false;
                                }
                                if (!(details.get("color")==null || details.get("color").isEmpty())){
                                        color = details.get("color");
                                        memory = details.get("memory");
                                        System.out.println("Returning true");
                                        return true;
                                }
                        }
                }
                System.out.println("Returning false");
                return false;
        }

        public void setUrl(String url) {
                byte[] decoded = Base64.decode(url);
                this.url = new String(decoded);
        }

        public String getUrl() {
                return url;
        }

        public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
                SnapdealProductPageParserController s = new SnapdealProductPageParserController();
                //              //String url = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3QvaHRjLWRlc2lyZS01MjYtZy82ODE1MDk5MjQyNDg/c3VwYz1TREw1MjU0MDI1OTYmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MzM1NTAmYWZmX3N1Yj1TSEEzMTQzMjEwODk3MA==";
                String url = "aHR0cHM6Ly93d3cuc25hcGRlYWwuY29tL3Byb2R1Y3QvaXBob25lLTZzLTY0Z2IvNjczMzYyNjQyNTUwP3N1cGM9U0RMNzcwODU3MDUx";
                byte[] decoded = Base64.decode(url);
                System.out.println(new String(decoded));
                s.setUrl(url);
                System.out.println(s.getColorMessage().getMessage());
                //memcachedClient.set("java", "bawaaaa");
                //System.out.println(memcachedClient.get("hi"));
        }

}