-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShellDictionary.py
75 lines (61 loc) · 2.15 KB
/
ShellDictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from bs4 import BeautifulSoup
import requests
import sys
import shlex
import subprocess
if len(sys.argv) >= 2:
word = sys.argv[1].lower()
else:
word = 'nothing'
def get_definition(word):
'''
gets the definition of the argument word from the merriam-webster
exception not handled
'''
s = BeautifulSoup(requests.get('http://www.merriam-webster.com/dictionary/'+word).text)
l=s.find('ul', attrs={'class':"definition-list no-count"}).find_all('li')
for item in l:
print item.text
def get_free_dictionary_definition(word):
'''
gets the definition of the argument word from thefreedictionary.com
'''
s = BeautifulSoup( requests.get( 'http://www.thefreedictionary.com/' + word).text)
div_id_def = s.find('div', attrs={'id':"Definition"})
definition = ''
try:
all_defs = div_id_def.find_all('div', attrs={'class':'ds-list'})
for item in all_defs:
definition += item.text + '\n'
except:
definition = 'Are you sure that\'s an English word? HINT: check spelling.'
return definition
def get_pronunciation(word):
'''
gets the pronounciation of word in mp3 format from link in cambridge
'''
resource = 'http://dictionary.cambridge.org/dictionary/english/'
r = requests.get(resource + word)
s = BeautifulSoup(r.text)
mp3_link_area = s.find(lambda tag: tag.has_attr('data-src-mp3'))
try:
mp3_link = mp3_link_area.get('data-src-mp3')
with open('temp.mp3', 'wb') as f:
f.write(requests.get(mp3_link).content)
command = subprocess.call(shlex.split('afplay temp.mp3'))
command = subprocess.call(shlex.split('rm -f temp.mp3'))
status = ''
except:
status = 'Sorry! Pronunciation not found!'
return status
def doit(fn, word):
'''
does nothing except returning function
'''
return fn(word)
if __name__ == '__main__':
definition = doit(get_free_dictionary_definition, word)
times_ = 10; print '==' * times_ + ' Definition : ' + word + ' ' + '==' * times_
print definition
status = doit(get_pronunciation, word)
print status