partsdb

electronic parts inventory
git clone https://git.e1e0.net/partsdb.git
Log | Files | Refs | README | LICENSE

octopart.py (2778B)


      1 #!/usr/bin/env python3
      2 # -*- coding: utf-8 -*-
      3 # vim:fenc=utf-8
      4 
      5 import json
      6 import sys
      7 import urllib.error
      8 import urllib.request
      9 
     10 
     11 GET_PARTS_GRAPHQL_QUERY = '''
     12     query MyPartSearch($q: String!, $filters: Map) {
     13         search(q: $q, filters: $filters, limit: 5) {
     14             total
     15             results {
     16                 part {
     17                     id
     18                     mpn
     19                     aka_mpns
     20                     manufacturer {
     21                         name
     22                     }
     23                     short_description
     24                     specs {
     25                         attribute {
     26                             name
     27                             shortname
     28                         }
     29                         display_value
     30                     }
     31                     best_datasheet {
     32                         url
     33                         mime_type
     34                     }
     35                     best_image {
     36                         url
     37                     }
     38                     document_collections {
     39                         name
     40                         documents {
     41                             name
     42                             url
     43                             mime_type
     44                         }
     45                     }
     46                 }
     47             }
     48         }
     49     }
     50 '''
     51 
     52 
     53 # copy pasted from: https://github.com/prisma-labs/python-graphql-client/blob/master/graphqlclient/client.py
     54 class OctopartClient():
     55 
     56     def __init__(self, token):
     57         self.endpoint = 'https://octopart.com/api/v4/endpoint'
     58         if token is None:
     59             print('I need OCTOPART_TOKEN')
     60             sys.exit(1)
     61         self.token = token
     62 
     63     def execute(self, query, variables=None):
     64         return self._send(query, variables)
     65 
     66     def _send(self, query, variables):
     67         data = {'query': query,
     68                 'variables': variables}
     69         headers = {'Accept': 'application/json',
     70                    'Content-Type': 'application/json'}
     71 
     72         if self.token is not None:
     73             headers['token'] = '{}'.format(self.token)
     74 
     75         req = urllib.request.Request(self.endpoint,
     76                                      json.dumps(data).encode('utf-8'), headers)
     77 
     78         try:
     79             response = urllib.request.urlopen(req)
     80             return response.read().decode('utf-8')
     81         except urllib.error.HTTPError as e:
     82             print((e.read()))
     83             raise e
     84 
     85     def get_part(self, mpn):
     86         query = GET_PARTS_GRAPHQL_QUERY
     87         # parameters = {
     88         #     "q": "INA219AIDR",
     89         #     "filters": {
     90         #         "manufacturer_id": ["370"]
     91         #     }
     92         # }
     93         parameters = {
     94             "q": mpn
     95         }
     96 
     97         resp = self.execute(query, parameters)
     98         # print(json.dumps(json.loads(resp), indent = 2))
     99         return json.loads(resp)