Skip to content

Instantly share code, notes, and snippets.

@Zemaaan
Last active February 24, 2018 18:07
Show Gist options
  • Save Zemaaan/0faf7260c453a8bc9e2a5c7cbc91d821 to your computer and use it in GitHub Desktop.
Save Zemaaan/0faf7260c453a8bc9e2a5c7cbc91d821 to your computer and use it in GitHub Desktop.
# _*_ coding:utf-8 _*_
# version 0.3.1 Red Queen system
from urllib import request
import json
import webbrowser
import forecastio
from time import ctime
import wikipedia
import subprocess
__VOICE__ = True
__DATA_ERROR__ = 'something went wrong while fetching data.'
__GOD__ = False
__REMOTE_SERVER__ = 'www.google.com'
__NO_INTERNET__ = 'Internet connection not present.'
__GUI__ = 1
__VERSION__ = 'v0.3'
__MEMORY__ = []
__LATITUDE__ = 45.815011
__LONGITUDE__ = 15.981919
__WEATHER_KEY__ = 'e2f885c80f8b660f4bf04777e2324ee4' # WARNING : software will not work if directives are missing
__INDEX_ERROR__ = 'Please check you spelling.'
__EMPTY_ERROR__ = 'Please enter something.'
__NAME_ERROR__ = 'Something is undefined,obviously.'
__CHECK__ = False
__MINIMAL_CONFIDENCE__ = .5
__VOICE_TIMEOUT__ = 5
print('ready!')
while True:
user = input('').encode('utf-8', errors = 'strict').decode('utf-8', errors = 'strict')
if user == 'god': # very useful if something goes wrong
__GOD__ = True
while __GOD__:
print('Command Interface and Administrative Mode active.')
user = input('>')
text = user.replace(' ', '%20')
# @formatter:off
with request.urlopen('https://api.wit.ai/message?q=%s&access_token=UB26LLZLYNNPYAIKGSUWOMVBIF6MOJUA' % text) as response:
html = response.read()
# @formatter:on
html = html.decode()
data = json.loads(html)
confidence = data['outcomes'][0]['confidence']
intent = data['outcomes'][0]['intent']
# noinspection PyBroadException
try:
if intent == 'alarm':
time = data['outcomes'][0]['entities']['datetime'][0]['values'][0]['value']
print('time --> ', time)
print('confidence --> ' + confidence)
elif intent == 'info_on':
term = data['outcomes'][0]['entities']['wikipedia_search_query'][0]['value']
print('term --> ', term)
print('confidence --> ' + confidence)
elif intent == 'find_on_g_maps':
term = data['outcomes'][0]['entities']['contact'][0]['value']
print('term --> ', term)
print('confidence --> ' + confidence)
elif intent == 'weather':
print('intent --> ' + intent)
print('confidence --> ' + confidence)
elif intent == 'find_route_on_gmaps':
origin = data['outcomes'][0]['entities']['origin'][0]['value']
destination = data['outcomes'][0]['entities']['destination'][0]['value']
print('origin -->', origin)
print('destination -->', destination)
print('confidence --> ' + confidence)
elif intent == 'open_file':
filename = data['outcomes'][0]['entities']['file_name'][0]['value']
print('filename --> ' + filename)
print('confidence --> ' + confidence)
elif intent == 'find_antonym':
antonym = data['outcomes'][0]['entities']['antonym'][0]['value']
print('intent --> ' + intent)
print('antonym --> ' + antonym)
print('confidence --> ' + confidence)
except KeyError:
print('error while extracting information')
except:
print('something went wrong')
text = user.replace(' ', '%20')
with request.urlopen('https://api.wit.ai/message?q=%s&access_token=UB26LLZLYNNPYAIKGSUWOMVBIF6MOJUA' % text) as response:
html = response.read()
html = html.decode()
data = json.loads(html)
# noinspection PyRedeclaration
confidence = round(data['outcomes'][0]['confidence'], 2)
intent = data['outcomes'][0]['intent']
if intent == 'info_on':
term = data['outcomes'][0]['entities']['wikipedia_search_query'][0]['value']
summary = wikipedia.summary(term)
print(summary)
print('Should i save information in text file? yes/no')
save = input('')
if save == 'yes':
# noinspection PyBroadException
try:
file = open(term + '.txt', 'w')
file.write(summary)
except FileNotFoundError:
print('File not found')
except:
print('something went wrong with file writing')
else:
continue
elif intent == 'weather':
# noinspection PyBroadException
try:
forecast = forecastio.load_forecast(__WEATHER_KEY__, __LATITUDE__, __LONGITUDE__)
current = forecast.currently()
print(current.summary)
except:
print('Weather reporting fetching went wrong.')
elif intent == 'google':
search_term = data['outcomes'][0]['entities']['google_search_query'][0]['value']
print('Googling %s' % search_term)
webbrowser.open('https://www.google.com?q=%s' % search_term)
elif intent == 'shutdown':
quit()
elif intent == 'show_time':
ctime()
elif intent == 'find_route_on_gmaps':
origin = data['outcomes'][0]['entities']['origin'][0]['value']
destination = data['outcomes'][0]['entities']['destination'][0]['value']
webbrowser.open('https://www.google.hr/maps/dir/%s/%s/' % (origin, destination))
elif intent == 'open_file':
filename = data['outcomes'][0]['entities']['file_name'][0]['value']
file = open(filename + '.txt', 'a')
file.write('* textfile automatically generated by Red Queen *')
print(filename + '.txt was successfully created.')
elif intent == 'find_distance':
origin = data['outcomes'][0]['entities']['origin'][0]['value'].replace(' ', '%20') # FIXME : this needs to be done better
destination = data['outcomes'][0]['entities']['destination'][0]['value'].replace(' ', '%20') # FIXME : this needs to be done better
with request.urlopen('http://maps.googleapis.com/maps/api/distancematrix/json?origins=%s&destinations=%s&mode=driving&language=en-EN&sensor=false' % (origin, destination)) as response:
html = response.read()
html = html.decode()
information = json.loads(html)
origin = data['outcomes'][0]['entities']['origin'][0]['value'].replace('%20', ' ') # FIXME : this needs to be done better
destination = data['outcomes'][0]['entities']['destination'][0]['value'].replace('%20', ' ') # FIXME : this needs to be done better
length = information['rows'][0]['elements'][0]['distance']['text']
print('distance between ' + origin + ' and ' + destination + ' is ' + length)
elif intent == 'find_antonym':
try:
antonym = data['outcomes'][0]['entities']['antonym'][0]['value']
# @formatter:off
search_for_antonym_in_mysql_database = subprocess.Popen(['F:/xampp/php/php.exe', 'search_db.php', antonym, 'antonym'], stdout = subprocess.PIPE)
# @formatter:on
result = search_for_antonym_in_mysql_database.communicate()[0].decode()
print(antonym + ' stands for ' + result)
except KeyError:
print('Could not not fetch antonym from Wit.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment