Created
August 21, 2017 05:28
-
-
Save woohgit/7f217da98db7fe5316128b0dd127b50c to your computer and use it in GitHub Desktop.
Earthquake report - HipChat Card style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
import json | |
import uuid | |
def get_magnitude_text_and_color(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 minor" % value, None | |
elif value > 3.9 and value <= 4.9: | |
return "%s light" % value, "lozenge-success" | |
elif value > 4.9 and value <= 5.9: | |
return "%s moderate" % value, "lozenge-new" | |
elif value > 5.9 and value <= 6.9: | |
return "%s strong" % value, "lozenge-moved", | |
elif value > 6.9 and value <= 7.9: | |
return "%s major" % value, "lozenge-current" | |
elif value > 7.9: | |
return "%s great" % value, "lozenge-error" | |
else: | |
return "unknown", None | |
def report_earthquake(): | |
jishin = None | |
url = "http://earthquake-report.com/feeds/recent-eq?json" | |
try: | |
entries = json.loads(urllib.urlopen(url).read()) | |
for entry in entries: | |
if "JAPAN" in entry['location'].upper(): | |
jishin = entry | |
break | |
except Exception: | |
pass | |
if jishin is not None: | |
mag_text, mag_color = get_magnitude_text_and_color(jishin["magnitude"]) | |
card1 = { | |
"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": { | |
"style": mag_color, | |
"label": mag_text | |
}, | |
"label": "Magnitude"} | |
], | |
"title": jishin["title"], | |
"activity": { | |
"html": "Earthquake - %s" % jishin["location"].lower(), | |
"icon": "http://icooon-mono.com/i/icon_15889/icon_158890_256.png" | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment