partsdb

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

octopart.py (2956B)


      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                     octopart_url
     25                     specs {
     26                         attribute {
     27                             name
     28                             shortname
     29                         }
     30                         display_value
     31                     }
     32                     best_datasheet {
     33                         url
     34                         mime_type
     35                     }
     36                     best_image {
     37                         url
     38                     }
     39                     document_collections {
     40                         name
     41                         documents {
     42                             name
     43                             url
     44                             mime_type
     45                         }
     46                     }
     47                     sellers {
     48                         company {
     49                             name
     50                         }
     51                     }
     52                 }
     53             }
     54         }
     55     }
     56 '''
     57 
     58 
     59 # copy pasted from: https://github.com/prisma-labs/python-graphql-client/blob/master/graphqlclient/client.py
     60 class OctopartClient():
     61 
     62     def __init__(self, token):
     63         self.endpoint = 'https://octopart.com/api/v4/endpoint'
     64         if token is None:
     65             print('I need OCTOPART_TOKEN')
     66             sys.exit(1)
     67         self.token = token
     68 
     69     def execute(self, query, variables=None):
     70         return self._send(query, variables)
     71 
     72     def _send(self, query, variables):
     73         data = {'query': query,
     74                 'variables': variables}
     75         headers = {'Accept': 'application/json',
     76                    'Content-Type': 'application/json'}
     77 
     78         if self.token is not None:
     79             headers['token'] = '{}'.format(self.token)
     80 
     81         req = urllib.request.Request(self.endpoint,
     82                                      json.dumps(data).encode('utf-8'), headers)
     83 
     84         try:
     85             response = urllib.request.urlopen(req)
     86             return response.read().decode('utf-8')
     87         except urllib.error.HTTPError as e:
     88             print((e.read()))
     89             raise e
     90 
     91     def get_part(self, mpn):
     92         query = GET_PARTS_GRAPHQL_QUERY
     93         # parameters = {
     94         #     "q": "INA219AIDR",
     95         #     "filters": {
     96         #         "manufacturer_id": ["370"]
     97         #     }
     98         # }
     99         parameters = {
    100             "q": mpn
    101         }
    102 
    103         resp = self.execute(query, parameters)
    104         # print(json.dumps(json.loads(resp), indent = 2))
    105         return json.loads(resp)