zephyr_mirror: Rewrite to use the API.
(imported from commit 6d3bce15eb045f28897abb6ddcdbea8e10629a7d)
This commit is contained in:
parent
9c500c5b0d
commit
0bb3717079
|
@ -4,6 +4,7 @@ import urllib
|
||||||
import simplejson
|
import simplejson
|
||||||
from urllib2 import HTTPError
|
from urllib2 import HTTPError
|
||||||
import time
|
import time
|
||||||
|
import traceback
|
||||||
|
|
||||||
class HumbugAPI():
|
class HumbugAPI():
|
||||||
def __init__(self, email, api_key, verbose=False, site="https://app.humbughq.com"):
|
def __init__(self, email, api_key, verbose=False, site="https://app.humbughq.com"):
|
||||||
|
@ -33,8 +34,9 @@ class HumbugAPI():
|
||||||
max_message_id = None
|
max_message_id = None
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
if max_message_id is not None:
|
||||||
options["first"] = "0"
|
options["first"] = "0"
|
||||||
options["last"] = str(last_received)
|
options["last"] = str(max_message_id)
|
||||||
messages = self.get_messages(options)
|
messages = self.get_messages(options)
|
||||||
except HTTPError, e:
|
except HTTPError, e:
|
||||||
# 502/503 typically means the server was restarted; sleep
|
# 502/503 typically means the server was restarted; sleep
|
||||||
|
@ -42,7 +44,9 @@ class HumbugAPI():
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print "HTTP Error getting zephyrs; trying again soon."
|
print "HTTP Error getting zephyrs; trying again soon."
|
||||||
print e
|
print e
|
||||||
|
print traceback.format_exc()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
continue
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
# For other errors, just try again
|
# For other errors, just try again
|
||||||
print e
|
print e
|
||||||
|
|
|
@ -12,7 +12,7 @@ Example: print-messages --user=tabbott@humbughq.com --site=http://127.0.0.1:8000
|
||||||
parser = optparse.OptionParser(usage=usage)
|
parser = optparse.OptionParser(usage=usage)
|
||||||
parser.add_option('--site',
|
parser.add_option('--site',
|
||||||
dest='site',
|
dest='site',
|
||||||
default="https://app.humbughq.com/",
|
default="https://app.humbughq.com",
|
||||||
action='store')
|
action='store')
|
||||||
parser.add_option('--api-key',
|
parser.add_option('--api-key',
|
||||||
dest='api_key',
|
dest='api_key',
|
||||||
|
|
|
@ -16,9 +16,6 @@ from urllib2 import HTTPError
|
||||||
|
|
||||||
sys.path.append("/mit/tabbott/Public/python-zephyr/")
|
sys.path.append("/mit/tabbott/Public/python-zephyr/")
|
||||||
sys.path.append("/mit/tabbott/Public/python-zephyr/build/lib.linux-x86_64-2.6/")
|
sys.path.append("/mit/tabbott/Public/python-zephyr/build/lib.linux-x86_64-2.6/")
|
||||||
import zephyr
|
|
||||||
|
|
||||||
zephyr.init()
|
|
||||||
|
|
||||||
parser = optparse.OptionParser()
|
parser = optparse.OptionParser()
|
||||||
parser.add_option('--forward-class-messages',
|
parser.add_option('--forward-class-messages',
|
||||||
|
@ -41,8 +38,27 @@ parser.add_option('--forward-from-humbug',
|
||||||
dest='forward_to_humbug',
|
dest='forward_to_humbug',
|
||||||
default=True,
|
default=True,
|
||||||
action='store_false')
|
action='store_false')
|
||||||
|
parser.add_option('--site',
|
||||||
|
dest='site',
|
||||||
|
default="https://app.humbughq.com",
|
||||||
|
action='store')
|
||||||
|
parser.add_option('--api-key',
|
||||||
|
dest='api_key',
|
||||||
|
default="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||||
|
action='store')
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
|
sys.path.append(".")
|
||||||
|
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
|
||||||
|
import api.common
|
||||||
|
humbug_client = api.common.HumbugAPI(email=os.environ["USER"] + "@mit.edu",
|
||||||
|
api_key=options.api_key,
|
||||||
|
verbose=True,
|
||||||
|
site=options.site)
|
||||||
|
|
||||||
|
import zephyr
|
||||||
|
zephyr.init()
|
||||||
|
|
||||||
browser = None
|
browser = None
|
||||||
csrf_token = None
|
csrf_token = None
|
||||||
|
|
||||||
|
@ -245,17 +261,7 @@ def zephyr_to_humbug(options):
|
||||||
with open(log_file, 'a') as log:
|
with open(log_file, 'a') as log:
|
||||||
process_loop(log)
|
process_loop(log)
|
||||||
|
|
||||||
def get_new_humbugs(max_humbug_id):
|
def forward_to_zephyr(message):
|
||||||
browser.addheaders.append(('X-CSRFToken', csrf_token))
|
|
||||||
submit_hash = {"mit_sync_bot": 'yes'}
|
|
||||||
if max_humbug_id is not None:
|
|
||||||
submit_hash["first"] = str(0)
|
|
||||||
submit_hash["last"] = str(max_humbug_id)
|
|
||||||
submit_data = urllib.urlencode([(k, v.encode('utf-8')) for k,v in submit_hash.items()])
|
|
||||||
res = browser.open("https://app.humbughq.com/api/get_updates", submit_data)
|
|
||||||
return simplejson.loads(res.read())['zephyrs']
|
|
||||||
|
|
||||||
def send_zephyr(message):
|
|
||||||
zsig = u"%s\u200B" % (username_to_fullname(message["sender_email"]))
|
zsig = u"%s\u200B" % (username_to_fullname(message["sender_email"]))
|
||||||
if ' dot ' in zsig:
|
if ' dot ' in zsig:
|
||||||
print "ERROR! Couldn't compute zsig for %s!" % (message["sender_email"])
|
print "ERROR! Couldn't compute zsig for %s!" % (message["sender_email"])
|
||||||
|
@ -293,41 +299,18 @@ def send_zephyr(message):
|
||||||
zeph.setmessage(body)
|
zeph.setmessage(body)
|
||||||
zeph.send()
|
zeph.send()
|
||||||
|
|
||||||
|
def maybe_forward_to_zephyr(message):
|
||||||
|
if message["sender_email"] == os.environ["USER"] + "@mit.edu":
|
||||||
|
if float(message["timestamp"]) < float(datetime.datetime.now().strftime("%s")) - 5:
|
||||||
|
print "Alert! Out of order message!", message["timestamp"], datetime.datetime.now().strftime("%s")
|
||||||
|
return
|
||||||
|
forward_to_zephyr(message)
|
||||||
|
|
||||||
def humbug_to_zephyr(options):
|
def humbug_to_zephyr(options):
|
||||||
# Sync messages from zephyr to humbug
|
# Sync messages from zephyr to humbug
|
||||||
browser_login()
|
|
||||||
print "Starting syncing messages."
|
print "Starting syncing messages."
|
||||||
max_humbug_id = None
|
humbug_client.call_on_each_message(maybe_forward_to_zephyr,
|
||||||
while True:
|
options={"mit_sync_bot": 'yes'})
|
||||||
try:
|
|
||||||
humbugs = get_new_humbugs(max_humbug_id)
|
|
||||||
except HTTPError, e:
|
|
||||||
# 502/503 typically means the server was restarted; sleep
|
|
||||||
# a bit, then try again
|
|
||||||
print "Failed getting zephyrs; trying again in 5 seconds."
|
|
||||||
time.sleep(2)
|
|
||||||
if e.code == 401:
|
|
||||||
# 401 means digest auth failed -- we need to login again
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
browser_login()
|
|
||||||
except HTTPError, e:
|
|
||||||
print "Failed logging in; trying again in 10 seconds."
|
|
||||||
time.sleep(10)
|
|
||||||
continue
|
|
||||||
break
|
|
||||||
continue
|
|
||||||
except:
|
|
||||||
# For other errors, just try again
|
|
||||||
time.sleep(2)
|
|
||||||
continue
|
|
||||||
for humbug in humbugs:
|
|
||||||
max_humbug_id = max(max_humbug_id, humbug["id"])
|
|
||||||
if humbug["sender_email"] == os.environ["USER"] + "@mit.edu":
|
|
||||||
if float(humbug["timestamp"]) < float(datetime.datetime.now().strftime("%s")) - 5:
|
|
||||||
print "Alert! Out of order message!", humbug["timestamp"], datetime.datetime.now().strftime("%s")
|
|
||||||
continue
|
|
||||||
send_zephyr(humbug)
|
|
||||||
|
|
||||||
if options.forward_to_humbug:
|
if options.forward_to_humbug:
|
||||||
zephyr_to_humbug(options)
|
zephyr_to_humbug(options)
|
||||||
|
|
Loading…
Reference in a new issue