def __init__(self, location, user_id=None): """Initialize user with location and user_id""" self.basecamp_location = None self.basecamp_name = None self.basecamp_set = False self.location = location self.user_id = user_id self.yakarma = None # Set endpoint to nearest server if required if settings.LOCATIONIZE_ENDPOINT: locationize_endpoint(self.location) # If no user ID specified, register new user if self.user_id is None: self.user_id = helper.generate_id(dashes=False, upper=True) # Do not register with Parse if APPID or CLIENTKEY is missing if None in [settings.PARSE_APPID, settings.PARSE_CLIENTKEY]: self.register(parse_register=False) else: self.register() # Log user ID in file if required if settings.LOG_USERIDS: with open("userids", "a") as userid_file: userid_file.write(self.user_id + "\n") # Update user properties from server self.update()
def register_user(user): """Return raw response data from registering user""" params = [("accuracy", user.location.accuracy), ("deviceID", generate_id(dashes=False, upper=True)), ("lat", user.location.latitude), ("long", user.location.longitude), ("token", get_token()), ("userID", user.user_id), ("userLat", user.location.latitude), ("userLong", user.location.longitude)] return _send("GET", settings.YIKYAK_ENDPOINT, "registerUser", params)
def upload_image(image): """Upload image to the AWS server""" # Create the object on the AWS server object_name = generate_id() response = _new_object(object_name) # Strip the query from the returned URL url = response.text[:response.text.find('?')] # Send the request auth = AWSAuth(settings.AWS_ACCESS_KEY, settings.AWS_SECRET_KEY, settings.AWS_BUCKET, object_name) files = {"file": image} return REQUEST("POST", url, files=files, auth=auth)
def register_user(user_id): """Register a user with Yik Yak's Parse service. Create a new installation and add user_id to it. Return installation ID and object ID""" # Installation ID iid = generate_id() # Create installation and check for errors response = _create_installation(iid) try: object_id = response.json()["result"]["data"]["objectId"] except (KeyError, ValueError): raise ParsingResponseError("Error creating installation", response) # Save user and check for errors and consistency response = _save_user(user_id, iid, object_id) try: if response.json()["result"]["data"]["channels"][0][1:-1] != user_id: raise ParsingResponseError("Error saving user", response) except (KeyError, ValueError): raise ParsingResponseError("Error saving user", response) return iid, object_id
def _send(method, data, iid): """Send data associated with an installation (ID: iid) to Yik Yak's Parse service using specified method. Return the response""" url = urljoin(settings.PARSE_ENDPOINT, method) data = { "classname": "_Installation", "data": data, "osVersion": settings.ANDROID_VERSION, "appBuildVersion": settings.PARSE_BUILD, "appDisplayVersion": settings.YIKYAK_VERSION, "v": settings.PARSE_VERSION_LETTER + settings.PARSE_VERSION, "iid": iid, "uuid": generate_id() } json_data = json.dumps(data) auth = OAuth1(settings.PARSE_APPID, settings.PARSE_CLIENTKEY) user_agent = "Parse Android SDK %s (com.yik.yak/%s) API Level %s" user_agent %= (settings.PARSE_VERSION, settings.PARSE_BUILD, settings.PARSE_API_LEVEL) headers = {"Accept-Encoding": "gzip", "User-Agent": user_agent} return REQUEST("POST", url, data=json_data, auth=auth, headers=headers)