#!/usr/bin/python

import os, readline, requests
from bs4 import BeautifulSoup

def indent(text):
	return '\n'.join('    ' + x for x in text.split('\n'))

url = 'http://api.wolframalpha.com/v2/query'
fn = os.path.expanduser('~/.wash.appid')
try:
	appid = file(fn).read()
except:
	print '\033[93m-- No Wolfram|Alpha app ID'
	print 'If you do not already have one, go to:'
	print '  https://developer.wolframalpha.com/portal/apisignup.html\033[0m'
	while True:
		appid = raw_input('Please enter your app ID: ').strip()
		if len(appid) == 17 and appid[6] == '-':
			with file(fn, 'w') as fp:
				fp.write(appid)
			break
		else:
			print '\033[91mInvalid app ID format\033[0m'

while True:
	try:
		query = raw_input('W|A> ')
	except EOFError:
		print
		break
	except KeyboardInterrupt:
		print
		continue
	if query.strip() == '':
		continue
	xml = requests.get(url, params=dict(input=query, appid=appid, format='plaintext')).text

	bs = BeautifulSoup(xml, 'xml')
	if bs.queryresult['success'] == 'true':
		for pod in bs.find_all('pod'):
			if len(pod.plaintext.contents):
				print '  \033[1m\033[92m%s\033[0m' % pod['title']
				print indent('\n'.join(pod.plaintext.contents))
	else:
		print '\033[91mQuery failed'
		for tip in bs.find_all('tip'):
			print '\033[93m-', tip['text'], '\033[0m'
	print
