Skip to content

Commit dd631d5

Browse files
Added search, changed browser agent
1 parent 2450fdf commit dd631d5

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

smallPartDb.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
# config the logging behavior
18-
logging.basicConfig(filename='log.log',level=logging.DEBUG)
18+
logging.basicConfig(filename='log.log', level=logging.DEBUG)
1919

2020
class Endpoint():
2121
base_url = ""
@@ -42,6 +42,7 @@ class Endpoint():
4242
footprintsId = ""
4343
parameters = ""
4444
parametersId = ""
45+
partsearch = ""
4546

4647
def __init__(self, base_url):
4748
self.base_url = base_url
@@ -69,6 +70,7 @@ def __init__(self, base_url):
6970
self.footprintsId = base_url + "footprints/{id}"
7071
self.parameters = base_url + "parameters"
7172
self.parametersId = base_url + "parameters/{id}"
73+
self.partsearch = base_url + "parts.jsonld?name={phrase}"
7274

7375
class smallPartDb():
7476
host = None
@@ -82,19 +84,25 @@ class smallPartDb():
8284
attachments = []
8385
footprints = []
8486
attachmentTypes = []
87+
token = None
8588

8689
def __init__(self, host, token):
8790
self.host = host
91+
self.token = token
8892
self.endpoint = Endpoint("http://" + self.host + "/api/")
8993

90-
self.header = {'Content-Type': 'application/json', 'User-Agent':'APS-DB-Converter', 'Accept': 'application/json', 'Authorization': 'Bearer ' + token}
94+
self.header = {'Content-Type': 'application/json', 'User-Agent':'smallPartDb', 'Accept': 'application/json', 'Authorization': 'Bearer ' + token}
9195
self.r = requests.Session()
9296
self.r.headers.update(self.header)
9397

94-
self.headerPatch = {'Content-Type': 'application/merge-patch+json', 'User-Agent':'APS-DB-Converter', 'Accept': 'application/ld+json', 'Authorization': 'Bearer ' + token}
98+
self.headerPatch = {'Content-Type': 'application/merge-patch+json', 'User-Agent':'smallPartDb', 'Accept': 'application/ld+json', 'Authorization': 'Bearer ' + token}
9599
self.rUpdate = requests.Session()
96100
self.rUpdate.headers.update(self.headerPatch)
97101

102+
self.searchSearch = {'Content-Type': 'application/ld+json', 'User-Agent':'smallPartDb', 'Accept': 'application/ld+json', 'Authorization': 'Bearer ' + token}
103+
self.rSearchPart = requests.Session()
104+
self.rSearchPart.headers.update(self.searchSearch)
105+
98106
self.getInfo()
99107

100108
def __str__(self):
@@ -515,6 +523,13 @@ def lookupManufacturer(self, name):
515523
warnings.warn("no manufacturer found for >" + name + "<")
516524
return manufacturersId
517525

526+
def searchPart(self, phrase):
527+
phrase = urllib.parse.quote(phrase)
528+
529+
url = self.endpoint.partsearch.format(phrase=phrase)
530+
print(url)
531+
return self.rSearchPart.get(url)
532+
518533
def getParts(self):
519534
url = self.endpoint.parts.format()
520535
p = 1
@@ -585,7 +600,7 @@ def getAttachments(self):
585600

586601
if __name__ == '__main__':
587602

588-
with open("settings.yaml") as stream:
603+
with open("./smallPartDb/settings.yaml") as stream:
589604
try:
590605
settings = yaml.safe_load(stream)
591606
except yaml.YAMLError as exc:
@@ -600,3 +615,11 @@ def getAttachments(self):
600615
if status.status_code == 200:
601616
for p in partDb.parts:
602617
print(str(p['id']) + ": " + p['name'])
618+
619+
print("search...")
620+
resp = partDb.searchPart("BC547")
621+
if resp.status_code == 200:
622+
print(json.dumps(resp.text))
623+
else:
624+
print("Error while search: " + str(resp.status_code))
625+

0 commit comments

Comments
 (0)