Exemplo n.º 1
0
 def __init__(self):
     try:
         self.octopart_api_key = os.environ['OCTOPART_API_KEY']
     except KeyError:
         print('Missing OCTOPART_API_KEY environment variable.')
         sys.exit(1)
     self.octopart = Octopart(apikey=self.octopart_api_key)
Exemplo n.º 2
0
def run(mpnArr):
    # prepare data for pyoctopart library
    octopartApi = Octopart(apikey=OCTOPART_API_KEY)
    i: int = 0
    for mpn in mpnArr:
        output = octopartApi.parts_match([{
            "mpn": mpn,
            "reference": "line" + i.__str__(),
        }])
        print(output)
        i = i + 1
        if ((i % 25) == 24):
            time.sleep(60)
Exemplo n.º 3
0
from pyoctopart.octopart import Octopart

o = Octopart.api(apikey="0b7f5d7f771394a62876")
Exemplo n.º 4
0
 def __init__(self, apikey, verbose=False):
     '''
     instanciate an engine
     '''
     self._e = Octopart(apikey=apikey, verbose=verbose)
Exemplo n.º 5
0
class PyPartsOctopart(PyPartsBase):
    '''
    Implementation of the part browse engine
    '''
    def __init__(self, apikey, verbose=False):
        '''
        instanciate an engine
        '''
        self._e = Octopart(apikey=apikey, verbose=verbose)

    def part_search(self, part_query):
        '''
        handles the part lookup/search for the given part query

        part_query: part string to search as product name

        outputs result on stdout
        '''
        limit = 100
        results = self._e.parts_search(q=part_query,
                                       limit=limit)
        start = 0
        hits = results[0]['hits']
        if hits == 0:
            print("No result")
            return ReturnValues.NO_RESULTS

        print("Searched for: '{}'".format(results[0]['request']['q']))
        def show_result(r):
            print(' → {:30} {:30} {}'.format(
                r['item']['mpn'], r['item']['manufacturer']['name'], r['snippet']
            ))

        for r in results[1]:
            show_result(r)
        while hits - limit > limit:
            start += limit
            hits -= limit
            results = self._e.parts_search(q=part_query, limit=limit,
                                            start=start)
            for r in results[1]:
                show_result(r)
        if hits-limit > 0:
            start += limit
            hits -= limit
            results = self._e.parts_search(q=part_query,
                                            limit=hits,
                                            start=start)
            for r in results[1]:
                show_result(r)
        return ReturnValues.OK

    def part_specs(self, part):
        '''
        returns the specifications of the given part. If multiple parts are
        matched, only the first one will be output.

        part: the productname or sku

        prints the results on stdout
        '''
        result = self._e.parts_match(
            queries=[{'mpn_or_sku': part}],
            exact_only=True,
            show_mpn=True,
            show_manufacturer=True,
            show_octopart_url=True,
            show_short_description=True,
            show_specs=True,
            show_category_uids=True,
            show_external_links=True,
            show_reference_designs=True,
            show_cad_models=True,
            show_datasheets=True,
            include_specs=True,
            include_category_uids=True,
            include_external_links=True,
            include_reference_designs=True,
            include_cad_models=True,
            include_datasheets=True
        )
        if result[1][0]['hits'] == 0:
            print("No result")
            return ReturnValues.NO_RESULTS

        result = result[1][0]['items'][0]
        print("Showing specs for '{}':".format(result['mpn']))
        print(" → Manufacturer:      {}".format(result['manufacturer']['name']))
        print("  → Specifications:    ")
        for k,v in result['specs'].items():
            name = v['metadata']['name'] if v['metadata']['name'] else k
            min_value = v['min_value'] if v['min_value'] else ''
            max_value = v['max_value'] if v['max_value'] else ''
            unit = ' ({})'.format(v['metadata']['unit']['name']) if v['metadata']['unit'] else ''
            value = ','.join(v['value']) if len(v['value']) > 0 else ''

            if value and not (min_value or max_value):
                print("    → {:20}: {}{}".format(name, value, unit))
            elif value and min_value and max_value:
                print("    → {:20}: {}{} (min: {}, max: {})".format(name, value, unit, min_value, max_value))
            elif not value and min_value and max_value:
                print("    → {:20}:{} min: {}, max: {}".format(name, unit, min_value, max_value))
            elif not value and min_value and not max_value:
                print("    → {:20}:{} min: {}".format(name, unit, min_value))
            elif not value and not min_value and max_value:
                print("    → {:20}:{} max: {}".format(name, unit, max_value))

        print(" → URI:               {}".format(result['octopart_url']))
        if result['external_links']['evalkit_url'] \
                or result['external_links']['freesample_url'] \
                or result['external_links']['product_url']:
            print("  → External Links")
            if result['external_links']['evalkit_url']:
                print("    → Evaluation kit: {}".format(result['external_links']['evalkit_url']))
            if result['external_links']['freesample_url']:
                print("    → Free Sample: {}".format(result['external_links']['freesample_url']))
            if result['external_links']['product_url']:
                print("    → Product URI: {}".format(result['external_links']['product_url']))
        if len(result['datasheets']) > 0:
            print("  → Datasheets")
            for datasheet in result['datasheets']:
                print("    → URL:      {}".format(datasheet['url']))
                if datasheet['metadata']:
                    print("      → Updated:  {}".format(datasheet['metadata']['last_updated']))
                    print("      → Nb Pages: {}".format(datasheet['metadata']['num_pages']))
        if len(result['reference_designs']) > 0:
            print("  → Reference designs: ")
        if len(result['cad_models']) > 0:
            print("  → CAD Models:        ")
        return ReturnValues.OK

    def part_datasheet(self, part, command=None, path=None):
        '''
        downloads and/or shows the datasheet of a given part

        command: if set will use it to open the datasheet.
        path: if set will download the file under that path.

        if path is given alone, the file will only get downloaded,
        if command is given alone, the file will be downloaded in a temporary
        folder, which will be destroyed just after being opened.
        if both path and command are given, the file will be downloaded and
        stored in the chosen location.
        '''
        result = self._e.parts_match(
            queries=[{'mpn_or_sku': part}],
            exact_only=True,
            show_mpn=True,
            show_datasheets=True,
            include_datasheets=True
        )
        if result[1][0]['hits'] == 0:
            print("No result")
            return ReturnValues.NO_RESULTS

        result = result[1][0]['items'][0]
        print("Downloading datasheet for '{}':".format(result['mpn']))
        try:
            if len(result['datasheets']) > 0:
                for datasheet in result['datasheets']:
                    if not path:
                        path = tempfile.mkdtemp()
                    out = path+'/'+result['mpn']+'-'+datasheet['url'].split('/')[-1]
                    download_file(datasheet['url'], out)
                    print('Datasheet file saved as {}.'.format(out))
                    if command:
                        subprocess.call([command, out])
        finally:
            if not path:
                shutil.rmtree(path)
        return ReturnValues.OK

    def part_show(self, part, printout=False):
        '''
        Opens/shows the aggregator's URI for the part.


        printout: if set, only printout the URI, do not open the browser.
        '''

        result = self._e.parts_match(
            queries=[{'mpn_or_sku': part}],
            exact_only=True,
            show_mpn=True,
            show_octopart_url=True
        )
        if result[1][0]['hits'] == 0:
            print("No result")
            return ReturnValues.NO_RESULTS
        result = result[1][0]['items'][0]
        if not printout:
            print("Opening page for part '{}'.".format(result['mpn']))
            webbrowser.open(result['octopart_url'], 2)
        else:
            print("Webpage for part '{}':".format(result['mpn']))
            print("    → URL:      {}".format(result['octopart_url']))
        return ReturnValues.OK
Exemplo n.º 6
0
class Scottopart:
    def __init__(self):
        try:
            self.octopart_api_key = os.environ['OCTOPART_API_KEY']
        except KeyError:
            print('Missing OCTOPART_API_KEY environment variable.')
            sys.exit(1)
        self.octopart = Octopart(apikey=self.octopart_api_key)


    def match_by_mpn(self, mpn):
        """Lookup a list of MPNs in Octopart."""
        if not isinstance(mpn, (list, tuple)):
            # If called with a single string, return a scalar instead of a list.
            return self.match_by_mpn([mpn])[0]
        octopart_results = []
        for i in range(0, len(mpn), 20):
            batched_mpns = mpn[i: i + 20]
            batched_queries = [{'mpn': x, 'seller': 'Digi-Key', 'limit': 1} for x in batched_mpns]
            result = self.octopart.parts_match(queries=batched_queries,
                                               exact_only=True,
                                               include_short_description=True,
                                               include_specs=True,
                                               include_category_uids=True)
            octopart_results.extend(result[0]['results'])
        # Now process the items in octopart_results
        scottopart_results = []
        for i, r in enumerate(octopart_results):
            if r['hits'] == 0:
                # This query returned no results!
                scottopart_results.append({'query_mpn': mpn[i],
                                           'success': False})
            else:
                table_name = get_tablename(r['items'][0]['category_uids'])
                table_row = {}
                table_row['Manufacturer'] = standardize_manufacturer(r['items'][0]['manufacturer']['name'])
                table_row['MPN'] = r['items'][0]['mpn']
                table_row['Description'] = r['items'][0]['short_description']
                table_row['ComponentLink1Description'] = 'Octopart'
                table_row['ComponentLink1URL'] = r['items'][0]['octopart_url']
                table_row['Type'] = get_parttype(r['items'][0]['category_uids'])
                # Check for non-active lifecycle status.
                if 'lifecycle_status' in r['items'][0]['specs'] and 'display_value' in r['items'][0]['specs']['lifecycle_status']:
                    if r['items'][0]['specs']['lifecycle_status']['display_value'] != 'Active':
                        table_row['Comment'] = 'WARNING: Lifecycle Status is ' + r['items'][0]['specs']['lifecycle_status']['display_value']
                authorized_suppliers = ['Digi-Key', 'Mouser']
                filtered_offers = [x for x in r['items'][0]['offers'] if x['seller']['name'] in authorized_suppliers]
                # Check for low in_stock_quantity.
                if max([x['in_stock_quantity'] for x in filtered_offers]) < 500:
                    table_row['Comment'] = 'WARNING: LOW STOCK'
                # Pick a supplier part number.
                for offer in r['items'][0]['offers']:
                    if offer['seller']['name'] == 'Digi-Key' and offer['packaging'] == 'Cut Tape':
                        table_row['Supplier 1'] = 'Digi-Key'
                        table_row['Supplier Part Number 1'] = offer['sku']
                # Fill in all the component specs.
                for key, value in r['items'][0]['specs'].items():
                    if 'display_value' in value:
                        table_row[value['metadata']['name']] = value['display_value']
                # Fix up some field names.
                if 'Voltage Rating (DC)' in table_row:
                    table_row['Voltage Rating'] = table_row.pop('Voltage Rating (DC)')
                # TODO: Normalize "Mount" and "Mounting Type", etc. to "Mounting Style"
                # Normalize ratings (eliminate trailing zeroes, use correct Unicode character for unit, etc.)
                table_row = normalize_ratings(table_row)
                # Standardize Description field for certain part types.
                table_row['Description'] = standardize_description(table_name, table_row)

                scottopart_results.append({'query_mpn': mpn[i],
                                           'success': True,
                                           'table_name': table_name,
                                           'table_row': table_row
                })
        return scottopart_results