Facebook
From tbrady, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 45
  1. from urllib.request import *
  2. from json import *
  3. from datetime import *
  4. from operator import *
  5. import math
  6.  
  7. class Earthquake:
  8.    def __init__(self, place, mag, longitude, latitude, time):
  9.       # place - string
  10.       # mag - float
  11.       # longitude - float
  12.       # latitude - float
  13.       # time - int
  14.  
  15.       self.place = place
  16.       self.mag = mag
  17.       self.longitude = longitude
  18.       self.latitude = latitude
  19.       self.time = time
  20.  
  21.  
  22.  
  23.    def __eq__(self, other):
  24.       return self.place == other.place and math.isclose(self.mag, other.mag) and math.isclose(self.longitude, other.longitude) and math.isclose(self.latitude, other.latitude) and self.time == other.time
  25.  
  26. # Print object function. Print each object with correct formatting #
  27.  
  28. # Move the printEarthquake down because of function order #
  29. def getChoice():
  30.    print("Options:")
  31.    print("  (s)ort")
  32.    print("  (f)ilter")
  33.    print("  (n)ew quakes")
  34.    print("  (q)uit")
  35.    print("")
  36.    choice = input("Choice: ").lower()
  37.    return choice
  38.  
  39.  
  40.  
  41. def quake_from_feature(f):
  42.    place = f["properties"]["place"]
  43.    mag = f["properties"]["mag"]
  44.    longitude = f["geometry"]["coordinates"][0]
  45.    latitude = f["geometry"]["coordinates"][1]
  46.    badTime = f["properties"]["time"]
  47.    time = badTime / 1000
  48.    earthquake = Earthquake(place, float(mag), float(longitude), float(latitude), int(time))
  49.  
  50.    return earthquake
  51.  
  52. def read_quakes_from_file(filename):
  53.    inFile = open(filename, "r")
  54.    newlist = []
  55.    for line in inFile:
  56.       linelist = line.split()
  57.  
  58.       place = " ".join(linelist[4:])
  59.       mag = linelist[0]
  60.       longitude = linelist[1]
  61.       latitude = linelist[2]
  62.       time = linelist[3]
  63.       earthquake = Earthquake(place, float(mag), float(longitude), float(latitude), int(time))
  64.  
  65.       newlist.append(earthquake)
  66.    inFile.close()
  67.  
  68.    return newlist
  69.  
  70. def filter_by_mag(quakes, low, high):
  71.    newlist = []
  72.    for x in quakes:
  73.       if (x.mag >= low) and (x.mag <= high):
  74.          newlist.append(x)
  75.    return newlist
  76.  
  77.      
  78. def filter_by_place(quakes, word):
  79.    newlist = []
  80.    for x in quakes:
  81.      
  82.       status = x.place.lower().find(word.lower())
  83.       if status > 0:
  84.          newlist.append(x)
  85.    return newlist
  86.  
  87. def get_json(url):
  88.    with urlopen(url) as response:
  89.       html = response.read()
  90.    htmlstr = html.decode("utf-8")
  91.    return loads(htmlstr)
  92.  
  93. def time_to_str(time):
  94.    return datetime.fromtimestamp(time).strftime('%Y-%m-%d %H:%M:%S')
  95.  
  96. def printEarthquake(earthquakes):
  97.    print("Earthquakes:")
  98.    print("------------")
  99.    for x in earthquakes:
  100.       time = time_to_str(x.time)
  101.       print("(%.2f" % x.mag + ") %40s" % x.place, "at", time, "(%7.3f, %.3f)" % (x.longitude, x.latitude))  
  102.    print("")
  103.  
  104. def choice_S(allQuakes):
  105.    choice = input("Sort by (m)agnitude, (t)ime, (l)ongitude, or l(a)titude? ").lower()
  106.    print("")
  107.    if choice == "m":
  108.       allQuakes.sort(key=attrgetter("mag"), reverse=True)
  109.    if choice == "t":
  110.       allQuakes.sort(key=attrgetter("time"), reverse=True)
  111.    if choice == "l":
  112.       allQuakes.sort(key=attrgetter("longitude"), reverse=True)
  113.    if choice == "a":
  114.       allQuakes.sort(key=attrgetter("latitude"))
  115.    printEarthquake(allQuakes)
  116.  
  117. def choice_F(allQuakes):
  118.    choice = input("Filter by (m)agnitude or (p)lace? ").lower()
  119.    # print("")
  120.    if choice == "m":
  121.       low = float(input("Lower bound: "))
  122.       high = float(input("Upper bound: "))
  123.       print("")
  124.       printEarthquake(filter_by_mag(allQuakes, low, high))
  125.    if choice == "p":
  126.       word = input("Search for what string? ").lower()
  127.       print("")
  128.       printEarthquake(filter_by_place(allQuakes, word))
  129.  
  130. def choice_N(allQuakes):
  131.    EQD = get_json('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_hour.geojson')
  132.    # print(EQD["features"][0])
  133.    featList = EQD["features"]
  134.    oldListLength = len(allQuakes)
  135.    # print(featList[0])
  136.    for x in range(len(featList)):
  137.       newQuake = quake_from_feature(featList[x])
  138.       if newQuake not in allQuakes:
  139.          allQuakes.append(newQuake)
  140.  
  141.    newListLength = len(allQuakes)
  142.  
  143.    if newListLength > oldListLength:
  144.       print("nNew quakes found!!!n")
  145.  
  146.    printEarthquake(allQuakes)
  147.  
  148.  
  149.  
  150. def write_quakes_to_file(filename, allQuakes):
  151.    out_file = open(filename, "w")
  152.  
  153.    for eq in earthquakes:
  154.       out_file.write(f"{eq.mag} {eq.longitude} {eq.latitude} {eq.time} {eq.place}n")
  155.  
  156.    out_file.close()
  157.  
  158.    # newQuake = quake_from_feature(f)
  159.    # if newQuake == True and (newQuake in allQuakes == False):
  160.    #    allQuakes.append(newQuake)
  161.    # print("New quakes found!!!")
  162.    # printEarthquake(allQuakes)
  163.  
  164.  
  165.  
  166. # valX = read_quakes_from_file("quakes.txt")
  167. # # printEarthquake(valX)
  168. # printEarthquake(filter_by_place(valX, "ca"))
  169.  
  170.  
  171. # print(read_quakes_from_file("quakes.txt"))
  172.  
  173.  
  174. --- MAIN BELOW ---
  175.  
  176. from quakeFuncs import *
  177.  
  178. def main():
  179.         # print("Earthquakes:")
  180.         # print("------------")
  181.  
  182.         feature = get_json("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_hour.geojson")
  183.  
  184.         print("")
  185.  
  186.         allQuakes = read_quakes_from_file("quakes.txt")
  187.  
  188.  
  189.  
  190.         printEarthquake(allQuakes)
  191.         choice = getChoice()
  192.        
  193.         while choice != "q":
  194.                 if choice == "s":
  195.                         choice_S(allQuakes)
  196.                 if choice == "f":
  197.                         choice_F(allQuakes)
  198.                 if choice == "n":
  199.                         choice_N(allQuakes)
  200.                 choice = getChoice()
  201.        
  202.         if choice == "q":
  203.                 write_quakes_to_file("quakes.txt", allQuakes)
  204.                 quit()
  205.  
  206.    
  207. if __name__ == "__main__":
  208.    main()
  209.  
  210.  
  211.    
  212.