2012-10-01 15:36:44 -04:00
|
|
|
#!/usr/bin/python
|
|
|
|
import mechanize
|
|
|
|
import urllib
|
|
|
|
import simplejson
|
|
|
|
from urllib2 import HTTPError
|
|
|
|
import time
|
2012-10-02 15:47:59 -04:00
|
|
|
import traceback
|
2012-10-01 15:36:44 -04:00
|
|
|
|
2012-10-03 17:21:09 -04:00
|
|
|
# TODO: Just switch to pycurl
|
|
|
|
class NoHistory(object):
|
|
|
|
def add(self, *a, **k): pass
|
|
|
|
def clear(self): pass
|
|
|
|
|
2012-10-01 15:36:44 -04:00
|
|
|
class HumbugAPI():
|
|
|
|
def __init__(self, email, api_key, verbose=False, site="https://app.humbughq.com"):
|
2012-10-03 17:21:09 -04:00
|
|
|
self.browser = mechanize.Browser(history=NoHistory())
|
2012-10-01 15:36:44 -04:00
|
|
|
self.browser.set_handle_robots(False)
|
|
|
|
self.browser.add_password("https://app.humbughq.com/", "tabbott", "xxxxxxxxxxxxxxxxx", "wiki")
|
|
|
|
self.api_key = api_key
|
|
|
|
self.email = email
|
|
|
|
self.verbose = verbose
|
|
|
|
self.base_url = site
|
|
|
|
|
|
|
|
def send_message(self, submit_hash):
|
|
|
|
submit_hash["email"] = self.email
|
|
|
|
submit_hash["api-key"] = self.api_key
|
2012-10-03 17:53:34 -04:00
|
|
|
submit_data = urllib.urlencode([(k, v) for k,v in submit_hash.items()])
|
2012-10-01 15:36:44 -04:00
|
|
|
res = self.browser.open(self.base_url + "/api/v1/send_message", submit_data)
|
|
|
|
return simplejson.loads(res.read())
|
|
|
|
|
2012-10-02 16:18:58 -04:00
|
|
|
def get_messages(self, options = {}):
|
|
|
|
options["email"] = self.email
|
|
|
|
options["api-key"] = self.api_key
|
|
|
|
submit_data = urllib.urlencode([(k, v.encode('utf-8')) for k,v in options.items()])
|
2012-10-02 16:28:47 -04:00
|
|
|
res = self.browser.open(self.base_url + "/api/v1/get_messages", submit_data)
|
2012-10-03 15:34:39 -04:00
|
|
|
return simplejson.loads(res.read())['messages']
|
2012-10-01 15:36:44 -04:00
|
|
|
|
2012-10-02 16:18:58 -04:00
|
|
|
def call_on_each_message(self, callback, options = {}):
|
2012-10-01 15:36:44 -04:00
|
|
|
max_message_id = None
|
|
|
|
while True:
|
|
|
|
try:
|
2012-10-02 15:47:59 -04:00
|
|
|
if max_message_id is not None:
|
|
|
|
options["first"] = "0"
|
|
|
|
options["last"] = str(max_message_id)
|
2012-10-02 16:18:58 -04:00
|
|
|
messages = self.get_messages(options)
|
2012-10-01 15:36:44 -04:00
|
|
|
except HTTPError, e:
|
|
|
|
# 502/503 typically means the server was restarted; sleep
|
|
|
|
# a bit, then try again
|
|
|
|
if self.verbose:
|
|
|
|
print e
|
2012-10-03 15:34:39 -04:00
|
|
|
print "Error getting messages (probably server restart); retrying..."
|
2012-10-01 15:36:44 -04:00
|
|
|
time.sleep(1)
|
2012-10-02 15:47:59 -04:00
|
|
|
continue
|
2012-10-01 15:36:44 -04:00
|
|
|
except Exception, e:
|
|
|
|
# For other errors, just try again
|
2012-10-03 17:14:50 -04:00
|
|
|
if self.verbose:
|
|
|
|
print e
|
|
|
|
print traceback.format_exc()
|
|
|
|
print "Unexpected error! You should send Tim the above traceback. Retrying..."
|
2012-10-01 15:36:44 -04:00
|
|
|
time.sleep(2)
|
|
|
|
continue
|
|
|
|
for message in sorted(messages, key=lambda x: x["id"]):
|
|
|
|
max_message_id = max(max_message_id, message["id"])
|
|
|
|
callback(message)
|