My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package amzn.partner.core;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.client.CookieStore;
import org.apache.http.client.ResponseHandler;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import amzn.partner.AmazonPartnerNetActivity;
import android.os.AsyncTask;
import android.util.Log;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.client.protocol.ClientContext;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;


public class amznclient extends AsyncTask<String,Integer,String> {
private String username = "";
private String password = "";
private AmazonPartnerNetActivity parentActivity;

private CookieStore cookieStore = new BasicCookieStore();
private HttpContext localContext = new BasicHttpContext();
private DefaultHttpClient httpClient = new DefaultHttpClient();

private Calendar ReportFrom = Calendar.getInstance();
private Calendar ReportTo = Calendar.getInstance();

public amznclient(AmazonPartnerNetActivity activity){
this.parentActivity = activity;
this.localContext.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
this.httpClient.setCookieStore(this.cookieStore);
this.ReportFrom.set(Calendar.DAY_OF_MONTH, 1);
this.ReportTo.set(this.ReportTo.get(Calendar.YEAR),
this.ReportTo.get(Calendar.MONTH),
this.ReportTo.getActualMaximum(Calendar.DAY_OF_MONTH));
}

public void setReportingTimeframe(Calendar reportFrom, Calendar reportTo){
this.ReportFrom = reportFrom;
this.ReportTo = reportTo;
}

public ArrayList<Calendar> getReportingTimeframe(){
ArrayList<Calendar> result = new ArrayList<Calendar>();
result.add(this.ReportFrom);
result.add(this.ReportTo);
return result;
}

/* returns the username */
public String getUsername(){
return this.username;
}

/* set the user name for this account */
public void setUsername(String newUsername){
this.username = newUsername;
}

/* set the password for this account */
public void setPassword(String newPassword){
this.password = newPassword;
}

private String wget(String url, String postVars){
String data = "";
HttpResponse response = null;
Header locationHeader = null;

String userAgent = "Mozilla/5.0 (X11; Linux x86_64;"
+ " rv:7.0) Gecko/20100101 Firefox/7.0";

try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();

if(postVars.length() > 0){
HttpPost postMethod = new HttpPost(url);
postMethod.setHeader("User-Agent", userAgent);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

String[] postVarList = postVars.split("&");
for(int p=0; p<postVarList.length; p++){
String[] postVarItem = postVarList[p].split("=");
String varName = postVarItem[0];
String varValue = "";
if(postVarItem.length == 2){
varValue = postVarItem[1];
}
nameValuePairs.add(new BasicNameValuePair(varName, varValue));
}
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
this.localContext.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
this.httpClient.setCookieStore(this.cookieStore);
response = this.httpClient.execute(postMethod,this.localContext);
locationHeader = postMethod.getFirstHeader("Location");
}else{
HttpGet getMethod = new HttpGet(url);
getMethod.setHeader("User-Agent", userAgent);
this.localContext.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
this.httpClient.setCookieStore(this.cookieStore);
response = this.httpClient.execute(getMethod,this.localContext);
locationHeader = getMethod.getFirstHeader("Location");
}

int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
data = responseHandler.handleResponse(response);
}if (statusCode == 302) {
data = this.wget(locationHeader.getValue(), "");
}

} catch (Exception e) {
data = "";
}

return data;
}

/* fetches the report data from amazon */
protected String fetch(){
String result = "";
try{
String loginPageHtml = this.wget("https://partnernet.amazon.de/"
+ "gp/associates/login/login.html", "");

// parse the session id out of the raw html
// and process the actual login
if(loginPageHtml.indexOf("name=\"sessionId\"") > 0){
String sessionId = loginPageHtml.substring(loginPageHtml.indexOf
("name=\"sessionId\"")).substring(24);
sessionId = sessionId.substring(0,sessionId.indexOf("\""));
String loggedInPageHtml = "";

String urlParameters = "__mk_de_DE=%C3%85M%C3%85Z%C3%95%C3%91&action=sign-in&mode=1"
+ "&path=/gp/associates/login/login.html"
+ "&useRedirectOnSuccess=0&query=&pageAction=/gp/associates/login/login.html"
+ "&disableCorpSignUp=&protocol=https&referer=flex&sessionId="
+ sessionId + "&email=" + this.username
+ "&password=" + this.password;

// process the actual login
loggedInPageHtml = this.wget("https://partnernet.amazon.de/"
+ "gp/flex/sign-in/select.html", urlParameters);

// on success it redirects
if(loggedInPageHtml.indexOf("Angemeldet als")>0){
String reportUrl = "https://partnernet.amazon.de/gp/associates/network/reports/report.html"
+ "?__mk_de_DE=%C3%85M%C3%85Z%C3%95%C3%91&tag=&reportType=earningsReport"
+ "&program=all&preSelectedPeriod=yesterday&periodType=exact"
+ "&startDay=" + Integer.toString(this.ReportFrom.get(Calendar.DAY_OF_MONTH))
+ "&startMonth=" + Integer.toString(this.ReportFrom.get(Calendar.MONTH))
+ "&startYear=" + Integer.toString(this.ReportFrom.get(Calendar.YEAR))
+ "&endDay=" + Integer.toString(this.ReportTo.get(Calendar.DAY_OF_MONTH))
+ "&endMonth=" + Integer.toString(this.ReportTo.get(Calendar.MONTH))
+ "&endYear=" + Integer.toString(this.ReportTo.get(Calendar.YEAR))
+ "&submit.download_XML.x=102&submit.download_XML.y=4";
String reportXml = this.wget(reportUrl, "");
if(reportXml.indexOf("<AMZNShipmentTotals>") > 0){
result = reportXml;
}else{
result = "DATA_DOWNLOAD_FAILED";
}
}else{
result = "LOGIN_FAILED";
}
}else{
result = "LOGIN_FAILED";
}
}catch(Exception ex){
Log.e("APN", ex.getMessage());
}

return result;
}

@Override
protected String doInBackground(String... params) {
String result;
try {
result = this.fetch();
} catch (Exception e) {
Log.e("APN", e.getMessage());
result = null;
}

return result;
}

@Override
protected void onPostExecute(String result) {
this.parentActivity.updateDataReady(result);
}

}

Change log

4218666fdc61 by Jan Kammerath <jankammerath> on Oct 28, 2011   Diff
Almost finished with the app
Go to: 
Project members, sign in to write a code review

Older revisions

2f03fd4eb73b by Jan Kammerath <jankammerath> on Oct 28, 2011   Diff
Added the date pickers and lots of
stuff causing great progress here
d3ff0a221a7f by Jan Kammerath <jankammerath> on Oct 27, 2011   Diff
Finally got the cookie ident working.
fb5d00690dfc by Jan Kammerath <jankammerath> on Oct 26, 2011   Diff
Added the stuff to the httpclient but
cookies dont work yet
All revisions of this file

File info

Size: 7482 bytes, 208 lines
Powered by Google Project Hosting