-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
50 lines (43 loc) · 1.36 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from flask.wrappers import Response
from flask_cors import CORS
import json
from flask import Flask, jsonify
import boto3
from datetime import datetime
response=''
dynamo_client = boto3.Session(region_name='us-west-1').client('dynamodb')
def get_last24hrs_data(table):
# table=table
date=int(round(datetime.timestamp(datetime.now()),0))
response = dynamo_client.query(
TableName=table,
KeyConditionExpression='entry_type = :entry_type AND unix_time BETWEEN :dateprev AND :datenow',
ExpressionAttributeValues={
':entry_type':{'S': 'sample'},
':datenow': {'N': str(date)},
':dateprev': {'N': str(date - 86400)},
}
)
print(response['Items'])
return response
app = Flask(__name__)
CORS(app)
@app.route("/")
def homepage():
return(
f"Available Routes: <br/>"
f"/api/v1.0/aqi_last24hrs <br/>"
f"/api/v1.0/weather_last24hrs <br/>"
)
@app.route("/api/v1.0/aqi_last24hrs")
def aqi_last24hrs():
# get_last24hrs_data('aqi_hist_3')
# aqi_last24hrs = json.dumps(response,indent=4)
return get_last24hrs_data('aqi_hist_3')
@app.route("/api/v1.0/weather_last24hrs")
def weather_last24hrs():
# get_last24hrs_data('aqi_weather_3')
# weather_last24hrs = json.dumps(response,indent=4)
return get_last24hrs_data('aqi_weather_3')
if __name__ == '__main__':
app.run(debug=True)