def get_parsed_data(bucket, prefix): resp = config.S3_CLIENT.list_objects(Bucket=bucket, Prefix=prefix) logger.info('parsed overview files') logger.info(resp) files = resp.get('Contents', []) return [f['Key'].replace(prefix, '').split('-')[:2] for f in files]
def message(): response = '<?xml version="1.0" encoding="UTF-8" ?><Response></Response>' form_data = {key: val for key, val in request.form.items()} logger.info("Got new Twilio message: {!r}".format(form_data)) key = "raw/{}-{}.json".format(form_data["From"], form_data["MessageSid"]) config.S3_CLIENT.put_object( Key=key, Bucket=config.S3_MESSAGE_BUCKET, Body=json.dumps(form_data), ContentType="application/json" ) return response, 200, {"content-type": "application/xml"}
def read_s3(bucket, key): try: response = config.S3_CLIENT.get_object(Bucket=bucket, Key=key) raw_body = response['Body'].read() logger.info('Processing data: {}'.format(raw_body)) body = json.loads(raw_body) except Exception: logger.exception('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket)) raise return body
def send_phone_leaderboard(data): tickers = [d[1] for d in data] counter = collections.Counter(tickers) analytics_data = [{'value': count, 'label': ticker} for ticker, count in counter.most_common()] request_data = { 'api_key': GECKOBOARD_API_KEY, 'data': { 'items': analytics_data } } logger.info('Sending data to geckoboard') logger.info(json.dumps(request_data, indent=2)) resp = requests.post('https://push.geckoboard.com/v1/send/166608-0e9d28b7-40df-40db-959a-bba383c84ec3', json=request_data) resp.raise_for_status()
def send_stock_overview(data): tickers = [d[0] for d in data] counter = collections.Counter(tickers) analytics_data = [{'value': count, 'label': ticker} for ticker, count in counter.most_common()] request_data = { 'api_key': GECKOBOARD_API_KEY, 'data': { 'item': analytics_data } } logger.info('Sending data to geckoboard') logger.info(json.dumps(request_data, indent=2)) resp = requests.post('https://push.geckoboard.com/v1/send/166608-edb468d1-bea2-41fe-ae36-e45b326128aa', json=request_data) resp.raise_for_status()
def lookup_company(company): params = {'input': company} resp = requests.get(LOOKUP_API_URL, data=params, timeout=10) logger.info(resp.url) logger.info(resp.status_code) logger.info(resp.headers) logger.info(resp.text) # The stocks API doesn't actually return a failing status code, but we'll keep # this here in case there are network problems, etc. if not resp.ok: raise Exception('Unable to find info for "{}"'.format(company)) # Sometimes the same stock is listed on multiple exchanges. We can ignore those for the sake of this prototype. results = set((d['Symbol'], d['Name']) for d in resp.json()) return [{'symbol': symbol, 'name': name} for symbol, name in results]
def get_ticker_info(ticker): params = {'symbol': ticker} resp = requests.get(QUOTE_API_URL, data=params, timeout=10) logger.info(resp.url) logger.info(resp.status_code) logger.info(resp.headers) logger.info(resp.text) # The stocks API doesn't actually return a failing status code, but we'll keep # this here in case there are network problems, etc. if not resp.ok: raise Exception('Unable to find a symbol for "{}"'.format(ticker)) # This is how the stocks API responds when there's an error, they always report # a 200 status code elif 'Message' in resp.json(): raise Exception('Unable to find a symbol for "{}"'.format(ticker)) return resp.json()
def log_lambda(event, context): logger.info('Running {} v{}'.format(context.function_name, context.function_version)) logger.info('Event data: {}'.format(json.dumps(event, indent=2)))
def analyze(bucket): data = get_parsed_data(bucket, 'parsed-overview/') logger.info('beautiful stock data') logger.info(data) send_stock_overview(data) send_phone_leaderboard(data)
#!/usr/bin/env python import os import boto3 from lager import logger try: import local_config logger.info('Imported local config {}'.format(local_config)) except: pass S3_MESSAGE_BUCKET = 'hotstockbling.messages' SESSION = boto3.session.Session(aws_access_key_id=os.environ['AWS_ACCESS_KEY'], aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'], region_name='us-west-2') S3_CLIENT = SESSION.client('s3')