def lambda_handler(event, context): pcd = get_postcode_from_event(event) if not pcd: return format_response({"error": "Postcode not found"}, 404) gss = get_gss(pcd) if not gss: return format_response({"error": "No data found for postcode"}, 404) try: hub = CommunityHub.get(gss) return format_response(hub.attribute_values, 200) except DoesNotExist: return format_response({"error": "No data found for this area"}, 404)
def lambda_handler(event, context): if "queryStringParameters" not in event or "type" not in event["queryStringParameters"]: return format_response({"error": "type must be one of 'shielding', 'vulnerable', or 'volunteering'"}, 400) if event["queryStringParameters"]["type"] not in ["shielding", "vulnerable", "volunteering"]: return format_response({"error": "type must be one of 'shielding', 'vulnerable', or 'volunteering'"}, 400) t = event["queryStringParameters"]["type"] # Exclude our Northern Ireland hack filters = CommunityHub.gss != "N07000001" if "nation" in event["queryStringParameters"]: if event["queryStringParameters"]["nation"] not in ["E", "N", "S", "W"]: return format_response({"error": "nation must be one of 'E', 'N', 'S' or 'W'"}, 400) else: filters = filters & CommunityHub.gss.startswith(event["queryStringParameters"]["nation"]) results = {} for item in CommunityHub.scan(filters): if t + "_url" in item.attribute_values: results[item.gss] = item.attribute_values[t + "_url"] return format_response(results, 200)
def grab_latest() -> List: """ Grab the latest [LIMIT] posts from subreddits inside [SUBREDDITS] and filter them down to just a handful of relevant fields and remove posts that have already been parsed in the past. """ # TODO: async requests, cuz fuggit posts = {} for sub in SUBREDDITS: url = _get_subreddit_url(sub) LOGGER.debug(f"Querying: {url}") response = get(url, headers={"User-Agent": USER_AGENT}) try: new_posts = parse_json_response(response.json()) except Exception as e: LOGGER.exception( f"Request to {url} failed with code {response.status_code}: {response.reason}" ) # We got some posts we haven't seen before. Let's filter through them if new_posts: posts[sub] = filter_results(new_posts, sub) subject = format_subject(posts) message = format_response(posts) if not message: LOGGER_RESULTS.info("No new posts.") return LOGGER_RESULTS.info(f"\n{message}") if EMAIL_NOTIFICATIONS: send_email(subject, message)
def lambda_handler(event, context): if "body" not in event: return format_response("Body not supplied", 400) try: if event["isBase64Encoded"]: data = json.loads(base64.b64decode(event["body"])) else: data = json.loads(event["body"]) except: raise return format_response("Body must be JSON", 400) if "token" not in data or data["token"] != os.environ.get("SECRET_TOKEN"): return format_response("Unauthorized", 403) if "gss" not in data: return format_response("GSS code must be supplied", 400) if "key" not in data: return format_response("Attribute must be supplied", 400) if "val" not in data: return format_response("Value must be supplied", 400) try: hub = CommunityHub.get(data["gss"]) except DoesNotExist: return format_response("GSS code not found", 404) if data["key"] in hub.attribute_values: old_value = hub.attribute_values[data["key"]] else: old_value = "" hub.attribute_values[data["key"]] = data["val"] hub.date_collected = datetime.datetime.today().strftime("%d/%m/%Y") hub.save() slack_fields = [ { "title": "GSS", "value": data["gss"], "short": True, }, { "title": "Attribute", "value": data["key"], "short": True, }, { "title": "Was", "value": old_value, "short": True, }, { "title": "Now", "value": hub.attribute_values[data["key"]], "short": True, }, ] msg = "Data updated for {}".format(hub.name) slack.post( text=msg, attachments=[{ "fallback": msg, "fields": slack_fields, }], )
def lambda_handler(event, context): if "body" not in event: return format_response("Body not supplied", 400) try: if event["isBase64Encoded"]: data = json.loads(base64.b64decode(event["body"])) else: data = json.loads(event["body"]) except: raise return format_response("Body must be JSON", 400) if "gss" not in data: return format_response("GSS code must be supplied", 400) if "pcd" not in data: return format_response("Postcode must be supplied", 400) try: hub = CommunityHub.get(data["gss"]) except DoesNotExist: return format_response("GSS code not found", 404) fields = [{ "title": "Name", "value": hub.name, "short": True, }, { "title": "Homepage URL", "value": hub.homepage_url, "short": True, }, { "title": "Phone Number", "value": hub.phone, "short": True, }, { "title": "Date Collected", "value": hub.date_collected, "short": True, }, { "title": "Hub URL", "value": hub.hub_url, "short": False, }, { "title": "Email", "value": hub.email, "short": True, }, { "title": "Notes", "value": hub.notes, "short": False, }] if "message" in data: fields.append({ "title": "Message from user", "value": data["message"], "short": False, }) msg = "Problem reported with the data for {}, from postcode {}".format( hub.name, data["pcd"]) slack.post( text=msg, attachments=[{ "fallback": msg, "text": "*Current data:*", "fields": fields, }], )