-
Notifications
You must be signed in to change notification settings - Fork 38
/
waveapps.py
209 lines (172 loc) · 7.34 KB
/
waveapps.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
"""Retrieves receipt images and extracted data from waveapps.com.
This uses the waveapps API (https://docs.waveapps.io/) to retrieve the data
directly.
Configuration:
==============
The following keys may be specified as part of the configuration dict:
- `credentials`: Required. Must be a `dict` with a `'token'` key specifying a
Full Access token. To generate a token, first sign in to https://waveapps.com
and then visit the "Manage Applications" page:
https://developer.waveapps.com/hc/en-us/articles/360019762711
Choose "Create an application", then after creating an application choose
"Create token".
Alternatively, if you have a valid OAuth2 client id, instead of the `'token'`
field you may specify `'client_id'`, `'username'`, and `'password'` fields.
Signing in with a Google account is not supported.
- `output_directory`: Required. Must be a `str` that specifies the path on the
local filesystem where the output will be written. If the directory does not
exist, it will be created.
- `use_business_directory`: Optional. If specified, must be a `bool`. If `True`,
create a subdirectory in `output_directory` to write the output for each
business ID.
- `active_only`: Optional. If specified, must be a `bool`. If `True`, do not
download deleted receipts.
Output format:
==============
This module downloads receipts for all businesses that are accessible using the
specified `credentials`. The receipts for each business is stored in the
sub-directory of the specified `output_directory` with a name equal to the
business name. If the sub-directory does not exist, it will be created.
Within each business sub-directory, for each receipt, the JSON data as returned
by the API is saved as `<receipt-id>.json`. The JSON data contains at least the
following fields:
- `id`: The unique receipt identifier, matching the `<receipt-id>` portion of
the filename.
- `date`: The date.
- `merchant`: Merchant name
- `note`: Optional note.
- `total`: Total amount.
- `currency_code`: The currency code.
The corresponding receipt images are saved in full resolution as:
`<receipt-id>.jpeg`, and if there are additional images, as
`<receipt-id>.01.jpeg`, `<receipt-id>.02.jpeg`, etc.
Example:
========
def CONFIG_waveapps():
return dict(
module='finance_dl.waveapps',
credentials={
'token': 'XXXXXX',
},
output_directory=os.path.join(data_dir, 'waveapps'),
)
"""
from typing import List, Any, Optional
import contextlib
import logging
import json
import os
import requests
from atomicwrites import atomic_write
logger = logging.getLogger('waveapps')
class WaveScraper(object):
def __init__(self, credentials: dict, output_directory: str,
use_business_directory: bool = False,
active_only: bool = False, headless=None):
del headless
self.credentials = credentials
self.output_directory = output_directory
self.use_business_directory = use_business_directory
self.active_only = active_only
def get_oauth2_token(self):
if 'token' in self.credentials:
logger.info('Using specified token')
self._oauth_token = {
'token_type': 'Bearer',
'access_token': self.credentials['token']
}
else:
logger.info('Obtaining oauth2 token')
oauth_url = 'https://api.waveapps.com/oauth2/token/'
response = requests.post(
oauth_url, files={
k: (None, v, None, {})
for k, v in [
('client_id', self.credentials['client_id']),
('username', self.credentials['username']),
('grant_type', 'password'),
('password', self.credentials['password']),
]
})
response.raise_for_status()
self._oauth_token = response.json()
self._authenticated_headers = {
'authorization':
self._oauth_token['token_type'] + ' ' +
self._oauth_token['access_token'],
}
def get_businesses(self):
logger.info('Getting list of businesses')
response = requests.get(
'https://api.waveapps.com/businesses/?include_personal=true',
headers=dict(self._authenticated_headers,
accept='application/json'),
)
response.raise_for_status()
result = response.json()
logger.info('Got %d businesses', len(result))
return result
def get_receipts(self, business_id: str):
logger.info('Getting receipts for business %s', business_id)
receipts = [] # type: List[Any]
response = requests.get(
'https://api.waveapps.com/businesses/' + business_id +
'/receipts/?active_only=' +
(self.active_only and 'true' or 'false'),
headers=dict(self._authenticated_headers,
accept='application/json'),
)
response.raise_for_status()
result = response.json()
cur_list = result['results']
logger.info('Received %d receipts', len(cur_list))
receipts.extend(cur_list)
return receipts
def save_receipts(self, receipts: List[Any], output_directory: Optional[str] = None):
if not output_directory:
output_directory = self.output_directory
if not os.path.exists(output_directory):
os.makedirs(output_directory)
for receipt in receipts:
output_prefix = os.path.join(output_directory,
str(receipt['id']))
json_path = output_prefix + '.json'
for image_i, image in enumerate(receipt['images']):
image_url = image['file']
if image_i == 0:
image_path = '%s.jpg' % (output_prefix, )
else:
image_path = '%s.%02d.jpg' % (output_prefix, image_i)
if not os.path.exists(image_path):
logger.info('Downloading receipt image %s', image_url)
r = requests.get(image_url)
r.raise_for_status()
data = r.content
with atomic_write(image_path, mode='wb', overwrite=True) as f:
f.write(data)
with atomic_write(
json_path,
mode='w',
overwrite=True,
encoding='utf-8',
newline='\n') as f:
json.dump(receipt, f, sort_keys=True, indent=' ')
def run(self):
self.get_oauth2_token()
output_directory = self.output_directory
businesses = self.get_businesses()
for business in businesses:
business_id = business['id']
receipts = self.get_receipts(business_id)
if receipts and self.use_business_directory:
output_directory = os.path.join(self.output_directory,
business_id)
self.save_receipts(receipts, output_directory)
def run(**kwargs):
scraper = WaveScraper(**kwargs)
scraper.run()
@contextlib.contextmanager
def interactive(**kwargs):
scraper = WaveScraper(**kwargs)
kwargs['scraper'] = scraper
yield kwargs