Python Wrapper for Bing Web Search API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = "Joe Cincotta, Iwan Cornelius, Michael Grassi and Gelin Luo" | |
__copyright__ = "Copyright 2017-2019, Thinking.Studio" | |
__version__ = "1.0" | |
__maintainer__ = "CINJ" | |
__status__ = "Production" | |
""" | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
software and associated documentation files (the "Software"), to deal in the Software | |
without restriction, including without limitation the rights to use, copy, modify, merge, | |
publish, distribute, sublicense, and/or sell copies of the Software, and to permit | |
persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies | |
or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | |
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | |
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. | |
""" | |
import traceback | |
import enum | |
import requests | |
class AnswerCount(object): | |
none = 0 | |
webpages = 1 | |
images = 2 | |
videos = 3 | |
related = 4 | |
map = {none: 0, webpages: 1, images: 2, videos: 3, related: 4} | |
class Filter(enum.Enum): | |
none = 0 | |
Computation = 1 | |
Images = 2 | |
News = 3 | |
RelatedSearches = 4 | |
SpellSuggestions = 5 | |
TimeZone = 6 | |
Videos = 7 | |
Webpages = 8 | |
class Freshness(enum.Enum): | |
none = 0 | |
Day = 1 | |
Week = 2 | |
Month = 3 | |
class SafeSearch(enum.Enum): | |
Off = 0 | |
Moderate = 1 | |
Strict = 2 | |
class Bing(object): | |
def __init__(self, text: str, | |
url_exclusions: list, | |
answers: AnswerCount, | |
result_count: int, | |
freshness: Freshness, | |
result_offset: int, | |
promote: Filter, | |
response_filter: Filter, | |
safe_search: SafeSearch): | |
self.__exclusions = url_exclusions | |
self.__text = text | |
self.__answers = answers | |
self.__promote = promote | |
self.__filter = response_filter | |
self.__freshness = freshness | |
self.__result_count = result_count | |
self.__result_offset = result_offset | |
self.__safe_search = safe_search | |
self.__body = list() | |
self.__title = list() | |
self.__execute() | |
def __len__(self): | |
return len(self.__title) | |
def __getitem__(self, item)->[str, str]: | |
return [self.__title[item], self.__body[item]] | |
def title(self, item) -> str: | |
return self.__title[item] | |
def text(self, item) -> str: | |
return self.__body[item] | |
def __execute(self): | |
response = requests.get(self.__compose_query_string(), | |
headers={"Ocp-Apim-Subscription-Key": "[TODO: Insert API Key Here]"} | |
).json() | |
try: | |
for result in response['webPages']['value']: | |
if not self.__is_excluded(result["url"]): | |
self.__title.append(result["name"]) | |
self.__body.append(result["snippet"]) | |
except Exception: | |
raise Exception(str(response) + "\n\n" + traceback.format_exc()) | |
def __is_excluded(self, url: str) -> bool: | |
if url is None: | |
return False | |
for exc in self.__exclusions: | |
if exc in url: | |
return True | |
return False | |
def __compose_query_string(self): | |
base = "https://api.cognitive.microsoft.com/bing/v7.0/search?q={}".format(self.__text) | |
if self.__answers != AnswerCount.none: | |
base += "&answerCount={}".format(AnswerCount.map[self.__answers]) | |
if self.__result_count > 0: | |
base += "&count={}".format(str(self.__result_count)) | |
if self.__freshness != Freshness.none: | |
base += "&freshness={}".format(self.__freshness.name) | |
if self.__result_offset > 0: | |
base += "&offset={}".format(str(self.__result_offset)) | |
if self.__promote != Filter.none: | |
base += "&promote={}".format(self.__promote.name) | |
if self.__filter != Filter.none: | |
base += "&responseFilter={}".format(self.__filter.name) | |
if self.__safe_search != SafeSearch.Moderate: | |
base += "&safeSearch={}".format(self.__safe_search.name) | |
return base |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment