summaryrefslogtreecommitdiff
path: root/cron/jordan_handle.py
blob: 57c9f287098efa99ee2e0bf149e6e457944bbdb7 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import boto3
import json
import urllib3

import dateutil.parser as dp
import xml.etree.ElementTree as ET

from datetime import datetime, timedelta
from io import StringIO


BUCKET = 'imgs.geshem.space'

http = urllib3.PoolManager()
s3 = boto3.resource('s3')
client = boto3.client('s3')
yesterday = (datetime.utcnow().date() - timedelta(days=1)).strftime('%Y%m%d')


def jordan_update():
    SOURCE_XML = 'http://212.35.78.67:8383/data/212JOC_rain_intensity.dpsri_dBR.xml'
    IMG_PREFIX = 'jordan/'

    try:
        res = http.request('GET', SOURCE_XML).data.decode('utf-8')
    except:
        return 'Connection error'

    root = ET.fromstring(res)

    try:
        latest_imgs = client.list_objects_v2(Bucket=BUCKET, Prefix=IMG_PREFIX, StartAfter=IMG_PREFIX + yesterday)['Contents']
        latest_keys = [i['Key'] for i in latest_imgs]
    except KeyError:
        latest_keys = []

    for r in ['images140', 'images280']:
        imgs = sorted(maps_json[r].items(), key=lambda x: x[0], reverse=True)
        res = r[-3:]
        for ts, url in imgs:
            dt = datetime.strptime(ts, '%Y:%m:%d:%H:%M')
            d = dt.strftime('%Y%m%d')
            t = dt.strftime('%H%M')
            img = http.request('GET', url).data
            key = '{}{}/{}/{}.png'.format(IMG_PREFIX, d, t, res)

            if latest_keys:
                if key not in latest_keys:
                    client.put_object(Bucket=BUCKET, Key=key, Body=img, ContentType='image/png', CacheControl='public, max-age=31536000')
                    latest_keys.append(key)
                    response += 'Put {}, '.format(key)
                else:
                    response += 'Skipping {}, '.format(key)
            else:
                try:
                    _ = client.head_object(Bucket=BUCKET, Key=key)
                    response += 'Skipping {}, '.format(key)
                except:
                    client.put_object(Bucket=BUCKET, Key=key, Body=img)
                    latest_keys.append(key)
                    response += 'Put {}, '.format(key)

    if latest_keys:
        index = {}
        for r in ['220']:
            keys = sorted(list(filter(lambda k: k.endswith('{}.png'.format(r)), latest_keys)))[-10:]
            index[r] = keys
        client.put_object(Bucket=BUCKET, Key='jordan.json', Body=json.dumps(index),
                          ContentType='application/json', CacheControl='public, max-age=60')

    return response


def update(event, context):

    res = jordan_update()

    body = {
        'message': f'SUCCESS: {res}',
        'input': event
    }

    response = {
        'statusCode': 200,
        'body': json.dumps(body)
    }

    return response