From 65bab6c927df4991e0ec74e2f8b024a91eadfd3c Mon Sep 17 00:00:00 2001 From: Nootan Ghimire Date: Sat, 20 Jan 2018 17:24:15 +0700 Subject: [PATCH] Isolate stuffS --- src/sources/Exceptions/NoResultException.py | 7 +++++++ src/sources/UrbanDictionary/__init__.py | 22 +++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/sources/Exceptions/NoResultException.py create mode 100644 src/sources/UrbanDictionary/__init__.py diff --git a/src/sources/Exceptions/NoResultException.py b/src/sources/Exceptions/NoResultException.py new file mode 100644 index 0000000..910e0e0 --- /dev/null +++ b/src/sources/Exceptions/NoResultException.py @@ -0,0 +1,7 @@ +class NoResultException(Exception): + def __init__(self, message): + self.messge = message + + def __str__(self): + return self.message + diff --git a/src/sources/UrbanDictionary/__init__.py b/src/sources/UrbanDictionary/__init__.py new file mode 100644 index 0000000..cdaa246 --- /dev/null +++ b/src/sources/UrbanDictionary/__init__.py @@ -0,0 +1,22 @@ +import requests +#import NotFoundException + +class UrbanDictionary(object): + def __init__(self): + self._name = 'Urban Dictionary' + self._apiBaseUrl = 'http://api.urbandictionary.com/v0/define?term=' + + def getMeaning(self, term): + callUrl = self._apiBaseUrl + term + result = requests.get(callUrl) + try: + answer = result.json() + except ValueError: + # @todo: log this in future + raise NotFoundException('We could not find the term') + if not answer["list"]: + raise NotFoundException('We could not find the term') + + return answer["list"][0]["definition"] + +