diff --git a/config.py b/config.py index d5c8e5e8..97144f12 100644 --- a/config.py +++ b/config.py @@ -6,3 +6,6 @@ JOKES_SOURCE_FILE = 'data/jokes.json' MAPQUEST_CONSUMER_KEY = '' TIME_ZONE_DB_API_KEY = '' + + +GOOGLE_URL_SHORTENER = '' diff --git a/modules/src/__init__.py b/modules/src/__init__.py index 956e3df9..9da5d15f 100644 --- a/modules/src/__init__.py +++ b/modules/src/__init__.py @@ -13,4 +13,5 @@ 'request', 'time', 'wiki', + 'url', ] diff --git a/modules/src/url.py b/modules/src/url.py new file mode 100644 index 00000000..239dd380 --- /dev/null +++ b/modules/src/url.py @@ -0,0 +1,24 @@ +import requests +import config +import os +import json +from templates.text import TextTemplate + + +GOOGLE_URL_SHORTENER = os.environ.get('GOOGLE_URL_SHORTENER', config.GOOGLE_URL_SHORTENER) + + +def process(input, entities=None): + output = {} + try: + long_url = entities['word'][0]['value'] + headers = {'content-type': 'application/json'} + r = requests.post('https://www.googleapis.com/urlshortener/v1/url?key=' + GOOGLE_URL_SHORTENER, data=json.dumps({'longUrl': long_url}), headers=headers) + data = r.json() + + output['input'] = input + output['output'] = TextTemplate(data['id']).get_message() + output['success'] = True + except: + output['success'] = False + return output \ No newline at end of file diff --git a/modules/tests/test_url.py b/modules/tests/test_url.py new file mode 100644 index 00000000..230bc5ad --- /dev/null +++ b/modules/tests/test_url.py @@ -0,0 +1,7 @@ +import modules + +def test_utl(): + assert('url' == modules.process_query('help shorten google.com')[0]) + assert('url' == modules.process_query('google.com shortener')[0]) + assert('url' == modules.process_query('give me a short version of google.com')[0]) + assert('url' != modules.process_query('something random')[0])