Skip to content

Instantly share code, notes, and snippets.

@kebman
Last active July 27, 2021 11:18
Show Gist options
  • Save kebman/2d3520ae366802993ebb35c9eb01c493 to your computer and use it in GitHub Desktop.
Save kebman/2d3520ae366802993ebb35c9eb01c493 to your computer and use it in GitHub Desktop.
Get SSE lightning strikes from the Norwegian Meteorological Institute at https://lyn.met.no/events
#!/usr/bin/python3
from sseclient import SSEClient
import json
# get the stream
thundercloud = SSEClient('https://lyn.met.no/events')
# keep checking for new lighning strikes
for lightning in thundercloud:
# check if set is empty
if(lightning.data != None or lightning.data != ""):
# check for line breaks / new lines and act accordingly
if ('\n' in lightning.data):
# temporary list to dump data in
lines = []
# temporary list to dump json in
temp_list = []
# remove escapes and dumps into list
lines.extend(lightning.data.split())
# make json out of data
for line in lines:
json_obj = json.loads(line)
temp_list.extend(json_obj)
# print json in list
for i in temp_list:
print("Latitude: %a, Longitude: %a" % (i['Point'][1], i['Point'][0]))
else:
# if no linebreaks detected
# make json
data = json.loads(lightning.data)
# print json
print("Latitude: %a, Longitude: %a" % (data[0]['Point'][1], data[0]['Point'][0]))
else:
# if set is empty
print(": heartbeat")
@kebman
Copy link
Author

kebman commented Jul 26, 2021

Every so often I will get this error:

Traceback (most recent call last):
  File "\lyn_met_no_SSE_client.py", line 46, in <module>
    data = json.loads(lightning.data)
  File "\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I thought it might be due to an empty message, but I don't think that's it. Hope someone can help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment