Exemple #1
0
 def search(self, query, numResultsRequested=50, offset=0):
     # TODO: Replace the x's with the Primary Account Key of your
     # Microsoft Account.
     bingKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
     
     self._query = query
     self._numResultsRequested = numResultsRequested
     self._offset = offset
     
     searchService = PyMsCognitiveImageSearch(
             bingKey, query,
             custom_params='?color=ColorOnly&imageType=Photo')
     searchService.current_offset = offset
     
     try:
         self._results = searchService.search(numResultsRequested, 'json')
     except Exception as e:
         print >> sys.stderr, \
             'Error when requesting Bing image search for "%s":' % query
         print >> sys.stderr, e.message
         self._offset = 0
         self._numResultsReceived = 0
         return
     
     json = searchService.most_recent_json
     self._numResultsReceived = len(self._results)
     if self._numResultsRequested < self._numResultsReceived:
         # py_ms_cognitive modified the request to get more results.
         self._numResultsRequested = self._numResultsReceived
     self._numResultsAvailable = int(json[u'totalEstimatedMatches'])
     
     if self.verbose:
         print 'Received results of Bing image search for "%s":' % query
         pprint.pprint(json)
    def search(self, query, numResultsRequested=50, offset=0):
        if 'BING_SEARCH_KEY' in os.environ:
            bingKey = os.environ['BING_SEARCH_KEY']
        else:
            sys.stderr.write(
                    'Environment variable BING_SEARCH_KEY is undefined. '
                    'Please define it, equal to your Bing Search API key.\n')
            return

        self._query = query
        self._numResultsRequested = numResultsRequested
        self._offset = offset

        params = {'color':'ColorOnly', 'imageType':'Photo'}

        searchService = PyMsCognitiveImageSearch(
                bingKey, query, custom_params=params)
        searchService.current_offset = offset

        try:
            self._results = searchService.search(numResultsRequested, 'json')
        except Exception as e:
            sys.stderr.write(
                    'Error when requesting Bing image search for '
                    '"%s":\n' % query)
            sys.stderr.write('%s\n' % str(e))
            self._offset = 0
            self._numResultsReceived = 0
            return

        json = searchService.most_recent_json
        self._numResultsReceived = len(self._results)
        if self._numResultsRequested < self._numResultsReceived:
            # py_ms_cognitive modified the request to get more results.
            self._numResultsRequested = self._numResultsReceived
        self._numResultsAvailable = int(json[u'totalEstimatedMatches'])

        if self.verbose:
            print('Received results of Bing image search for '
                  '"%s":' % query)
            pprint.pprint(json)