There could be many reasons why someone may want to keep track of earthquakes in the near by area. So, for statistical reasons i wanted to keep a record of mine. I used Python this time to retrieve and manipulate the data, as it gives me many options, for the future to manipulate the data.
The way, i used, to get the info is simple and accurate, cause it uses an RSS feed from an institute in my country, which keeps logs of earthquakes. So the idea is:
- Get the RSS feed with the data
- Retrieve data (coordinates, magnitude)
- Check if the event is near me
- Check if i have recorded this event in the past
- Report info at the terminal.
To retrieve the data i used requests
and beautifulsoup
to extract/find the data. A SHA256 was created for each found entry, which is compared with previous stored checksums, to check if this event has been reported, cause when we get the RSS feed, older events are being downloaded again and again. To keep the stored event list short, we check if the size is more than a number (ex. 500) and if so, we truncate the list and store the new events at the bottom of the list. The RSS feed doesn't give a big number of events and per day there aren't happening more than 500 earthquakes near by... so we are sure that the list containing the checksums, will contain data for about 2-5 days old. There is no reason to keep more than that, just to check if the earthquake is new or not.
The script doesn't do anything more. From this point, you can decide what to do with the data. Do you want to just get an email/notification whenever an earthquake occurs? Would you like to store the magnitudes and keep a record of them, for statistical use? You could also use it to send an email/sms to family if an earthquake is bigger than a size you specify.
Hope you find it useful. If so, don't forget to leave a comment on how you use the data :)
#!/usr/bin/python3
import requests
from bs4 import BeautifulSoup
import re
import os
import hashlib
# you can enter the center of the area you want or specify top/left/down/right
# coordinates below.
# Athens
lat = 38.00
lon = 23.70
dist = 0.50
# These coordinates represent a box around your target area. Use decimal
# coordinates.
left=lon - dist #22.50
up= lat + dist #38.50
right=lon + dist #24.60
down= lat - dist # 37.50
filename="eqnearu.dat"
# list to store hashes
eq = []
# calculate sha256 checksum
def sha256(data):
# Convert data to bytes if it’s not already
if isinstance(data, str):
data = data.encode()
# Calculate SHA-256 hash
sha256_hash = hashlib.sha256(data).hexdigest()
return sha256_hash
# load old hashes to list
def loadpasteq():
if not os.path.isfile(filename):
return []
with open(filename) as f:
lines = f.read().splitlines()
return lines
# save hash list to file
def saveeq(lst):
with open(filename, "w") as outfile:
outfile.write("\n".join(lst))
# load old hashes
eq = loadpasteq()
# get RSS feed with earthquake info
rss_url = 'http://www.geophysics.geol.uoa.gr/stations/maps/seismicity.xml'
response = requests.get(rss_url)
if response.status_code == 200:
rss_feed = response.text
soup = BeautifulSoup(rss_feed, features="xml")
entries = soup.find_all('item')
# check each entry for coordinates and check if are inside our box
for entry in entries:
desc = entry.find('description')
items = desc.text.split(';')
x = re.search("Latitude: ......",items[0])
lat = float(x.group(0).split()[1].strip()[:-1])
x = re.search("Longitude: ......",items[0])
lon = float(x.group(0).split()[1].strip()[:-1])
# are they inside the target area?
if down <= lat and lat <= up:
if left <= lon and lon <= right:
# calculate checksum
sha = sha256(entry.text)
# if this is a new earthquake / not stored in our file
# show the data or you could send an email, notification etc.
if not (sha in eq):
eq.append(sha)
print("-"*40)
print(entry.text)
# get the magnitude, in case we wont to report big earthquakes
m = re.search("M ...,",entry.text)
#if m: print("Magnitude: " + m.group(0).split()[1][:-1])
print("-"*40)
# to keep the list short, just keep the last 500 events
if len(eq)>500:
eq = eq[-500:]
saveeq(eq)
else:
print("Failed to get RSS feed. Status code:", response.status_code)