box = box.BoxApi() box_params = {} box_params["folder_id"] = folder_id box_params["filename"] = file.split(os.pathsep)[-1] if options.verbosity >= 2: sys.stderr.write("DEBUG: box_params: %s\n" % box_params) try: f = open(file, 'rb') except: sys.stderr.write("ERROR: Could not open file.\n") sys.exit(1) r = box.request("POST", "https://upload.box.com/api/2.0/files/content", append_url=False, data=box_params, files={"file": f}) rj = r.json() #print r, r.text if options.verbosity >= 2: sys.stderr.write("DEBUG: status: %d\n" % r.status_code) try: json_str = json.dumps(r.json(), indent=True) for line in json_str.split("\n"): sys.stderr.write("DEBUG: resonse: %s\n" % line) except: sys.stderr.write("DEBUG: WARNING: could not cleanly display response data: %s\n" % r.text) if r.status_code != 201: sys.stderr.write("ERROR: ") try: sys.stderr.write(rj["message"])
box_params["limit"] = config.get("user_lookup", "box_request_limit") except: box_params["limit"] = 100 box_params["offset"] = 0 fetched_count = 0 storage_list = [] while True: for i in range(6): if i != 0: time.sleep(2**i) try: #request users get_box_users = box.request("GET", "/users", params=box_params) break except: #re-initialize box object with current auth token box = box.BoxApi() if get_box_users.status_code != requests.codes.ok: logger.warn("Response code \"%d \"from \"%s\"\n" % (get_box_users.status_code, get_box_users.url)) body = get_box_users.text box_users = json.loads(body) if len(box_users["entries"]) < 1: break fetched_count += len(box_users["entries"]) logger.info("Fetched %d out of %d." % (fetched_count, box_users["total_count"])) for box_user in box_users["entries"]: username = box_user["login"].split("@")[0].lower()
box_params["limit"] = config.get("user_lookup", "box_request_limit") except: box_params["limit"] = 100 box_params["offset"] = 0 fetched_count = 0 storage_list = [] while True: for i in range(6): if i != 0: time.sleep(2**i) try: #request users get_box_users = box.request("GET", "/users", params=box_params) break except: #re-initialize box object with current auth token box = box.BoxApi() if get_box_users.status_code != requests.codes.ok: logger.warn("Response code \"%d \"from \"%s\"\n" % (get_box_users.status_code, get_box_users.url)) body = get_box_users.text box_users = json.loads(body) if len(box_users["entries"]) < 1: break fetched_count += len(box_users["entries"]) logger.info("Fetched %d out of %d." % (fetched_count, box_users["total_count"]))
"staff": re.compile(r"(Regular|Temporary)Staff(AA|DBRN|FLNT)"), "faculty": re.compile(r"Faculty(AA|DBRN|FLNT)"), "sponsored": re.compile(r"SponsoredAffiliate(AA|DBNR|FLNT)") } types = ("user", "shared") storage = ([], []) affiliations = {} roles = dict.fromkeys(role_rules.keys(), 0) ids = [] for attr_values in infile: attrs = dict(zip(headers, attr_values)) id = attrs["box_id"] if attrs["box_account_type"].lower() == "shared": ids.append(id) for id in ids: data = json.dumps({ "user": {"id": id}, "group": {"id": group_id, "role": "member"}}) if options.dry_run: print data else: r = box.request("POST", "/group_memberships", data=data) if r.status_code == 201: print "User ID %s added to group." % id elif r.status_code == 409: print "User ID %s NOT added to group already exists." % id else: print "WARNING: Received an unexpected response:" print r.text