def get_config(): """Return list of websites that are allowed""" key_list = [ "yikYakRepApplicationConfiguration", "endpoints", "threat_checks", "default_endpoint", "shareThreshold" ] try: response = GET(settings.CONFIG_URL) # Make sure all necessary keys are in dict if all(key in response.json()["configuration"] for key in key_list): return response.json()["configuration"] else: raise ParsingResponseError("Config settings missing", response) except ValueError: raise ParsingResponseError("Failed to get config settings", response)
def get_sites(): """Return list of websites that are allowed""" response = GET(settings.ALLOWED_SITES_URL) try: return response.json() except ValueError: raise ParsingResponseError("Failed to get allowed websites", response)
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 check_notif_error(raw): """Make sure the raw response from manipulating notifications does not contain an error""" try: if raw.json()["error"] != {}: return False else: return True except (KeyError, ValueError): raise ParsingResponseError("Error marking notifications", raw)
def set_basecamp(self, name, location=None): """Set the basecamp to location with name. Return True if successful, False if unsuccessful""" # Set location if not None, otherwise set to user's location location = location if location else self.location raw = yikyakapi.save_basecamp(self, name, location) self.update() try: return raw.json()["saveBasecamp"] except (KeyError, ValueError): raise ParsingResponseError("Setting basecamp failed", raw)
def update(self): """Update Yakarma and basecamp information""" raw = yikyakapi.get_messages(self, self.location, basecamp=True) # Check if too close to school self._get_yak_list(raw) try: self.yakarma = int(raw.json()["yakarma"]) except (KeyError, ValueError): raise ParsingResponseError("Getting Yakarma failed", raw) try: self.basecamp_set = bool(int(raw.json()["bcEligible"])) except (KeyError, ValueError): raise ParsingResponseError("Getting bcEligible failed", raw) try: latitude = float(raw.json()["bcLat"]) longitude = float(raw.json()["bcLong"]) self.basecamp_name = raw.json()["bcName"] self.basecamp_location = Location(latitude, longitude) except (KeyError, ValueError): pass
def _get_comment_list(self, raw_data): """Return list of comments from raw""" try: return [Comment(raw, self) for raw in raw_data.json()["comments"]] except (KeyError, ValueError): raise ParsingResponseError("Getting comment list failed", raw_data)
def _get_peek_location_list(self, raw_data, categ): """Return list of peek locations in category (categ) from raw""" try: return [PeekLocation(raw, self) for raw in raw_data.json()[categ]] except (KeyError, ValueError): raise ParsingResponseError("Getting peek list failed", raw_data)
def _get_notification_list(self, raw_data): """Return list of Yaks from raw server response""" try: return [Notification(raw, self) for raw in raw_data.json()["data"]] except (KeyError, ValueError): raise ParsingResponseError("Getting notifs failed", raw_data)