-
Notifications
You must be signed in to change notification settings - Fork 0
/
kijiji_api.py
303 lines (248 loc) · 11 KB
/
kijiji_api.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
from xml.parsers.expat import ExpatError, errors
from httpx_socks import SyncProxyTransport
from urllib.parse import urlparse, urlunparse
from datetime import datetime, timedelta
import httpx
import xmltodict
import pgeocode
class KijijiApiException(Exception):
"""KijijiApi class exception"""
class KijijiApi:
"""API for interfacing with Kijiji site
This class is stateless and does not manage user logins on its own.
Must login first to use methods that require a user ID and token.
Methods raise KijijiApiException on errors
"""
def __init__(self, session=None, proxy=None):
# Base API URL
self.base_url = 'https://mingle.kijiji.ca/api'
# Kijiji app version number
self.app_ver = '17.7.0'
# Common HTTP header fields
self.headers = {
'Accept': 'application/xml',
'Accept-Language': 'en-CA',
'User-Agent': f'com.ebay.kijiji.ca {self.app_ver} (LGE Nexus 5; Android 6.0.1; en_US)',
'X-ECG-VER': '3.6',
}
if session:
if not isinstance(session, httpx.Client):
raise KijijiApiException("'session' kwarg must be an httpx.Client object")
self.session = session
# Append common headers
self.session.headers = self.headers
else:
# Kijiji sometimes takes a bit longer to respond to API requests
# e.g. for loading conversations
timeout = httpx.Timeout(30.0, connect=30.0)
# Added proxy
if proxy is not None:
transport = SyncProxyTransport.from_url('socks5://' + proxy.get_username() + ':' + proxy.get_password()
+ '@' + proxy.get_host() + ':' + proxy.get_port())
self.session = httpx.Client(timeout=timeout, headers=self.headers, transport=transport)
else:
self.session = httpx.Client(timeout=timeout, headers=self.headers)
def login(self, username, password):
"""Login to Kijiji
:param username: login username
:param password: login password
:return: Tuple of user ID and session token
"""
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {
'username': username,
'password': password,
'socialAutoRegistration': 'false',
}
r = self.session.post(f'{self.base_url}/users/login', headers=headers, data=payload)
doc = self._parse_response(r.text)
if r.status_code == 200:
try:
user_id = doc['user:user-logins']['user:user-login']['user:id']
# email = doc['user:user-logins']['user:user-login']['user:email']
token = doc['user:user-logins']['user:user-login']['user:token']
except KeyError as e:
raise KijijiApiException(f"User ID and/or user token not found in response text: {e}")
return user_id, token
else:
raise KijijiApiException(self._error_reason(doc))
def get_ad(self, user_id, token, ad_id=None):
"""Get existing ad(s)
If ad_id is left unspecified, query all ads
:param user_id: user ID number
:param token: session token
:param ad_id: ad ID number
:return: response data dict
"""
headers = self._headers_with_auth(user_id, token)
url = f'{self.base_url}/users/{user_id}/ads'
if ad_id:
url += f'/{ad_id}'
else:
# Query all ads
url += '?size=50' \
'&page=0' \
'&_in=id,title,price,ad-type,locations,ad-status,category,pictures,start-date-time,' \
'features-active,view-ad-count,user-id,phone,email,rank,ad-address,phone-click-count,' \
'map-view-count,ad-source-id,ad-channel-id,contact-methods,attributes,link,description,' \
'feature-group-active,end-date-time,extended-info,highest-price '
r = self.session.get(url, headers=headers)
doc = self._parse_response(r.text)
if r.status_code == 200:
return doc
else:
raise KijijiApiException(self._error_reason(doc))
def get_profile(self, user_id, token):
"""Get profile data
:param user_id: user ID number
:param token: session token
:return: response data dict
"""
headers = self._headers_with_auth(user_id, token)
r = self.session.get(f'{self.base_url}/users/{user_id}/profile', headers=headers)
doc = self._parse_response(r.text)
if r.status_code == 200:
return doc
else:
raise KijijiApiException(self._error_reason(doc))
def get_categories(self, user_id, token):
"""Get all categories metadata
:param user_id: user ID number
:param token: session token
:return: response data dict
"""
headers = self._headers_with_auth(user_id, token)
r = self.session.get(f'{self.base_url}/categories', headers=headers)
doc = self._parse_response(r.text)
if r.status_code == 200:
return doc
else:
raise KijijiApiException(self._error_reason(doc))
def get_locations(self, user_id, token):
"""Get all locations metadata
:param user_id: user ID number
:param token: session token
:return: response data dict
"""
headers = self._headers_with_auth(user_id, token)
r = self.session.get(f'{self.base_url}/locations', headers=headers)
doc = self._parse_response(r.text)
if r.status_code == 200:
return doc
else:
raise KijijiApiException(self._error_reason(doc))
@staticmethod
def geo_location(postal_code):
postalcode = postal_code[:3]
try:
nomi = pgeocode.Nominatim('ca')
location = nomi.query_postal_code(postalcode)
except Exception as e:
raise KijijiApiException(f'Error acquiring geo location data: {e}')
else:
return location
def delete_ad(self, user_id, token, ad_id):
"""Delete ad
:param user_id: user ID number
:param token: session token
:param ad_id: ad ID number
:return: boolean indicating if deletion was successful
"""
headers = self._headers_with_auth(user_id, token)
r = self.session.delete(f'{self.base_url}/users/{user_id}/ads/{ad_id}', headers=headers)
if r.status_code == 204:
return True
else:
raise KijijiApiException(self._error_reason(self._parse_response(r.text)))
def post_ad(self, user_id, token, data):
"""Post new ad No input validation is performed; incorrect inputs are expected to be reported back by Kijiji
API after attempting to post :param user_id: user ID number :param token: session token :param data: ad xml
data :return: new ad ID number
"""
headers = self._headers_with_auth(user_id, token)
headers.update({'Content-Type': 'application/xml'})
# Expects data to be in correct XML format
xml = data
r = self.session.post(f'{self.base_url}/users/{user_id}/ads', headers=headers, data=xml)
doc = self._parse_response(r.text)
if r.status_code == 201:
try:
ad_id = doc['ad:ad']['@id']
except KeyError as e:
raise KijijiApiException(f"User ID and/or user token not found in response text: {e}")
return ad_id
else:
raise KijijiApiException(self._error_reason(doc))
# def upload_image(self, user_id, token, filename, stream, content_type):
def upload_image(self, user_id, token, data):
"""Upload image Kijiji mobile API
:param user_id: user ID number
:param token: session token
:return: full image URL
"""
api_endpoint = 'https://mobile-api.kijiji.ca/v1/images/upload'
headers = self._headers_with_auth(user_id, token)
headers.update({
'Accept': 'application/json',
'X-ECG-Platform': 'android',
'X-ECG-App-Version': self.app_ver,
})
# Image expiration epoch timestamp
# Kijiji sets this to 199 days, 23 hours from now
expiration_timestamp = int((datetime.today() + timedelta(days=199, hours=23)).timestamp())
# Multipart form data
files = {
'bucketAlias': (None, b'ca-prod-fsbo-ads'),
'objectExpiration': (None, str(expiration_timestamp).encode('utf-8')),
'file': (data.filename, data.read(), data.content_type), # original
# 'file': (filename, stream, content_type),
}
r = self.session.post(api_endpoint, headers=headers, files=files)
# Response is in JSON format
doc = r.json()
if r.status_code == 201:
try:
url = doc['url']
except KeyError as e:
raise KijijiApiException(f"Image URL not found in response text: {e}")
# Query string is appended to URL to specify image size (thumbnail size by default)
# Strip all query strings from URL
url = urlunparse(urlparse(url)._replace(query=''))
return url
else:
raise KijijiApiException(self._error_reason_mobile(doc))
@staticmethod
def _headers_with_auth(user_id, token):
return {'X-ECG-Authorization-User': f'id="{user_id}", token="{token}"'}
@staticmethod
def _parse_response(text):
try:
doc = xmltodict.parse(text)
except ExpatError as e:
raise KijijiApiException(f"Unable to parse text: {errors.messages[e.code]}")
return doc
@staticmethod
def _error_reason(doc):
messages = []
try:
base = doc['api-base-error']
# Three different possible error types - search through all
for err_type in ['api-error', 'api-field-error', 'api-debug-error']:
err_type_plural = err_type + 's' # Error type name with an appended 's'
if err_type_plural in base and base[err_type_plural]:
if err_type in base[err_type_plural] and base[err_type_plural][err_type]:
# Check if there are multiple error messages in the same error type
if isinstance(base[err_type_plural][err_type], list):
for err in base[err_type_plural][err_type]:
messages.append(err_type + ': ' + err['message'])
else:
messages.append(err_type + ': ' + base[err_type_plural][err_type]['message'])
except (TypeError, KeyError):
return 'Unknown API error'
return messages
@staticmethod
def _error_reason_mobile(doc):
try:
return doc['message']
except KeyError:
return 'Unknown mobile API error'