Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import os
- import webbrowser
- import pickle
- import requests
- import json
- import pyperclip
- import configparser
- import urllib3
- from multiprocessing import Queue
- def get_api():
- try:
- with open('api.pickle', 'rb') as f:
- app_id, app_key = pickle.load(f)
- except:
- app_id = input('TODO: Enter your own app_id: ')
- app_key = input('TODO: Enter your own app_key: ')
- with open('api.pickle', 'wb') as f:
- pickle.dump([app_id, app_key], f)
- return app_id, app_key
- def auth():
- language = 'en'
- url = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/' + language + '/' + word_id.lower() + '/regions=' + region
- r = requests.get(url, headers={'app_id': app_id, 'app_key': app_key})
- try:
- json_str = json.dumps(r.json())
- data = json.loads(json_str)
- return data
- except:
- print('Error')
- def get_lexical():
- lexical_list = []
- try:
- word = data['results'][0]['id']
- lexical_list.append(word)
- except:
- pass
- lexical_list.append('==========================================================================================')
- lexical = data['results'][0]['lexicalEntries']
- for d in lexical:
- try:
- lexicalCategory = (d['lexicalCategory'])
- lexical_list.append(lexicalCategory)
- try:
- phoneticSpelling = (d['pronunciations'][0]['phoneticSpelling'])
- lexical_list.append(phoneticSpelling)
- lexical_list.append('-----------------------------------------')
- except:
- pass
- except:
- pass
- lexical_list.append('==========================================================================================')
- return lexical_list
- def get_definitions():
- defenition_list = []
- try:
- definitions1 = data['results'][0]['lexicalEntries'][0]['entries'][0]['senses']
- for d in definitions1:
- try:
- domains = (d['domains'])
- defenition_list.append(domains)
- except:
- pass
- try:
- definitions = (d['definitions'])
- defenition_list.append(definitions)
- except:
- pass
- try:
- examples = (d['examples'][0]['text'])
- defenition_list.append(examples)
- defenition_list.append('-----------------------------------------')
- except:
- pass
- except:
- pass
- try:
- definitions2 = data['results'][0]['lexicalEntries'][0]['entries'][0]['senses'][0]['subsenses']
- for d in definitions2:
- try:
- domains = (d['domains'])
- defenition_list.append(domains)
- except:
- pass
- try:
- definitions = (d['definitions'])
- defenition_list.append(definitions)
- except:
- pass
- try:
- examples = (d['examples'][0]['text'])
- defenition_list.append(examples)
- defenition_list.append('-----------------------------------------')
- except:
- pass
- defenition_list.append('==========================================================================================')
- except:
- pass
- try:
- definitions3 = data['results'][0]['lexicalEntries'][1]['entries'][0]['senses']
- for d in definitions3:
- try:
- domains = (d['domains'])
- defenition_list.append(domains)
- except:
- pass
- try:
- definitions = (d['definitions'])
- defenition_list.append(definitions)
- except:
- pass
- try:
- examples = (d['examples'][0]['text'])
- defenition_list.append(examples)
- defenition_list.append('-----------------------------------------')
- except:
- pass
- except:
- pass
- try:
- definitions4 = data['results'][0]['lexicalEntries'][1]['entries'][0]['senses'][0]['subsenses']
- for d in definitions4:
- try:
- domains = (d['domains'])
- defenition_list.append(domains)
- except:
- pass
- try:
- definitions = (d['definitions'])
- defenition_list.append(definitions)
- except:
- pass
- try:
- examples = (d['examples'][0]['text'])
- defenition_list.append(examples)
- defenition_list.append('-----------------------------------------')
- except:
- pass
- defenition_list.append('==========================================================================================')
- except:
- pass
- try:
- definitions5 = data['results'][0]['lexicalEntries'][2]['entries'][0]['senses']
- for d in definitions5:
- try:
- domains = (d['domains'])
- defenition_list.append(domains)
- except:
- pass
- try:
- definitions = (d['definitions'])
- defenition_list.append(definitions)
- except:
- pass
- try:
- examples = (d['examples'][0]['text'])
- defenition_list.append(examples)
- defenition_list.append('-----------------------------------------')
- except:
- pass
- except:
- pass
- try:
- definitions6 = data['results'][0]['lexicalEntries'][2]['entries'][0]['senses'][0]['subsenses']
- for d in definitions6:
- try:
- domains = (d['domains'])
- defenition_list.append(domains)
- except:
- pass
- try:
- definitions = (d['definitions'])
- defenition_list.append(definitions)
- except:
- pass
- try:
- examples = (d['examples'][0]['text'])
- defenition_list.append(examples)
- defenition_list.append('-----------------------------------------')
- except:
- pass
- defenition_list.append('==========================================================================================')
- except:
- pass
- try:
- definitions7 = data['results'][0]['lexicalEntries'][2]['entries'][1]['senses']
- for d in definitions7:
- try:
- domains = (d['domains'])
- defenition_list.append(domains)
- except:
- pass
- try:
- definitions = (d['definitions'])
- defenition_list.append(definitions)
- except:
- pass
- try:
- examples = (d['examples'][0]['text'])
- defenition_list.append(examples)
- defenition_list.append('-----------------------------------------')
- except:
- pass
- except:
- pass
- try:
- definitions8 = data['results'][0]['lexicalEntries'][2]['entries'][1]['senses'][0]['subsenses']
- for d in definitions8:
- try:
- domains = (d['domains'])
- defenition_list.append(domains)
- except:
- pass
- try:
- definitions = (d['definitions'])
- defenition_list.append(definitions)
- except:
- pass
- try:
- examples = (d['examples'][0]['text'])
- defenition_list.append(examples)
- defenition_list.append('-----------------------------------------')
- except:
- pass
- defenition_list.append('==========================================================================================')
- except:
- pass
- return defenition_list
- def get_etymologies():
- etymologies_list = []
- try:
- etymologies = data['results'][0]['lexicalEntries'][0]['entries']
- for d in etymologies:
- etymologies = (d['etymologies'])
- etymologies_list.append(etymologies)
- except:
- pass
- try:
- etymologies = data['results'][0]['lexicalEntries'][1]['entries']
- for d in etymologies:
- etymologies = (d['etymologies'])
- etymologies_list.append(etymologies)
- except:
- pass
- try:
- etymologies = data['results'][0]['lexicalEntries'][2]['entries']
- for d in etymologies:
- etymologies = (d['etymologies'])
- etymologies_list.append(etymologies)
- except:
- pass
- try:
- etymologies = data['results'][0]['lexicalEntries'][2]['entries'][1]['etymologies']
- etymologies_list.append(etymologies)
- except:
- pass
- return etymologies_list
- def write_file():
- thefile = open('text_output.txt', 'w', encoding='utf8')
- for item in get_lexical():
- thefile.write(item + '\n')
- for item in get_definitions():
- thefile.write("%s\n" % item)
- for item in get_etymologies():
- thefile.write("%s\n" % item)
- def print_results():
- thefile = open('text_output.txt', 'r', encoding='utf8')
- text = thefile.readlines()
- print("\n".join(text))
- def copy_to_clipboard():
- thefile = open('text_output.txt', 'r', encoding='utf8')
- text = thefile.readlines()
- pyperclip.copy("\n".join(text))
- def open_google_image():
- url = 'https://www.google.com/search?tbm=isch&q=' + word_id
- webbrowser.open_new_tab(url)
- print('Please enter (chcp 65001) in the Windows console to display utf8 chars')
- input('Press Enter to continue...' )
- region_entry = input('Do you want to change the IPA region to United Kingdom?(y)(United States is default) ')
- if region_entry == 'y':
- region = 'gb'
- else:
- region = 'us'
- open_browser = input('Image search on Google(y)(n is default)')
- if open_browser == 'y':
- open_browser = 'y'
- else:
- open_browser = 'n'
- exit = ''
- get_api = get_api()
- app_id = get_api[0]
- app_key = get_api[1]
- while exit != 'q':
- os.system('cls')
- word_id = input('Enter a word: ')
- data = auth()
- if data:
- get_lexical()
- get_definitions()
- get_etymologies()
- write_file()
- print_results()
- copy_to_clipboard()
- if open_browser == 'y':
- open_google_image()
- exit = input("Press Enter to continue...(q - exit)")
- else:
- pass
Advertisement
Add Comment
Please, Sign In to add comment