forked from maxklein/AppSalesGraph
/
appdailysales.py
378 lines (307 loc) · 14.6 KB
/
appdailysales.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/python
#
# appdailysales.py
#
# iTune Connect Daily Sales Reports Downloader
# Copyright 2008-2009 Kirby Turner
#
# Version 1.9
#
# Latest version and additional information available at:
# http://appdailysales.googlecode.com/
#
#
# This script will download yesterday's daily sales report from
# the iTunes Connect web site. The downloaded file is stored
# in the same directory containing the script file. Note: if
# the download file already exists then it will be overwritten.
#
# The iTunes Connect web site has dynamic urls and form field
# names. In other words, these values change from session to
# session. So to get to the download file we must navigate
# the site and webscrape the pages. Joy, joy.
#
#
# Contributors:
# Leon Ho
# Rogue Amoeba Software, LLC
# Keith Simmons
# Andrew de los Reyes
# Maarten Billemont
# Max Klein
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# -- Change the following to match your credentials --
# -- or use the command line options. --
appleId = 'Your Apple Id'
password = 'Your Password'
outputDirectory = ''
unzipFile = False
verbose = False
daysToDownload = 1
dateToDownload = None
# ----------------------------------------------------
import urllib
import urllib2
import cookielib
import datetime
import re
import getopt
import sys
import os
import gzip
import StringIO
import traceback
import wx
from result_event import ResultEvent
shutDownSalesDownload = False
try:
import BeautifulSoup
except ImportError:
BeautifulSoup = None
class ITCException(Exception):
def __init__(self,value):
self.value = value
def __str__(self):
return repr(self.value);
# The class ReportOptions defines a structure for passing
# report options to the download routine. The expected
# data attributes are:
# appleId
# password
# outputDirectory
# unzipFile
# verbose
# daysToDownload
# Note that the class attributes will default to the global
# variable value equivalent.
class ReportOptions:
def __getattr__(self, attrname):
if attrname == 'appleId':
return appleId
elif attrname == 'password':
return password
elif attrname == 'outputDirectory':
return outputDirectory
elif attrname == 'unzipFile':
return unzipFile
elif attrname == 'verbose':
return verbose
elif attrname == 'daysToDownload':
return daysToDownload
elif attrname == 'dateToDownload':
return dateToDownload
else:
raise AttributeError, attrname
# There is an issue with Python 2.5 where it assumes the 'version'
# cookie value is always interger. However, itunesconnect.apple.com
# returns this value as a string, i.e., "1" instead of 1. Because
# of this we need a workaround that "fixes" the version field.
#
# More information at: http://bugs.python.org/issue3924
class MyCookieJar(cookielib.CookieJar):
def _cookie_from_cookie_tuple(self, tup, request):
name, value, standard, rest = tup
version = standard.get('version', None)
if version is not None:
version = version.replace('"', '')
standard["version"] = version
return cookielib.CookieJar._cookie_from_cookie_tuple(self, tup, request)
def showCookies(cj):
for index, cookie in enumerate(cj):
print index, ' : ', cookie
def readHtml(opener, url, data=None):
request = urllib2.Request(url, data)
urlHandle = opener.open(request)
html = urlHandle.read()
return html
def downloadFile(options, notify_window, days_to_download):
#if options.verbose == True:
global shutDownSalesDownload
wx.PostEvent(notify_window, ResultEvent("Connecting..."))
# print '-- begin script --'
urlBase = 'https://itts.apple.com%s'
cj = MyCookieJar();
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
if shutDownSalesDownload == True: return
# Go to the iTunes Connect website and retrieve the
# form action for logging into the site.
urlWebsite = urlBase % '/cgi-bin/WebObjects/Piano.woa'
html = readHtml(opener, urlWebsite)
match = re.search('" action="(.*)"', html)
urlActionLogin = urlBase % match.group(1)
if shutDownSalesDownload == True: return None, None
wx.PostEvent(notify_window, ResultEvent("Connected! Logging in..."))
# Login to iTunes Connect web site and go to the sales
# report page, get the form action url and form fields.
# Note the sales report page will actually load a blank
# page that redirects to the static URL. Best guess here
# is that the server is setting some session variables
# or something.
webFormLoginData = urllib.urlencode({'theAccountName':options.appleId, 'theAccountPW':options.password, '1.Continue.x':'0', '1.Continue.y':'0'})
html = readHtml(opener, urlActionLogin, webFormLoginData)
html_lc = html.lower()
if html_lc.find("piano-sign in") > 0:
wx.PostEvent(notify_window, ResultEvent("SalesDownloadError: Could not login"))
return [], True
if html_lc.find("checkvendoridnumber") > 0:
# Oh, oh, we have a multiple vendor login
soup = BeautifulSoup.BeautifulSoup( html )
# Let's get all vendor IDs
selectField = soup.find( 'select', attrs={'id': 'selectName'} )
html_options = selectField.findAll('option')
for option in html_options:
val = option['value']
if val == "0": continue
# Get the url to post to
form = soup.find( 'form', attrs={'name': 'superPage' } )
urlSelectVendor = urlBase % form['action']
wosid1 = soup.find( 'input', attrs={'name': 'wosid'} )['value']
# countryName = soup.find( 'input', attrs={'id': 'hiddenCountryName'} )['value']
# This is fragile. It should be parsed from the html
vendorSelectData = urllib.urlencode({'vndrid': val, 'wosid':wosid1, '9.6.0':val, '9.18':"", 'SubmitBtn':'Submit'})
html = readHtml(opener, urlSelectVendor, vendorSelectData)
if html.find("selDateType") > 0:
# Looks like we found the page
html_lc = html.lower()
break
break # Let's just do first vendor for now
if html.find("selDateType") <= 0:
# Page was not opened
wx.PostEvent(notify_window, ResultEvent("SalesDownloadError: Multiple Vendors not supported"))
return [], True
if shutDownSalesDownload == True: return None, None
# Get the form field names needed to download the report.
successfully_parsed = False
if BeautifulSoup:
# if options.verbose == True:
wx.PostEvent(notify_window, ResultEvent("Logged in! Accessing sales data"))
# print 'using BeautifulSoap for HTML parsing'
try:
soup = BeautifulSoup.BeautifulSoup( html )
# print html
form = soup.find( 'form', attrs={'name': 'frmVendorPage' } )
try:
urlDownload = urlBase % form['action']
except TypeError:
if html.find("Session Time Out") != -1:
wx.PostEvent(notify_window, ResultEvent("Session timeout"))
else:
wx.PostEvent(notify_window, ResultEvent("Invalid Data Returned. Try again later."))
return
fieldNameReportType = soup.find( 'select', attrs={'id': 'selReportType'} )['name']
fieldNameReportPeriod = soup.find( 'select', attrs={'id': 'selDateType'} )['name']
fieldNameDayOrWeekSelection = soup.find( 'input', attrs={'name': 'hiddenDayOrWeekSelection'} )['name'] #This is kinda redundant
fieldNameSubmitTypeName = soup.find( 'input', attrs={'name': 'hiddenSubmitTypeName'} )['name'] #This is kinda redundant, too
successfully_parsed = True
except:
pass
if successfully_parsed == False:
match = re.findall('name="frmVendorPage" action="(.*)"', html)
urlDownload = urlBase % match[0]
match = re.findall('name="(.*?)"', html)
fieldNameReportType = match[4] # selReportType
fieldNameReportPeriod = match[5] # selDateType
fieldNameDayOrWeekSelection = match[8] # hiddenDayOrWeekSelection
fieldNameSubmitTypeName = match[9] # hiddenSubmitTypeName
wx.PostEvent(notify_window, ResultEvent("Requesting daily sales..."))
if shutDownSalesDownload == True: return None, None
# Ah...more fun. We need to post the page with the form
# fields collected so far. This will give us the remaining
# form fields needed to get the download file.
webFormSalesReportData = urllib.urlencode({fieldNameReportType:'Summary', fieldNameReportPeriod:'Daily', fieldNameDayOrWeekSelection:'Daily', fieldNameSubmitTypeName:'ShowDropDown'})
html = readHtml(opener, urlDownload, webFormSalesReportData)
if shutDownSalesDownload == True: return None, None
if BeautifulSoup:
soup = BeautifulSoup.BeautifulSoup( html )
form = soup.find( 'form', attrs={'name': 'frmVendorPage' } )
try:
urlDownload = urlBase % form['action']
except TypeError:
wx.PostEvent(notify_window, ResultEvent("Invalid Data Returned. Try again later."))
return
select = soup.find( 'select', attrs={'id': 'dayorweekdropdown'} )
fieldNameDayOrWeekDropdown = select['name']
else:
match = re.findall('name="frmVendorPage" action="(.*)"', html)
urlDownload = urlBase % match[0]
match = re.findall('name="(.*?)"', html)
fieldNameDayOrWeekDropdown = match[6]
# Set the list of report dates.
reportDates = []
if not days_to_download == None:
for a_day in days_to_download:
date = '%02i/%02i/%i' % (a_day.month, a_day.day, a_day.year)
reportDates.append( date )
else:
if options.dateToDownload == None:
for i in range(int(options.daysToDownload)):
today = datetime.date.today() - datetime.timedelta(i + 1)
date = '%02i/%02i/%i' % (today.month, today.day, today.year)
reportDates.append( date )
else:
reportDates = [options.dateToDownload]
if options.verbose == True:
wx.PostEvent(notify_window, ResultEvent("Retrieving for dates:" + reportDates.__str__()))
# print 'reportDates: ', reportDates
unavailableCount = 0
filenames = []
for downloadReportDate in reportDates:
wx.PostEvent(notify_window, ResultEvent("Requesting: " + downloadReportDate))
if shutDownSalesDownload == True: return None, None
# And finally...we're ready to download yesterday's sales report.
webFormSalesReportData = urllib.urlencode({fieldNameReportType:'Summary', fieldNameReportPeriod:'Daily', fieldNameDayOrWeekDropdown:downloadReportDate, 'download':'Download', fieldNameDayOrWeekSelection:downloadReportDate, fieldNameSubmitTypeName:'Download'})
urlHandle = opener.open(urlDownload, webFormSalesReportData)
try:
if shutDownSalesDownload == True: return None, None
filename = urlHandle.info().getheader('content-disposition').split('=')[1]
# For some reason, there are geese feet on the filename
filename = filename.replace("'", "")
filename = filename.replace("\"", "")
# filesize = urlHandle.info().getheader('size')
# wx.PostEvent(notify_window, ResultEvent("Getting Sales: " + downloadReportDate + " (" + filesize + ")"))
filebuffer = urlHandle.read()
urlHandle.close()
if options.unzipFile == True:
if options.verbose == True:
wx.PostEvent(notify_window, ResultEvent("Unzipping:" + filename))
# print 'unzipping archive file: ', filename
#Use GzipFile to de-gzip the data
ioBuffer = StringIO.StringIO( filebuffer )
gzipIO = gzip.GzipFile( 'rb', fileobj=ioBuffer )
filebuffer = gzipIO.read()
filename = os.path.join(options.outputDirectory, filename)
if options.unzipFile == True and filename[-3:] == '.gz': #Chop off .gz extension if not needed
filename = os.path.splitext( filename )[0]
if options.verbose == True:
print 'saving download file:', filename
downloadFile = open(filename, 'w')
downloadFile.write(filebuffer)
downloadFile.close()
filenames.append( filename )
except AttributeError:
wx.PostEvent(notify_window, ResultEvent('%s report is not available - try again later.' % downloadReportDate))
# print '%s report is not available - try again later.' % downloadReportDate
unavailableCount += 1
# if unavailableCount > 0:
# raise ITCException, '%i report(s) not available - try again later' % unavailableCount
if options.verbose == True:
wx.PostEvent(notify_window, ResultEvent("Sales Report Download Complete"))
return filenames, False