-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprovider_partstack.py
More file actions
executable file
·270 lines (243 loc) · 8.67 KB
/
provider_partstack.py
File metadata and controls
executable file
·270 lines (243 loc) · 8.67 KB
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
# -*- coding: utf-8 -*-
import requests
QUERY_FRAGMENT = """
fragment f on Stock {
products {
basic {
manufacturer
mfgpartno
status
}
url
imageUrl
datasheetUrl
}
summary {
inStockInventory
medianPrice
suppliersInStock
}
}
"""
QUERY_STATUS_MAP = {
'active': 'Active',
'active-unconfirmed': 'Active',
'nrfnd': 'NRND',
'eol': 'Obsolete',
'obsolete': 'Obsolete',
'discontinued': 'Obsolete',
'transferred': 'Obsolete',
'contact mfr': None, # Not supported, but here to avoid warning.
}
MANUFACTURER_REPLACEMENTS = {
'ä': 'ae',
'ö': 'oe',
'ü': 'ue',
'texas instruments': 'ti',
'stmicroelectronics': 'st',
}
MANUFACTURER_REMOVALS = set([
'contact',
'devices',
'electronics',
'inc.',
'inc',
'incorporated',
'integrated',
'international',
'limited',
'ltd.',
'ltd',
'microelectronics',
'semiconductor',
'semiconductors',
'solutions',
'systems',
'technology',
'usa',
])
class Partstack:
ID = 'partstack'
NAME = 'Partstack'
URL = 'https://partstack.com'
LOGO_FILENAME = 'parts-provider-partstack.png'
def __init__(self, query_url, query_token, query_timeout, db, logger):
self._query_url = query_url
self._query_token = query_token
self._query_timeout = query_timeout
self._db = db
self._logger = logger
def fetch(self, parts, status):
# Filter out the parts which need to be requested and abort if
# there are no parts to be requested.
filtered_parts = [p for p in parts if 'results' not in p]
if len(filtered_parts) == 0:
return 0
# Request parts data.
query_response = requests.post(
self._query_url,
headers=self._build_headers(),
json=self._build_request(filtered_parts),
timeout=self._query_timeout,
)
query_json = query_response.json()
data = query_json.get('data') or {}
errors = query_json.get('errors') or []
if (len(data) == 0) and (type(query_json.get('message')) is str):
errors.append(query_json['message'])
for error in errors:
self._logger.warning("GraphQL Error: " + str(error))
# Handle quota limit.
next_access_time = query_json.get('nextAccessTime')
if (len(data) == 0) and (next_access_time is not None):
self._logger.warning("Quota limit: " + str(next_access_time))
status['next_access_time'] = next_access_time
# Convert query response data.
for i in range(len(parts)):
if 'results' not in parts[i]:
mpn = parts[i]['mpn']
manufacturer = parts[i]['manufacturer']
part_data = data.get('q' + str(i)) or {}
product = self._get_product(part_data, mpn, manufacturer)
if product is not None:
parts[i]['results'] = 1
basic = product.get('basic') or {}
summary = part_data.get('summary') or {}
self._add_pricing_url(parts[i], product)
self._add_image_url(parts[i], product)
self._add_status(parts[i], basic)
self._add_availability(parts[i], summary)
self._add_prices(parts[i], summary)
self._add_resources(parts[i], product)
self._db.add_parts_cache(self.ID, parts[i])
return 0
def _build_headers(self):
return {
'Content-Type': 'application/json',
'Accept': 'application/json, multipart/mixed',
'Authorization': 'Bearer {}'.format(self._query_token),
}
def _build_request(self, parts):
args = []
queries = []
variables = {}
for i in range(len(parts)):
if 'results' not in parts[i]:
query = 'q{}:findStocks(mfgpartno:$mpn{}){{...f}}'.format(i, i)
args.append('$mpn{}:String!'.format(i))
queries.append(query)
variables['mpn{}'.format(i)] = parts[i]['mpn']
if len(queries) == 0:
return None
query = 'query Stocks({}) {{\n{}\n}}'.format(
','.join(args),
'\n'.join(queries)
) + QUERY_FRAGMENT
return dict(query=query, variables=variables)
def _get_product(self, data, mpn, manufacturer):
products = (data.get('products') or [])
for p in products:
p['_score'] = self._calc_product_match_score(
p, mpn.lower(), self._normalize_manufacturer(manufacturer))
products = sorted([p for p in products if p['_score'] > 0],
key=lambda p: p['_score'], reverse=True)
return products[0] if len(products) else None
def _calc_product_match_score(self, p, mpn_n, mfr_n):
score = 0
status_p = QUERY_STATUS_MAP.get(self._get_basic_value(p, 'status'))
if status_p == 'Active':
score += 200
elif status_p == 'NRND':
score += 100
mpn_p = self._get_basic_value(p, 'mfgpartno').lower()
if mpn_p == mpn_n:
score += 20 # MPN matches exactly.
elif mpn_p.replace(' ', '') == mpn_n.replace(' ', ''):
score += 10 # MPN matches when ignoring whitespaces.
else:
return 0 # MPN does not match!
mfr_p = self._normalize_manufacturer(
self._get_basic_value(p, 'manufacturer'))
if mfr_p == mfr_n:
score += 4 # Manufacturer matches exactly.
elif mfr_n in mfr_p:
score += 3 # Manufacturer matches partially.
elif mfr_n.replace(' ', '') in mfr_p.replace(' ', ''):
score += 2 # Manufacturer matches partially when ignoring spaces.
elif mfr_n.split(' ')[0] in mfr_p:
score += 1 # The first term of the manufacturer matches.
else:
return 0 # Manufacturer does not match!
return score
def _get_basic_value(self, product, key):
if type(product) is dict:
basic = product.get('basic')
if type(basic) is dict:
value = basic.get(key)
if type(value) is str:
return value
return ''
def _normalize_manufacturer(self, mfr):
mfr = mfr.lower()
for old, new in MANUFACTURER_REPLACEMENTS.items():
mfr = mfr.replace(old, new)
terms = [s for s in mfr.split(' ') if s not in MANUFACTURER_REMOVALS]
return ' '.join(terms)
def _add_pricing_url(self, out, data):
value = data.get('url')
if value is not None:
out['pricing_url'] = value
def _add_image_url(self, out, data):
value = data.get('imageUrl')
if value is not None:
out['picture_url'] = value
def _add_status(self, out, data):
status = data.get('status') or ''
status_n = status.lower()
value = QUERY_STATUS_MAP.get(status_n.lower())
if value is not None:
out['status'] = value
elif len(status_n) and (status_n not in QUERY_STATUS_MAP):
self._logger.warning(f'Unknown part lifecycle status: {status}')
def _add_availability(self, out, data):
stock = data.get('inStockInventory')
suppliers = data.get('suppliersInStock')
values = []
if type(stock) is int:
values.append(self._stock_to_availability(stock))
if type(suppliers) is int:
values.append(self._suppliers_to_availability(suppliers))
if len(values):
out['availability'] = min(values)
def _stock_to_availability(self, stock):
if stock > 100000:
return 10 # Very Good
elif stock > 5000:
return 5 # Good
elif stock > 200:
return 0 # Normal
elif stock > 0:
return -5 # Bad
else:
return -10 # Very Bad
def _suppliers_to_availability(self, suppliers):
if suppliers > 30:
return 10 # Very Good
elif suppliers > 9:
return 5 # Good
elif suppliers > 1:
return 0 # Normal
elif suppliers > 0:
return -5 # Bad
else:
return -10 # Very Bad
def _add_prices(self, out, summary):
value = summary.get('medianPrice')
if type(value) in [float, int]:
out['prices'] = [dict(quantity=1, price=float(value))]
def _add_resources(self, out, data):
value = data.get('datasheetUrl')
if value is not None:
out['resources'] = [
dict(name="Datasheet", mediatype="application/pdf", url=value),
]