Subversion Repositories SmartDukaan

Rev

Rev 11266 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10545 vikram.rag 1
package in.shop2020;
2
 
3
import in.shop2020.model.v1.catalog.SnapdealItemDetails;
4
import in.shop2020.model.v1.inventory.SnapdealStockAtEOD;
5
import in.shop2020.thrift.clients.CatalogClient;
6
import in.shop2020.thrift.clients.InventoryClient;
7
import inventory.Inventory;
8
import inventory.InventoryItems;
9
import java.io.BufferedReader;
10
import java.io.IOException;
11
import java.io.InputStreamReader;
12
import java.io.UnsupportedEncodingException;
13
import java.util.ArrayList;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17
import org.apache.http.HttpResponse;
18
import org.apache.http.NameValuePair;
19
import org.apache.http.client.ClientProtocolException;
20
import org.apache.http.client.entity.UrlEncodedFormEntity;
21
import org.apache.http.client.methods.HttpGet;
22
import org.apache.http.client.methods.HttpPost;
23
import org.apache.http.impl.client.DefaultHttpClient;
24
import org.apache.http.message.BasicNameValuePair;
25
import org.apache.thrift.TException;
26
import org.apache.thrift.transport.TTransportException;
27
 
28
import com.google.gson.Gson;
29
 
30
public class SnapdealInventoryAtEOD {
31
 
32
	private static DefaultHttpClient client = new DefaultHttpClient();
33
	private static Map<Long, SnapdealItemDetails> itemSnapdealMap = new HashMap<Long, SnapdealItemDetails>();;
34
	private static Map<String, SnapdealItemDetails> snapdealItemMap = new HashMap<String, SnapdealItemDetails>();
35
 
36
	static{
37
		CatalogClient catalogServiceClient = null;
38
		try {
39
			catalogServiceClient = new CatalogClient();
40
		} catch (TTransportException e1) {
41
			e1.printStackTrace();
42
		}
43
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
44
		List<SnapdealItemDetails> allSnapdealItems = null;
45
 
46
		try {
47
			allSnapdealItems = catalogClient.getAllSnapdealItems();
48
		} catch (TException e) {
49
 
50
			e.printStackTrace();
51
		}
52
		for(SnapdealItemDetails snapdealItem:allSnapdealItems){
53
			snapdealItemMap.put(snapdealItem.getSkuAtSnapdeal(),snapdealItem);
54
			itemSnapdealMap.put(snapdealItem.getItem_id(), snapdealItem);
55
 
56
		}
57
		System.out.println("++++++++++++++++++++++++++++++");
58
		System.out.println("Created Item and Snapdeal Map ");
59
		System.out.println("++++++++++++++++++++++++++++++");
60
 
61
	}
62
 
63
 
64
	private static void fetchcurrentinventoryfromsnapdeal() throws UnsupportedEncodingException, TException{
65
		int i = 1;
66
		int items=0;
67
		Gson gson = new Gson();
68
		ArrayList<Inventory> currentInventoryItemList = new ArrayList<Inventory>();
69
		HttpGet get;
70
		HttpResponse response = null;
71
		BufferedReader rd= null;
72
		System.out.println("Fetching current inventory ");
73
		while(true){
74
			Long time = System.currentTimeMillis();
75
			//System.out.println("Fetching current inventory page " +i);
76
			get = new HttpGet("http://seller.snapdeal.com/inventory/search?gridType=normal&_search=false&nd="+time+"&rows="+30+"&page="+i+"&sidx=&sord=asc");
77
			try {
78
				response = client.execute(get);
79
			} catch (ClientProtocolException e) {
80
 
81
				e.printStackTrace();
82
			} catch (IOException e) {
83
 
84
				e.printStackTrace();
85
			}
86
			try {
87
				rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
88
			} catch (IllegalStateException e) {
89
 
90
				e.printStackTrace();
91
			} catch (IOException e) {
92
 
93
				e.printStackTrace();
94
			}
95
			InventoryItems inventoryitems = (InventoryItems) gson.fromJson(rd, InventoryItems.class);
96
			if(inventoryitems.getRows().size()!=0){
97
				for(Inventory inventory : inventoryitems.getRows()){
98
					if(snapdealItemMap.containsKey(inventory.getSellerSku())){
99
						currentInventoryItemList.add(inventory);
100
					}
101
 
102
					else{
103
					}
104
					items++;
105
				}
106
			}
107
			else{
108
				System.out.println("Common items  " + items);
109
				System.out.println("Total Pages Fetched  " + i);
110
				break;
111
			}
112
			i++;
113
		}
114
		int total =0;
115
		List<SnapdealStockAtEOD> allsnapdealstock = new ArrayList<SnapdealStockAtEOD>();
116
		long time = System.currentTimeMillis();
117
		for(Inventory inventory:currentInventoryItemList){
118
			SnapdealStockAtEOD snapdealStockAtEOD = new SnapdealStockAtEOD();
119
			System.out.println("Item Id " + snapdealItemMap.get(inventory.getSellerSku()).getItem_id());
120
			snapdealStockAtEOD.setItem_id(snapdealItemMap.get(inventory.getSellerSku()).getItem_id());
121
			System.out.println("Sku at SD " + snapdealItemMap.get(inventory.getSellerSku()).getSkuAtSnapdeal());
122
			System.out.println("Inventory " + inventory.getAvailableInventory());
123
			snapdealStockAtEOD.setAvailability(inventory.getAvailableInventory());
124
			snapdealStockAtEOD.setDate(time);
125
			allsnapdealstock.add(snapdealStockAtEOD);
126
			total++; 
127
		}
128
		System.out.println("Inventory found for " + total);
129
		try{
130
			new InventoryClient().getClient().updateSnapdealStockAtEOD(allsnapdealstock);
131
		}
132
		catch(Exception e){
133
			new InventoryClient().getClient().updateSnapdealStockAtEOD(allsnapdealstock);
134
		}
135
 
136
	}
137
 
138
 
139
	public static void handleLogin() throws ClientProtocolException, IOException{
140
		HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/keymaker/login");
141
		HttpResponse response = null;
142
		try {
143
			response = client.execute(get);
144
		} catch (ClientProtocolException e) {
145
 
146
			e.printStackTrace();
147
		} catch (IOException e) {
148
 
149
			e.printStackTrace();
150
		}
151
		BufferedReader rd = null;
152
		try {
153
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
154
		} catch (IllegalStateException e1) {
155
 
156
			e1.printStackTrace();
157
		} catch (IOException e1) {
158
 
159
			e1.printStackTrace();
160
		}
161
		String line = "";
162
		StringBuffer sb = new StringBuffer();
163
		try {
164
			while ((line = rd.readLine()) != null) {
165
				sb.append(line);
166
				//System.out.println(line);
167
			}
168
		} catch (IOException e) {
169
 
170
			e.printStackTrace();
171
		}
172
		int i= sb.toString().indexOf("name=\"lt\" value=");
173
		char[] charArray = sb.toString().toCharArray();
174
		String lt = "";
175
		int j=0;
176
		for(j=i+16;j<=charArray.length;j++){
177
 
178
			if(charArray[j]==' '){
179
				break;
180
			}
181
		}
182
		lt = sb.substring(i+17,j-1);
183
		System.out.println("LT VALUE " + lt);
184
		i= sb.toString().indexOf("name=\"execution\" value=");
185
		charArray = sb.toString().toCharArray();
186
		String ex = "";
187
		j=0;
188
		for(j=i+24;j<=charArray.length;j++){
189
			if(charArray[j]==' '){
190
				break;
191
			}
192
		}
193
		ex = sb.substring(i+24,j-1);
194
		System.out.println("EXECUTION VALUE " + ex);
195
		HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
196
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
197
		//nameValuePairs.add(new BasicNameValuePair("username",
198
		//"khushal.bhatia@saholic.com"));
199
		nameValuePairs.add(new BasicNameValuePair("username",
200
		"saholic-snapdeal@saholic.com"));
201
		//nameValuePairs.add(new BasicNameValuePair("password",
202
		//"sonline"));
203
		nameValuePairs.add(new BasicNameValuePair("password",
15223 manish.sha 204
		"saholic15"));
10545 vikram.rag 205
		nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));
206
		nameValuePairs.add(new BasicNameValuePair("execution",ex));
207
		nameValuePairs.add(new BasicNameValuePair("lt",lt));
208
		nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));
209
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
210
		response = client.execute(post);
211
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
212
		line = "";
213
		while ((line = rd.readLine()) != null) {
214
			System.out.println(line);
215
		}
216
		get = new HttpGet("http://seller.snapdeal.com/inventory/");
217
		response = client.execute(get);
218
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
219
		while ((line = rd.readLine()) != null) {
220
			//System.out.println(line);
221
		}
222
	}
223
 
224
	public static void main(String... args) throws ClientProtocolException, IOException, TException{
225
		handleLogin();
226
		fetchcurrentinventoryfromsnapdeal();
227
	}
228
 
229
 
230
}