这是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
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
GOODREADS_ACCESS_TOKEN = '<GOODREADS_ACCESS_TOKEN>'
WORDS_API_KEY = '<WORDS_API_KEY>'
JOKES_SOURCE_FILE = 'data/jokes.json'
MAPQUEST_ACCESS_TOKEN = 'CcNE2f13xlbD9bMBYzWHsyasWeyGDfwA'
TIME_ZONE_ACCESS_TOKEN = 'M4K04AV6U6IT'
1 change: 1 addition & 0 deletions modules/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
'movie',
'quote',
'request',
'time',
'wiki',
]
24 changes: 24 additions & 0 deletions modules/src/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import requests
import config
import os
from templates.text import TextTemplate
from datetime import datetime

MAPQUEST_ACCESS_TOKEN = os.environ.get('MAPQUEST_ACCESS_TOKEN', config.MAPQUEST_ACCESS_TOKEN)
TIME_ZONE_ACCESS_TOKEN = os.environ.get('TIME_ZONE_ACCESS_TOKEN', config.TIME_ZONE_ACCESS_TOKEN)

def process(input, entities):
output = {}
try:
l = requests.get('http://open.mapquestapi.com/nominatim/v1/search.php?key=' + MAPQUEST_ACCESS_TOKEN + '&format=json&q='+ entities['time'][0]['value'] + '&addressdetails=0&limit=1')
location = l.json()
r = requests.get('http://api.timezonedb.com/?lat='+ location[0]['lat'] + '&lng='+ location[0]['lon'] + '&format=json&key=' + TIME_ZONE_ACCESS_TOKEN)
time_data = r.json()
the_time = datetime.utcfromtimestamp(time_data['timestamp']).strftime('%Y-%m-%d %H:%M:%S')
output['input'] = input
output['output'] = TextTemplate('Location: ' + location[0]['display_name'] + '\nTime: ' + the_time + '\nTime Zone: ' + time_data['abbreviation']).get_message()
output['success'] = True
except:
output['success'] = False

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

def test_time():

assert('time' == modules.process_query('time in new york')[0])
assert('time' == modules.process_query('beijing time')[0])
assert('time' == modules.process_query('time at paris')[0])
assert('time' != modules.process_query('something random')[0])