Created
September 20, 2012 18:05
-
-
Save aoisensi/3757426 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import re | |
import os | |
import sys | |
import json | |
import urllib | |
import urllib2 | |
import hashlib | |
bot_id = 'shoutbot' | |
bot_secret = 'UUOJ3DJRC5VwrPBwNxkG2leZZ0n' | |
bot_verifier = hashlib.sha1(bot_id + bot_secret).hexdigest() + bot_id + bot_secret | |
calc_url = 'http://www.google.co.jp/ig/calculator' | |
def main(): | |
content_length = int(os.environ['CONTENT_LENGTH']) | |
query = sys.stdin.read(content_length) | |
array = json.loads(query) | |
status = array['status'] | |
counter = array['counter'] | |
events = array['events'] | |
for event in events: | |
event_id = event['event_id'] | |
message = event['message'] | |
id = message['id'] | |
room = message['room'] | |
public_session_id = message['public_session_id'] | |
icon_url = message['icon_url'] | |
speaker_id = message['speaker_id'] | |
nickname = message['nickname'] | |
text = message['text'] | |
timestamp = message['timestamp'] | |
local_id = message['local_id'] | |
gcreg = re.compile(r'calc\(\S+\)') | |
formula = gcreg.search(text).group()[5:-1] | |
calc_param = urllib.urlencode({'q' : formula}) | |
calc_urle = calc_url + '?' + calc_param | |
calc_req = urllib2.urlopen(calc_urle) | |
calc_result = calc_req.read() | |
calc_re = re.compile('\{lhs:\s\\\"(.*)\\\",rhs:\s\\\"(\d+.?\d+)\\\",error:\s\\\"(.*)\\\",icc:\s(.*)\}') | |
calc_m = calc_re.match(formula) | |
print calc_m.group(2) | |
print 'test' | |
if __name__ == u'__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment