import json import boto3 from boto3.dynamodb.conditions import Key, Attr from decimal import * from datetime import datetime, timedelta from itertools import groupby from operator import itemgetter dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('wxData') s3 = boto3.client('s3') def get_datetime(s): y = int(s[0:4]) m = int(s[4:6]) d = int(s[6:8]) utc_dt = datetime(y, m, d, 0, 0, 0) adj_dt = 1000*((utc_dt - datetime(1970,1,1)).total_seconds()) est_dt = adj_dt - (5 * 60 * 60 * 1000) return est_dt def writeHTML_header(): hd = '' hd += '<html xmlns="http://www.w3.org/1999/xhtml" ><head>\n' hd += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n' hd += '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>\n' hd += '<script src="https://code.highcharts.com/highcharts.js"></script>\n' hd += '<script src="https://code.highcharts.com/highcharts-more.js"></script>\n' hd += '<script src="https://code.highcharts.com/modules/exporting.js"></script>\n' hd += '<link rel="stylesheet" type="text/css" href="wx.css">\n' hd += '</head><body><div id="container1"></div></body></html>\n' hd += '<script type=\"text/javascript\">Highcharts.chart(\'container1\', {\nchart: {type: \'line\', zoomType: \'x\'},\n\ttitle: { text: \'Travel Planning Temperature Data\', x: -20 },\n' hd += '\tsubtitle: {text: \'Daily Temperature Range\', x: -20 },\n\tcaption:{ text: \'Click on a city in the legend to toggle on/off\'},\n\txAxis: {type: \'datetime\', dateTimeLabelFormats: { day: \'%e %b\' },\n' hd += '\ttitle: { text: \'Date\' }}, \n\tyAxis: {\n\t\ttitle: { text: \'Temperature \' + String.fromCharCode(176) + \'F\' },\n' hd += '\t\tmin: 20, max: 100,\n\t\tplotLines: [{ value: 0, width: 0.5, color: \'#808080\' }] },\n' hd += '\ttooltip: {valueSuffix: String.fromCharCode(176) + \' F\'},\n\tlegend: {layout: \'vertical\', align: \'right\', verticalAlign: \'middle\', borderWidth: 0 },\n' hd += '\tplotOptions: {series: { marker: { enabled: false}, fillOpacity: 0.1 }},\n' hd += '\tseries: [\n\t{\n\t' return hd def updateOneCity(cName): print(cName) ud = '' response = table.scan(FilterExpression=Attr('city').eq(cName)) input = response['Items'] input_data = [] for V in input: changed = V['dt'] V['dt'] = changed[0:8] input_data.append(V) keyfunc = key = lambda i: i['dt'] data = sorted(input_data, key = keyfunc) lGroups = {} uniquekeys = [] for k, g in groupby(data, keyfunc): groups = [] groups.append(list(g)) # Store group iterator as a list lGroups[k] = groups ud += '\tname: \'{0}\', type: \'arearange\', data: [\n\t\t'.format(cName) idx = 0 for da in lGroups: for l in lGroups[da]: seq = [x['t'] for x in l] tmn = min(seq) tmx = max(seq) c_dt = get_datetime(da) if idx == 0: ud += '[{0},{1},{2}]'.format(int(c_dt), tmn, tmx) else: ud += ',[{0},{1},{2}]'.format(int(c_dt), tmn, tmx) idx = 1 ud += ']\n\t}' return ud def lambda_handler(event, context): #Get the service resource. bucket = 'data.nyporter.com' key = 'tvlwx2.html' fd = '' fd += writeHTML_header() idx = 1 for city in cities: fd += updateOneCity(city) if idx <= len(cities)-1: fd += ',{\n' else: fd += '\n]}\n);</script>\n</body></html>' idx += 1</html>' try: encodeData = fd.encode("utf-8") s3.put_object( Bucket=bucket, ACL='public-read', Key= key, Body=encodeData, ContentType='text/html' ) except Exception as e: print(e) raise e