Convert zephyr_mirror humbug sending to use the API.

(imported from commit 948a12a1dca903c47b7427248688f2079c7eecdb)
This commit is contained in:
Tim Abbott 2012-10-03 16:32:50 -04:00
parent 9330142937
commit d2a43dd6e1
2 changed files with 13 additions and 60 deletions

8
mit_subs_list.py Normal file
View file

@ -0,0 +1,8 @@
subs_lists = {}
subs_lists['default'] = """\
""".split()
all_subs = set()
for sub_list in subs_lists.values():
for sub in sub_list:
all_subs.add(sub)

View file

@ -1,5 +1,4 @@
#!/usr/bin/python #!/usr/bin/python
import mechanize
import urllib import urllib
import sys import sys
import logging import logging
@ -59,76 +58,24 @@ humbug_client = api.common.HumbugAPI(email=os.environ["USER"] + "@mit.edu",
import zephyr import zephyr
zephyr.init() zephyr.init()
browser = None
csrf_token = None
def browser_login():
logger = logging.getLogger("mechanize")
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.INFO)
global browser
browser = mechanize.Browser()
browser.set_handle_robots(False)
## debugging code to consider
# browser.set_debug_http(True)
# browser.set_debug_responses(True)
# browser.set_debug_redirects(True)
# browser.set_handle_refresh(False)
browser.add_password("https://app.humbughq.com/", "tabbott", "xxxxxxxxxxxxxxxxx", "wiki")
browser.open("https://app.humbughq.com/")
browser.follow_link(text_regex="\s*Log in\s*")
browser.select_form(nr=0)
browser["username"] = os.environ["USER"] + "@mit.edu"
browser["password"] = os.environ["USER"]
global csrf_token
csrf_token = browser["csrfmiddlewaretoken"]
browser.submit()
def compute_humbug_username(zephyr_username): def compute_humbug_username(zephyr_username):
return zephyr_username.lower().split("@")[0] + "@mit.edu" return zephyr_username.lower().split("@")[0] + "@mit.edu"
def send_humbug(zeph): def send_humbug(zeph):
zeph["forged"] = "yes"
zeph["sender"] = compute_humbug_username(zeph["sender"]) zeph["sender"] = compute_humbug_username(zeph["sender"])
zeph['fullname'] = username_to_fullname(zeph['sender']) zeph['fullname'] = username_to_fullname(zeph['sender'])
zeph['shortname'] = zeph['sender'].split('@')[0] zeph['shortname'] = zeph['sender'].split('@')[0]
if "instance" in zeph: if "instance" in zeph:
zeph["instance"] = zeph["instance"][:30] zeph["instance"] = zeph["instance"][:30]
browser.addheaders.append(('X-CSRFToken', csrf_token))
humbug_data = []
for key in zeph.keys(): for key in zeph.keys():
if key == "zsig":
# Don't send zsigs to the Humbug server
continue
if isinstance(zeph[key], unicode): if isinstance(zeph[key], unicode):
val = zeph[key].encode("utf-8") zeph[key] = zeph[key].encode("utf-8")
elif isinstance(zeph[key], str): elif isinstance(zeph[key], str):
val = zeph[key].decode("utf-8") zeph[key] = zeph[key].decode("utf-8")
humbug_data.append((key, val))
try:
browser.open("https://app.humbughq.com/forge_message/", urllib.urlencode(humbug_data))
except HTTPError, e:
if e.code == 401:
# Digest auth failed; server was probably restarted; login in again
while True:
try:
browser_login()
except HTTPError, e:
print "Failed logging in; trying again in 10 seconds."
time.sleep(10)
continue
break
print "Auth failure; trying again after logging in a second time!"
browser.open("https://app.humbughq.com/forge_message/", urllib.urlencode(humbug_data))
else:
raise
humbug_client.send_message(zeph)
def fetch_fullname(username): def fetch_fullname(username):
try: try:
@ -227,8 +174,6 @@ def process_loop(log):
def zephyr_to_humbug(options): def zephyr_to_humbug(options):
browser_login()
import mit_subs_list import mit_subs_list
subs = zephyr.Subscriptions() subs = zephyr.Subscriptions()
if options.forward_class_messages: if options.forward_class_messages: