Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.dtrapi.controllers;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

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;

public class SnapdealProductPageParser 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 final String INDEX = "index";

    public String getJavaScriptToEmbedd() throws IOException, JSONException, URISyntaxException{
        try{
            parseUrl();
            System.out.println(productUrl);
            Document doc = Jsoup.connect(productUrl).get();
            doc.outputSettings().charset("UTF-8");
            JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
            for (int element=0; element<jsonArray.length();element++){
                if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
                    color = jsonArray.getJSONObject(element).getString("value");
                }
            }
            setResultJson(getJavaScriptCode());
        }
        catch (Exception e){
            log.error("Error while getting product details " +e);
            setResultJson(getJavaScriptCode());
        }
        return INDEX;
        
    }
    
    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{
        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;
        }
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl() {
        return url;
    }

    public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
        SnapdealProductPageParser s = new SnapdealProductPageParser();
        s.setUrl("http://m.snapdeal.com/product/htc-desire-620-g/2140463474?supc=SDL872629472&utm_source=aff_prog&utm_campaign=afts&offer_id=17&aff_id=33550&aff_sub=SHA31430302193");

    }



}