Created
January 14, 2015 22:03
-
-
Save rfk/4e033d8c91acc33497a1 to your computer and use it in GitHub Desktop.
This file contains 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
import sys | |
import base64 | |
import getpass | |
import hashlib | |
import urllib2 | |
import optparse | |
import requests | |
def email_to_idn(addr): | |
"""Convert a UTF-8 email address to it's IDN (punycode) equivalent.""" | |
addr = urllib2.unquote(addr).decode('utf-8') | |
if '@' not in addr: | |
return addr | |
prefix, suffix = addr.split('@', 1) | |
return "%s@%s" % (prefix.encode('idna'), suffix.encode('idna')) | |
def main(argv): | |
parser = optparse.OptionParser(usage="usage: %prog [options] username") | |
parser.add_option("-p", "--password", | |
help="password for the sync1.1 user account") | |
parser.add_option("-H", "--host", | |
help="the sync1.1 hosting the user account") | |
(options, args) = parser.parse_args(argv) | |
if len(args) != 1: | |
parser.error("Incorrect number of arguments") | |
username = args[0] | |
if "@" in username: | |
username = email_to_idn(username).lower() | |
username = hashlib.sha1(username).digest() | |
username = base64.b32encode(username).lower() | |
if options.password is None: | |
options.password = getpass.getpass() | |
if options.host is None: | |
# Do cluster URL discovering against production server. | |
url = "https://auth.services.mozilla.com/user/1.0/{}/node/weave" | |
r = requests.get(url.format(username)) | |
r.raise_for_status() | |
options.host = r.content.strip() | |
while options.host.endswith("/"): | |
options.host = options.host[:-1] | |
url = options.host + "/1.1/{}/info/collections".format(username) | |
r = requests.get(url, auth=(username, options.password)) | |
r.raise_for_status() | |
print r | |
for hdr in r.headers: | |
print "{}: {}".format(hdr, r.headers[hdr]) | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment