Subversion Repositories SmartDukaan

Rev

Rev 13198 | Rev 13636 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.services;

import in.shop2020.model.v1.order.OrderSource;
import in.shop2020.serving.model.Order;
import in.shop2020.serving.model.OrderItems;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.utils.GmailUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.mail.MessagingException;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;

public class FlipkartOrderStatusReconciliation{
        static String emailFromAddress;
        static String password;
        static GmailUtils mailer;
        static String sendTo[];

        static {
                emailFromAddress = "build-staging@shop2020.in";
                password = "shop2020";
                mailer = new GmailUtils();
                sendTo = new String[] { "kshitij.sood@shop2020.in", "manish.sharma@shop2020.in", "amit.gupta@shop2020.in" };
        }

        public static void main(String[] args) throws MessagingException {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost("https://seller.flipkart.com/login");
                BufferedReader rd= null;

                List<List<String>> deliveredOrderDataList = new ArrayList<List<String>>();
                SimpleDateFormat gotFlipkartDateFormat = new SimpleDateFormat("MMM dd, yyyy");
                //SimpleDateFormat createDateFormatter = new SimpleDateFormat("MMM dd, yyyy hh:mm aaa");
                SimpleDateFormat ourDBDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                long minCreationDate = 0l;
                try {
                        /*minCreationDate = 1386432631000l;new TransactionClient().getClient().getMinCreatedTimeStampUndeliveredOrdersForSource(OrderSource.FLIPKART.getValue());*/
                        Calendar cal = Calendar.getInstance();
                        cal.add(Calendar.DAY_OF_MONTH, -30);
                        minCreationDate = cal.getTimeInMillis();
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("authName",
                        "flipkart"));
                        nameValuePairs.add(new BasicNameValuePair("username",
                        "flipkart-support@saholic.com"));
                        nameValuePairs.add(new BasicNameValuePair("password",
                        "bestmobiledeals2010"));
                        post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
                        post.addHeader("Cookie","__gads=ID=dfe0374cf04d1576:T=1381304511:S=ALNI_Ma2TpDrlF9_amkoqp3MOYJBaFAOUg; km_lv=x; S=d1t173VTuVxLMgd3in7R3mbHGNz0STqFkHgnidNrhKqBdz8wEKt%2BO9d%2B%2FRDHtf6fmq2ugaE7nZHV7zYSxOLcWBzq31A%3D%3D; __isReg=true; _ga=GA1.2.1156779613.1390806906; km_ai=m2z93iskuj81qiid; km_ni=m2z93iskuj81qiid; km_uq=; pincode=201301; __sonar=6631097455123118581; s_cc=true; s_sq=%5B%5BB%5D%5D; __utma=19769839.1733153395.1408690721.1416647806.1417179072.33; __utmc=19769839; __utmz=19769839.1415795608.26.5.utmcsr=savemoneyindia.com|utmccn=(referral)|utmcmd=referral|utmcct=/; SN=2.VIB1DBEC9C6CD94162ACB61060135212D1.SI47EBA2DE47044D0AA7C05F24AD4F42EB.VS141717906546900755654.1417179082; VID=2.VIB1DBEC9C6CD94162ACB61060135212D1.1417179082.VS141717906546900755654; NSID=2.SI47EBA2DE47044D0AA7C05F24AD4F42EB.1417179082.VIB1DBEC9C6CD94162ACB61060135212D1; T=TI138130450866706710769873257251497444628820234222703981199700765991; s_ppv=53; __CG=u%3A6208250296506778000%2Cs%3A1505183865%2Ct%3A1417179187815%2Cc%3A2%2Ck%3Awww.flipkart.com/30/77/1452%2Cf%3A0%2Ci%3A1; connect.sid=s%3AsMt6GmxeIzE45d0nKtsgQvNm.sea6W5znLeuulBNKyJu1%2FmvpO5xROchbo49GJMoHoFs; __utmt=1; __utma=143439159.1156779613.1390806906.1415709197.1417274947.45; __utmb=143439159.1.10.1417274947; __utmc=143439159; __utmz=143439159.1390806906.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)");
                        post.addHeader("User-agent", "Mozilla/4.0");
                        post.addHeader("Referer", "seller.flipkart.com");
                        HttpResponse response = client.execute(post);
                        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                        String line = "";
                        Gson gson;
                        List<Order> orders = new ArrayList<Order>();
                        OrderItems new_orders;
                        HttpGet get_new;

                        long time;
                        int i=1;
                        while ((line = rd.readLine()) != null) {
                                System.out.println(line);
                        }
                        time = System.currentTimeMillis();

                        outer:while(true){
                                get_new = new HttpGet("https://seller.flipkart.com/dashboard/som/shipped_order_items?status=delivered&page="+i+"&page_size=50&_="+time);
                                get_new.addHeader("Cookie","__gads=ID=dfe0374cf04d1576:T=1381304511:S=ALNI_Ma2TpDrlF9_amkoqp3MOYJBaFAOUg; km_lv=x; __isReg=true; __sonar=6631097455123118581; T=TI138130450866706710769873257251497444628820234222703981199700765991; __CG=u%3A6208250296506778000%2Cs%3A2098853214%2Ct%3A1419058011787%2Cc%3A1%2Ck%3Awww.flipkart.com/24/24/76%2Cf%3A0%2Ci%3A1; S=d1t12N7BXdo%2FbcBeFmoQgBpmZF9muLFEXcitcZroHtblNXfGjBz3%2BCu%2FEpAonWZnl5ncyeqnHCuxIDEE%2BfhGDvldTTQ%3D%3D; s_ch_list=%5B%5B'Affiliates'%2C'1421923225260'%5D%5D; __utma=19769839.1733153395.1408690721.1419941582.1421923225.39; __utmz=19769839.1421923225.39.6.utmcsr=adcharge.co.in|utmccn=(referral)|utmcmd=referral|utmcct=/ch300-3.htm; pincode=110001; SN=2.VIB1DBEC9C6CD94162ACB61060135212D1.SI47EBA2DE47044D0AA7C05F24AD4F42EB.VS142192322318882989513.1421925042; VID=2.VIB1DBEC9C6CD94162ACB61060135212D1.1421925042.VS142192322318882989513; NSID=2.SI47EBA2DE47044D0AA7C05F24AD4F42EB.1421925042.VIB1DBEC9C6CD94162ACB61060135212D1; connect.sid=s%3Avt4uSpKD48RKLMH%2BxqbFHueT.Z%2BwZTl4xr8jjZlNb2vCceV7oveBhyekGxVF45lhXXcQ; is_login=true; sellerId=m2z93iskuj81qiid; __utmt=1; __utma=143439159.1156779613.1390806906.1421925688.1422335522.64; __utmb=143439159.8.10.1422335522; __utmc=143439159; __utmz=143439159.1390806906.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _ga=GA1.2.1156779613.1390806906; _gat=1; km_ai=m2z93iskuj81qiid; km_ni=m2z93iskuj81qiid; km_vs=1; km_uq=");
                                //get_new.addHeader("Cookie","__gads=ID=dfe0374cf04d1576:T=1381304511:S=ALNI_Ma2TpDrlF9_amkoqp3MOYJBaFAOUg; km_lv=x; S=d1t173VTuVxLMgd3in7R3mbHGNz0STqFkHgnidNrhKqBdz8wEKt%2BO9d%2B%2FRDHtf6fmq2ugaE7nZHV7zYSxOLcWBzq31A%3D%3D; __isReg=true; pincode=201301; __sonar=6631097455123118581; s_cc=true; s_sq=%5B%5BB%5D%5D; __utma=19769839.1733153395.1408690721.1416647806.1417179072.33; __utmc=19769839; __utmz=19769839.1415795608.26.5.utmcsr=savemoneyindia.com|utmccn=(referral)|utmcmd=referral|utmcct=/; SN=2.VIB1DBEC9C6CD94162ACB61060135212D1.SI47EBA2DE47044D0AA7C05F24AD4F42EB.VS141717906546900755654.1417179082; VID=2.VIB1DBEC9C6CD94162ACB61060135212D1.1417179082.VS141717906546900755654; NSID=2.SI47EBA2DE47044D0AA7C05F24AD4F42EB.1417179082.VIB1DBEC9C6CD94162ACB61060135212D1; T=TI138130450866706710769873257251497444628820234222703981199700765991; s_ppv=53; __CG=u%3A6208250296506778000%2Cs%3A1505183865%2Ct%3A1417179187815%2Cc%3A2%2Ck%3Awww.flipkart.com/30/77/1452%2Cf%3A0%2Ci%3A1; is_login=true; sellerId=m2z93iskuj81qiid; __utma=143439159.1156779613.1390806906.1417274947.1417519426.46; __utmb=143439159.4.10.1417519426; __utmc=143439159; __utmz=143439159.1390806906.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _ga=GA1.2.1156779613.1390806906; km_ai=m2z93iskuj81qiid; km_ni=m2z93iskuj81qiid; km_vs=1; km_uq=");
                                response = client.execute(get_new);
                                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                                line = "";
                                gson = new Gson();

                                JSONObject jsonDataObj = null;
                                line = rd.readLine();

                                JSONArray jsonObjArr = null;

                                if(line == null || line.isEmpty()){
                                        if(i==120)
                                                break;
                                        i++;
                                        continue;
                                }

                                jsonDataObj = new JSONObject(line);
                                jsonObjArr = new JSONArray(jsonDataObj.get("order_items").toString());

                                if(jsonObjArr==null || jsonObjArr.length()==0){
                                        break;
                                }

                                if(jsonObjArr!=null && jsonObjArr.length()>0){
                                        for(int k=0; k< jsonObjArr.length(); k++){
                                                JSONObject jsonObj = null;
                                                jsonObj = jsonObjArr.getJSONObject(k);

                                                if(jsonObj!=null){
                                                        /*System.out.println("jsonObj..... "+ jsonObj);*/
                                                        if(minCreationDate > 0l && gotFlipkartDateFormat.parse(gotFlipkartDateFormat.format(minCreationDate)).getTime() <= gotFlipkartDateFormat.parse(jsonObj.get("created_date").toString()).getTime()){
                                                                List<String> delivered_dataList = new ArrayList<String>();
                                                                String externalId = jsonObj.get("external_id").toString();
                                                                String orderItemId = jsonObj.get("order_item_id").toString();
                                                                String modifiedDate = jsonObj.get("modified_date").toString();
                                                                delivered_dataList.add(externalId);
                                                                delivered_dataList.add(orderItemId);

                                                                delivered_dataList.add(ourDBDateFormat.format(gotFlipkartDateFormat.parse(modifiedDate).getTime()));
                                                                System.out.println("Flipkart Order Id... "+externalId + " ... Flipkart Sub Order Code... "+ orderItemId +" ... Delivery Date ... "+ ourDBDateFormat.format(gotFlipkartDateFormat.parse(modifiedDate).getTime()) + "....Created Date..... "+ jsonObj.get("created_date").toString());
                                                                deliveredOrderDataList.add(delivered_dataList);
                                                        }
                                                        else{
                                                                break outer;
                                                        }
                                                }
                                        }
                                }

                                i++;
                        }

                        System.out.println("deliveredOrderDataList size... "+deliveredOrderDataList.size());
                        if(deliveredOrderDataList!=null && deliveredOrderDataList.size()>0){
                                new TransactionClient().getClient().updateFlipkartOrdersStatus(deliveredOrderDataList);
                        }

                } catch(Exception e){
                        e.printStackTrace();
                        mailer.sendSSLMessage(sendTo, "Error in Flipkart Order Status Recon", emailFromAddress, password, "<html><body><p>Please check log</p></body></html>");
                }
        }
}