Beispiel #1
0
def bing(request):
    """
    Search bing using a paid API.
    ---
    type:
      translated:
        type: string
    parameters:
        - name: query
          description: search query
          required: true
          type: string
          paramType: form
        - name: tx
          description: Transaction Id  (proof of payment)
          type: string
          paramType: query
    """
    if 'query' not in request.data:
        return Response({"error": "Must provide a 'query' parameter."},
                        status=status.HTTP_400_BAD_REQUEST)
    api = BingSearchAPI(settings.AZURE_MARKETPLACE_KEY)
    result = api.search_web(
        request.data['query'],
        payload={'$format': 'json'}
    )
    if result.ok:
        return Response({"results": result.text})
    else:
        return Response({"error": result.text},
                        status=status.HTTP_400_BAD_REQUEST)
Beispiel #2
0
def bing(request):
    """
    Search bing using a paid API.
    ---
    type:
      translated:
        type: string
    parameters:
        - name: query
          description: search query
          required: true
          type: string
          paramType: form
        - name: tx
          description: Transaction Id  (proof of payment)
          type: string
          paramType: query
    """
    if 'query' not in request.data:
        return Response({"error": "Must provide a 'query' parameter."},
                        status=status.HTTP_400_BAD_REQUEST)
    api = BingSearchAPI(settings.AZURE_MARKETPLACE_KEY)
    result = api.search_web(request.data['query'], payload={'$format': 'json'})
    if result.ok:
        return Response({"results": result.text})
    else:
        return Response({"error": result.text},
                        status=status.HTTP_400_BAD_REQUEST)
Beispiel #3
0
def getRelevantURLForWord(wd, api_key):
    from bing_search_api import BingSearchAPI

    bing = BingSearchAPI(api_key)
    params = {'$format': 'json', '$skip': '10'}
    result = bing.search_web(wd, payload=params)
    if result.status_code == 200:
        entries = result.json()['d']['results']
        if entries:
            rank = random.randint(0, len(entries)-1)
            url = entries[rank]['Url']
            return url, rank+10
        else:
            return None
    else:
        raise ApiError("Web search api error: {}".format(result.status_code))