-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the lookup-requests.py sample (#349)
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# This version of the 'lookup' program uses the 'requests' library to make the | ||
# call to the free dictionary api. To install the library first run: | ||
# python3 -m venv .venv # create a local virtual environment | ||
# source .venv/bin/activate | ||
# pip3 install requests | ||
import sys | ||
import urllib.request | ||
import urllib.parse | ||
import json | ||
import requests | ||
|
||
def search_for_word(word): | ||
# Encode the word for HTML. | ||
encoded_word = urllib.parse.quote(word.encode('utf8')) | ||
|
||
# Try and download the definition using the amazing dictionaryapi.dev site. | ||
try: | ||
url = "https://api.dictionaryapi.dev/api/v2/entries/en/{}".format(encoded_word) | ||
if False: | ||
response = urllib.request.urlopen(url) | ||
if response.status == 404: | ||
print("NOT FOUND") | ||
sys.exit(1) | ||
with urllib.request.urlopen(url) as response: | ||
raw_json_data = response.read().decode('utf-8') | ||
|
||
elif True: | ||
# Avoid detection as a bot | ||
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}) | ||
raw_json_data = urllib.request.urlopen(req).read().decode('utf-8') | ||
else: | ||
# Use requests lib | ||
r = requests.get(url) | ||
raw_json_data = r.text | ||
|
||
# If the word is not found, return an empty definition. | ||
except urllib.error.HTTPError as http_error: | ||
if http_error.code == 404: | ||
return '' | ||
raise | ||
|
||
# Now try and parse the data. | ||
data = json.loads(raw_json_data) | ||
first_definition = data[0]['meanings'][0]['definitions'][0]['definition'] | ||
|
||
# Return the result. | ||
return first_definition | ||
|
||
# Read standard input until there is nothing left to read. | ||
while True: | ||
# Read a line of input. | ||
word = sys.stdin.readline() | ||
|
||
# If the user hits 'Ctrl+D' to end transmission, readline returns an | ||
# empty string and we can stop reading. | ||
if not word: | ||
break | ||
|
||
# If the input is an empty line or whitespace, skip it. | ||
if word.isspace(): | ||
continue | ||
|
||
# Strip whitespace from the word and find the definition. | ||
word = word.strip() | ||
stripped_word = word.strip() | ||
definition = search_for_word(stripped_word) | ||
|
||
# Write the result. | ||
print("{} - {}".format(word, definition)) |