-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.py
More file actions
54 lines (40 loc) · 1.58 KB
/
Copy pathquickstart.py
File metadata and controls
54 lines (40 loc) · 1.58 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
"""
Search + fetch a fragrance profile from the public Noteboxd API in Python.
NOTEBOXD_API_KEY=nb_live_... python examples/python/quickstart.py oud
Uses only the standard library (urllib) so there are no dependencies to install.
Get a free key at https://developers.noteboxd.com
"""
import json
import os
import sys
import urllib.parse
import urllib.request
API_KEY = os.environ.get("NOTEBOXD_API_KEY")
BASE = os.environ.get("NOTEBOXD_API_BASE_URL", "https://api.noteboxd.com")
if not API_KEY:
sys.exit("Set NOTEBOXD_API_KEY. Get one free at https://developers.noteboxd.com")
def api(path: str) -> dict:
req = urllib.request.Request(
f"{BASE}{path}",
headers={"Authorization": f"Bearer {API_KEY}", "Accept": "application/json"},
)
with urllib.request.urlopen(req) as resp:
return json.load(resp)
def main() -> None:
query = sys.argv[1] if len(sys.argv) > 1 else "oud"
results = api(f"/v1/search?q={urllib.parse.quote(query)}&type=fragrance&limit=5")
hits = results.get("data", [])
if not hits:
print(f"No fragrances found for {query!r}.")
return
print(f"Top matches for {query!r}:")
for hit in hits:
print(f" • {hit.get('name')} ({hit.get('slug')})")
first_id = hits[0].get("id")
if first_id:
profile = api(f"/v1/fragrances/{urllib.parse.quote(first_id)}").get("data", {})
print(f"\nProfile: {profile.get('name')}")
print(f" community score: {profile.get('communityScore')}")
print(f" family: {profile.get('familyPrimary')}")
if __name__ == "__main__":
main()