Browse Source

First commit. No wikipedia

master
Stephen Downward 2 years ago
parent
commit
cc95aae74d
1 changed files with 76 additions and 0 deletions
  1. +76
    -0
      main.py

+ 76
- 0
main.py View File

@@ -0,0 +1,76 @@
#!/usr/bin/python3

import wolframalpha
import sys, fcntl, os

#Change these
wa_appid = "" #Wolfram app id

EOL = "\r"
pkt_len = 200



wa_client = None
connected = True

def chunkstring(string, length):
return (string[0+i:length+i] for i in range(0, len(string), length))

def print_ax(str): #Formats for ax25d
str += EOL
for chunk in chunkstring(str, pkt_len):
sys.stdout.write(chunk)
sys.stdout.flush()



def help():
print_ax("Available commands:")
print_ax("ask <question>: Asks wolfram alpha (Uses internet. Do not rely on this in an emergency!)")
print_ax(" EX: ask Who is the prime minister of Canada")
print_ax("help: Display this message")
print_ax("bye: Disconnect from the host")

def ask(query): #Wolfram alpha
global wa_client
global wa_appid
if wa_client is None:
wa_client = wolframalpha.Client(wa_appid)
res = wa_client.query(query)
sys.stdout.write(next(res.results).text + EOL)
sys.stdout.flush()

#Set input to be non-blocking
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
readbuf = ""


#Greet user
print_ax("Welcome to the VA1QLE packet radio assistant v0.1. You can query Wolfram Alpha and Wikipedia here.")

#Show commands
help()

while connected:
readbuf += sys.stdin.read(4096)

splitInput = readbuf.split(EOL)
if(len(splitInput) > 1): #Stuff to process
for text in splitInput[:-1]: #There may be multiple commands
splitted = text.split(' ', 1)
if splitted[0] == 'help':
help()
elif splitted[0] == 'ask':
try:
ask(splitted[1])
except:
print_ax("Error connecting to wolfram alpha")
elif splitted[0] == 'bye':
print_ax("73 and Goodbye.")
connected = False
elif splitted[0] != '':
print_ax("ERROR: Command not found. Use 'help' to list available commands.")
readbuf = splitInput[-1] #Take what's left and put it back in the buffer

Loading…
Cancel
Save