这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
JOKES_SOURCE_FILE = 'data/jokes.json'
MAPQUEST_CONSUMER_KEY = '<MAPQUEST_CONSUMER_KEY>'
TIME_ZONE_DB_API_KEY = '<TIME_ZONE_DB_API_KEY>'


GOOGLE_URL_SHORTENER = '<GOOGLE_URL_SHORTENER>'
1 change: 1 addition & 0 deletions modules/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
'request',
'time',
'wiki',
'url',
]
24 changes: 24 additions & 0 deletions modules/src/url.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions modules/tests/test_url.py
Original file line number Diff line number Diff line change
@@ -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])