listen.py 1.45 KB
Newer Older
Nick Bearson's avatar
Nick Bearson committed
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
#!/usr/bin/env python
import websocket

import re, os
import json
import argparse
from subprocess import call

FILERE = re.compile(r'LC[0-9]{14}LGN00.zip')
BIN = os.path.dirname(os.path.abspath(__file__))
PROCESS = os.path.join(BIN, "process.sh")

def on_message(ws, message):
    js = json.loads(message)    
    print "scene   : %s" % js['properties']['metadata']['scene_id']
    print "Filename: %s" % js['name']
    print "Size    : %s" % js['size']
    print "URL     : %s" % js['selfRef']

    if (FILERE.match(js['name'])):
        print "\n### Matching filename found, processing...."
        try:
            results = call([PROCESS, js['name'], js['selfRef']])
        except Exception as e:
            print "ERROR calling process.sh %s" % e

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    pass

if __name__ == "__main__":

    parser = argparse.ArgumentParser(
        description="Listen for and then process a particular LANDSAT scene")
    parser.add_argument('-H', '--host', type=str, help='The Exnode service',
                        default="ws://dev.incntre.iu.edu:8888/subscribe/exnode")

    args = parser.parse_args()

    host = args.host

    ws = websocket.WebSocketApp(host,
                                on_message = on_message,
                                on_error = on_error,
                                on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()