How I write Oxford dictionary command line application by python + shell script for linux user

How I write Oxford dictionary command line application by python + shell script for linux user

Play this article

Implement

In first version, i provide dictionary to translate English to English.

Step 1: Read and understand oxford API

In this step, we need determine what API we should use, what parameters which are required and response data structure (this is really important for parse response data phrase).

For more detail you can go to: https://developer.oxforddictionaries.com

Step 2: Implement by python

First, we need declare necessary python library and constant variable.

import requests
import json
import sys

# TODO: replace with your own app_id and app_key
app_id = ‘oxford-app-id’
app_key = ‘oxford-app-key’

language = ‘en’
word_id = sys.argv[1]

Next, we need create and send request URL to API which we need:

url = ‘https://od-api.oxforddictionaries.com:443/api/v1/entries/' + language + ‘/’ + word_id.lower()
r = requests.get(url, headers = {‘app_id’: app_id, ‘app_key’: app_key})

And then, we get the result, and format it to Json:

resultStr = ‘Key word: ‘ + word_id + ‘\n\n’

results = r.json()[‘results’]

Next, the important phrase is parse response data:

resultStr = ‘Key word: ‘ + word_id + ‘\n\n’

results = r.json()[‘results’]
for result in results:
lexicalEntries = result[‘lexicalEntries’]
lexicalEntriesStr = ‘’

for lexicalEntrie in lexicalEntries:
entries = lexicalEntrie[‘entries’]
lexicalCategoryStr = ‘Lexical category: ‘ + lexicalEntrie[‘lexicalCategory’] + ‘\n’
pronunciationsStr = ‘Pronunciations: ‘ + lexicalEntrie[‘pronunciations’][0][‘phoneticSpelling’] + ‘\n’
textStr = ‘Text: ‘ + lexicalEntrie[‘text’] + ‘\n’

lexicalEntrieStr = ‘ — — — — — — — — — — — — — — — — — — — — — — — — — — — — \n’
lexicalEntrieStr = lexicalEntrieStr + textStr + pronunciationsStr + lexicalCategoryStr + ‘\n’;

for entry in entries:
senses = entry[‘senses’]

for sense in senses:
if ‘definitions’ in sense:
definitions = sense[‘definitions’]
definitionStr = ‘’
for definition in definitions:
definitionStr = ‘* ‘ + definitionStr + definition + ‘\n’

lexicalEntrieStr = lexicalEntrieStr + definitionStr

if ‘examples’ in sense:
examples = sense[‘examples’]
exampleStr = ‘Examples: \n’
for example in examples:
exampleStr = exampleStr + example[‘text’] + ‘\n\n’

lexicalEntrieStr = lexicalEntrieStr + exampleStr
lexicalEntriesStr = lexicalEntriesStr + lexicalEntrieStr + ‘\n’

resultStr = resultStr + lexicalEntriesStr

print(resultStr)

This phrase, depend on what we want to result to display.

Step 3: Create execute file by shell script

This step we need execute python by shell script with input parameter

#!/bin/bash

python PATH “$1”

Step 4: Create setup file by shell script

In this step, we need allow user can enter their application API and secret keys which can get from: https://developer.oxforddictionaries.com

#!/bin/bash

dictFolder=~/.dict/
dictFile=~/.dict/dict.py

mkdir -p $dictFolder

cp dict.default dict
cp dict.py.default $dictFile

sed -i ‘s?PATH?’$dictFile’?’ dict

echo Go to https://developer.oxforddictionaries.com to register Oxford application
echo -n “Enter APP ID > “
read appId
echo -n “Enter APP Key > “
read appKey

sed -i s/”oxford-app-id”/$appId/g $dictFile
sed -i s/”oxford-app-key”/$appKey/g $dictFile

echo “Move dict to /usr/bin”
sudo cp dict /usr/bin/

And that all for implement.

Source code: https://github.com/tuanlc/oxford-CLI

How to write command line application by python + shell scrip