Skip to content

Add Urban Dictionary Feature #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'quote',
'request',
'time',
'urban',
'url',
'video',
'weather',
Expand Down
43 changes: 43 additions & 0 deletions modules/src/urban.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import requests as rq
from bs4 import BeautifulSoup as bs
import re
import urllib

from templates.text import TextTemplate

def process(input, entities):
output = {}
try:
orig_phrase = entities['phrase'][0]['value']
phrase = urllib.quotes_plus(orig_phrase)
page = rq.get("http://www.urbandictionary.com/define.php?term="+phrase)
soup = bs(page.content, 'html.parser')
meaning = soup.find('div', class_='meaning').text
list_aux = meaning.split('.')
if list_aux[0] == "\nThere aren't any definitions for " + orig_phrase +"
yet":
meaning = "There aren't any definitions for "+orig_phrase+" yet on
Urban Dictionary."
else:
meaning = re.sub(''', '\'', meaning)
meaning = meaning[:min(len(meaning),300)]
meaning += "....\n"
meaning += "See the meaning at Urban Dictionary :-
http://www.urbandictionary.com/define.php?term=" + phrase
output['input'] = input
output['output'] = TextTemplate('Meaning of ' +
orig_phrase +
'from Urban Dictionary:\n' + meaning).get_message()
output['success'] = True

except:
error_message = 'I couldn\'t find the meaning of that phrase.'
error_message += '\nPlease ask me something else, like:'
error_message += '\nurban lol'
error_message += '\nlol urban'
error_message += '\nurbandictionary lol'
error_message += '\nslang lol'
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output

8 changes: 8 additions & 0 deletions modules/tests/test_urban.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import modules

def test_urban():
assert('urban' == modules.process_query('urban lol')[0])
assert('urban' == modules.process_query('lol urban')[0])
assert('urban' == modules.process_query('slang lol')[0])
assert('urban' == modules.process_query('urbandictionary lol')[0])
assert('urban' != modules.process_query('something random')[0])