octopart search added. note: the API key is NOT included in the public repo, for obvious reasons.

pull/3/head
Marek Baczynski 6 years ago
parent b3aad9a1ca
commit ad643464cb

@ -0,0 +1 @@
{"URL":"http://octopart.com/api/v3/parts/search?apikey=", "key":""}

@ -1,6 +1,6 @@
import os
import re
import json
import json, urllib.parse, urllib.request
import sqlalchemy
from functools import wraps
from sqlalchemy.sql import select
@ -18,6 +18,7 @@ app = Flask(__name__)
db_engine = {}
db_metadata = {}
parts = {}
octopartURL =""
def getContainers():
query = "select id, name from containers order by UPPER(name);"
@ -272,6 +273,26 @@ def deleteLocation(locationID):
r = db_engine.execute(s, id=locationID)
return '{"status":"ok"}'
@app.route('/parts/fetchOctopartSnippet/<searchTerm>')
def fetchOctopartSnippet(searchTerm):
if octopartURL == '':
return '{"result":"octopart integration not enabled"}'
args = [
('q', searchTerm),
('start', 0),
('limit', 1)
]
data = urllib.request.urlopen(octopartURL + '&' + urllib.parse.urlencode(args)).read()
search_response = json.loads(data)
result = '{"result":"no results. sorry :(("}'
if search_response['hits']>0:
try:
result = '{"result":"ok", "snippet":"' + (search_response['results'][0]['snippet']) + '"}';
except TypeError:
result = '{"result":"no results?"}'
return result
def connect(user, password, db, host='localhost', port=5432):
'''Returns a connection and a metadata object'''
# We connect with the help of the PostgreSQL URL
@ -291,6 +312,16 @@ if __name__ == '__main__':
postgres_credentials = json.load(f)
db_engine, db_metadata = connect(postgres_credentials['username'], postgres_credentials['password'], 'parts_v2')
parts = sqlalchemy.Table('parts', db_metadata)
try:
with open('octopartAPIkey.json') as f:
j = json.load(f);
if j['key'] is not '':
octopartURL = j['URL'] + j['key']
print ("Octopart credentials loaded.")
else:
raise FileNotFoundError
except FileNotFoundError:
print ("NO OCTOPART KEY FOUND. ABANDONING THAT PART")
# Example query
'''s = select([parts]).where(parts.c.notes != '')
for row in db_engine.execute(s):

@ -5,6 +5,7 @@ function init_edit(partID) {
$('table#details tr#partno td p').hide();
$('table#details tr#partno td input').show();
$('#magical_autofill').show();
$('table#details tr#description td p').hide();
$('table#details tr#description td input').show();
@ -42,6 +43,8 @@ function end_edit() {
$('table#details tr#partno td p').show();
$('table#details tr#partno td input').hide();
$('#magical_autofill').hide();
$('table#details tr#description td p').text($('table#details tr#description td input').val());
$('table#details tr#description td p').show();
$('table#details tr#description td input').hide();
@ -216,6 +219,18 @@ function container_onchange() {
}
}
function octopartFetch(){
$.getJSON(rootURL + 'fetchOctopartSnippet/' + $('#partno-input').val()).done(function(json){
if (json['result']=='ok'){
$('#description-input').val(json['snippet']);
}else{
$('#description-input').val('');
$('#description-input').attr('placeholder', json['result']);
}
});
}
$(document).ready(function() {
$.ajaxSetup({ cache: false });
$('.search-bar').on('keyup', function() {

@ -57,9 +57,10 @@
</select>
</td></tr>
<tr id="partno-head"><td>PART NUMBER</td></tr>
<tr id="partno"><td><p></p><input type="text" name="partno-input" placeholder="Part Number" class="pinfo-input"/></td></tr>
<tr id="partno"><td><p></p><input type="text" name="partno-input" placeholder="Part Number" class="pinfo-input" id="partno-input"/></td></tr>
<tr id="description-head"><td>DESCRIPTION</td></tr>
<tr id="description"><td><p></p><input type="text" name="description-input" placeholder="Description" class="pinfo-input"/></td></tr>
<tr id="description"><td><p></p><input type="text" name="description-input" placeholder="Description" class="pinfo-input" id="description-input"/></td></tr>
<tr id="magical_autofill" style="display:none"><td><input type="button" id="magic-autofill-button" onclick="octopartFetch()" value="magical auto-fill" /></td></tr>
<tr id="datasheet-head"><td>DATASHEET: </td></tr>
<tr id="datasheet"><td><input type="file" accept=".pdf" class="pinfo-input"></td></tr>
</table>

Loading…
Cancel
Save