diff --git a/modules/src/__init__.py b/modules/src/__init__.py index bf004743..0ca9f5a4 100644 --- a/modules/src/__init__.py +++ b/modules/src/__init__.py @@ -18,6 +18,7 @@ 'quote', 'request', 'time', + 'urban', 'url', 'video', 'weather', diff --git a/modules/src/urban.py b/modules/src/urban.py new file mode 100644 index 00000000..2e0ea143 --- /dev/null +++ b/modules/src/urban.py @@ -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 + diff --git a/modules/tests/test_urban.py b/modules/tests/test_urban.py new file mode 100644 index 00000000..9a03d556 --- /dev/null +++ b/modules/tests/test_urban.py @@ -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])