-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (41 loc) · 1.7 KB
/
Copy pathapp.py
File metadata and controls
54 lines (41 loc) · 1.7 KB
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
51
52
53
54
import requests
import boto3
import json
from datetime import datetime
from decimal import Decimal
def getRiver(table, riverid):
# Get river levels csv
# r = requests.get(f'http://apps.sepa.org.uk/database/riverlevels/{riverid}.csv')
r = requests.get(f'https://www2.sepa.org.uk/HydroData/api/Level15/{riverid}?csv=true')
print('SEPA response status: ' + str(r.status_code))
if r.status_code != 200:
return print(f'Bad SEPA response status: {str(r.status_code)}')
print('Successful SEPA response...')
content = r.text.splitlines()
line_count = 0
with table.batch_writer(overwrite_by_pkeys=['monitoring-station-id', 'timestamp']) as batch:
for line in content:
if line_count > 6:
row = line.split(',')
dt = datetime.strptime(row[0],"%d/%m/%Y %H:%M:%S")
timestamp = dt.strftime( '%Y-%m-%d %H:%M:%S')
data = json.loads(json.dumps({
'monitoring-station-id': riverid,
'timestamp': timestamp,
'depth': round(float(row[1]), 2)}), parse_float=Decimal)
# Add to dynamodb table put batch
batch.put_item(Item=data)
line_count += 1
else:
line_count += 1
print("Batch writing complete")
def lambda_handler(event, context):
aws_session = boto3.Session(region_name = 'eu-west-1')
aws_db = aws_session.resource('dynamodb')
table = aws_db.Table('river-level-readings')
riverid = event.get('riverid')
getRiver(table, riverid)
return {
'statusCode': 200,
'body': json.dumps('River-levels lambda has run!')
}