Skip to content

Instantly share code, notes, and snippets.

@woohgit
Created September 5, 2017 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woohgit/d8507232a6a9938547acdb603a56b18d to your computer and use it in GitHub Desktop.
Save woohgit/d8507232a6a9938547acdb603a56b18d to your computer and use it in GitHub Desktop.
earthquake
import urllib
import json
import uuid
from will.plugin import WillPlugin
from will.decorators import periodic
def get_magnitude_text_and_color_and_classification(magnitude):
"""
Great 8 or more
Major 7 - 7.9
Strong 6 - 6.9
Moderate 5 - 5.9
Light 4 - 4.9
Minor 3 -3.9
"""
value = float(magnitude)
if value < 3.9:
return "%s" % value, None, "minor"
elif value > 3.9 and value <= 4.9:
return "%s" % value, "lozenge-success", "light"
elif value > 4.9 and value <= 5.9:
return "%s" % value, "lozenge-new", "moderate"
elif value > 5.9 and value <= 6.9:
return "%s" % value, "lozenge-moved", "strong"
elif value > 6.9 and value <= 7.9:
return "%s" % value, "lozenge-current", "major"
elif value > 7.9:
return "%s" % value, "lozenge-error", "great"
else:
return "unknown", None
class EarthQuakeNotifier(WillPlugin):
@periodic(hour='*', minute='*/2')
def get_alerts(self):
url = "http://earthquake-report.com/feeds/recent-eq?json"
last_known_date = self.load("last_jishin_alert")
try:
entries = json.loads(urllib.urlopen(url).read())
latest = last_known_date
for entry in entries:
if entry['date_time'] > last_known_date and "JAPAN" in entry['location'].upper():
card = self.build_card(entry)
self.say(entry['title'], card=card)
if entry['date_time'] > latest:
latest = entry['date_time']
self.save("last_jishin_alert", latest)
except Exception as e:
print("feedparser exception %s" % e)
def build_card(self, jishin):
mag_text, mag_color, mag_class = get_magnitude_text_and_color_and_classification(jishin["magnitude"])
card = {
"id": str(uuid.uuid4()),
"icon": {"url": "http://icooon-mono.com/i/icon_15889/icon_158890_256.png"},
"url": jishin["link"],
"style": "application",
"format": "medium",
"attributes": [
{
"value": {
"label": jishin["location"].lower()
},
"label": "Location"
},
{
"value": {
"label": "%s %s" % (jishin["date_time"].split("T")[0],
jishin["date_time"].split("T")[1].split("+")[0])
},
"label": "Date"
},
{
"value": {
"label": mag_text
},
"label": "Magnitude"
},
{
"value": {
"style": mag_color,
"label": mag_class
},
"label": "Class"
}
],
"title": jishin["title"],
"activity": {
"html": 'Earthquake - %s' % jishin["location"].lower(),
"icon": "http://icooon-mono.com/i/icon_15889/icon_158890_256.png"
},
}
return card
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment