-
Notifications
You must be signed in to change notification settings - Fork 0
/
vocabBot.py
56 lines (49 loc) · 2.32 KB
/
vocabBot.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
import praw
import requests
import json
class vocabBot:
#initializes praw instance
def __init__(self):
self.redditInstance = praw.Reddit('vocab', user_agent='Python/PC:vocabularyredditbot:v0.1')
#checks for unread mentions and proccesses new mentions
def proccessMentions(self):
for mention in self.redditInstance.inbox.mentions(limit=None):
if mention.new:
self.replyToMention(mention)
mention.mark_read()
#reads in current message and responds to it based on content
def replyToMention(self, mention):
content = mention.body
words = content.split()
reply = self.makeMessage(words)
mention.reply(reply)
#takes message content and parses out target word
def makeMessage(self,words):
message = self.getDefinitions(words[0])
return message
#passes target word to dictionary api and parses and formats definitions
def getDefinitions(self, word):
with open('oxford.json') as f:
app_info = json.load(f)
language = 'en'
url = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/' + language + '/' + word.lower()
response = requests.get(url, headers = {'app_id': app_info["app_id"], 'app_key': app_info["app_key"]})
definitions = ""
index = 0
if (response.status_code==200):
if 'results' in response.json():
for result in response.json()['results']:
for lexentry in result['lexicalEntries']:
for entry in lexentry['entries']:
for sense in entry['senses']:
index+= 1
subIndex=0
for definition in sense['definitions']:
definitions+= str(index)+": "+definition+"\n\n"
if 'subsenses' in sense:
for subsense in sense['subsenses']:
for definition in subsense['definitions']:
subIndex+=1
definitions+= " "+str(index)+"."+str(subIndex)+": "+definition+"\n\n"
return definitions
return "Word not found"