earthquake = {
      'rms': '1.85',
      'updated': '2014-06-11T05:22:21.596Z',
      'type': 'earthquake',
      'magType': 'mwp',
      'longitude': '-136.6561',
      'gap': '48',
      'depth': '10',
      'dmin': '0.811',
      'mag': '5.7',
      'time': '2014-06-04T11:58:58.200Z',
      'latitude': '59.0001',
      'place': '73km WSW of Haines, Alaska',
      'net': 'us',
      'nst': '',
      'id': 'usc000rauc'}

"""
Explanation: Quakebot
PART ONE: Write your few tiny functions
End of explanation
"""


def depth_to_words(quake):
    if int(quake['depth']) <= 70:
        return 'shallow'
    elif int(quake['depth']) <= 300:
        return 'intermediate'
    else:
        return 'deep'
print(depth_to_words(earthquake))

def mag_to_words(quake):
    if float(quake['mag']) <= 4.0:
        return 'minor'
    elif float(quake['mag']) <= 5.0:
        return 'moderate'
    elif float(quake['mag']) <= 6.0:
        return 'strong'
    elif float(quake['mag']) <= 7.0:
        return 'major'
    else:
        return 'gigantic'
print(mag_to_words(earthquake))

def mag_numbers(quake):
    return str(quake['mag'])
print(mag_numbers(earthquake))
#type(mag_numbers(earthquake))

import dateutil.parser

#!pip install dateutils

def day(quake):
    timestring = quake['time']
    day = dateutil.parser.parse(timestring)
    return day.strftime("%A")
print(day(earthquake))

def time(quake):
    timestring = quake['time']
    time = dateutil.parser.parse(timestring)
    #if time < 12:00
    if int(time.strftime("%H")) <= 4:
        return 'night'
    elif int(time.strftime("%H")) <= 12:
        return 'morning'
    elif int(time.strftime("%H")) <= 18:
        return 'afternoon'
    elif int(time.strftime("%H")) <= 22:
        return 'evening'
    else:
        return 'night'
print(time(earthquake))
type(time(earthquake))

def date(quake):
    datestring = quake['time']
    date = dateutil.parser.parse(datestring)
    return date.strftime("%B %d")
print(date(earthquake))

def location(quake):
    location_string = str(quake['place'])
    return location_string
print(location(earthquake))

"""
Explanation: What we want: A DEPTH POWER, MAGNITUDE earthquake was reported DAY TIME_OF_DAY on DATE LOCATION.
Shallow earthquakes are between 0 and 70 km deep; intermediate earthquakes, 70 - 300 km deep; and deep earthquakes, 300 - 700
End of explanation
"""


def eq_to_sentence(quake):
    str_depth_to_words = str(depth_to_words(quake))
    str_mag_to_words = str(mag_to_words(quake))
    str_mag_numbers = str(mag_numbers(quake))
    return 'A ' + str_depth_to_words + " " + str_mag_to_words + " " + 'Magnitude' \
    + " " + str_mag_numbers + " earthquake was reported " + day(quake) + " " \
    + time(quake) + " on " + date(quake) + " " + location(quake) + "."
print(eq_to_sentence(earthquake))

"""
Explanation: PART TWO: Write the eq_to_sentence function
End of explanation
"""


#from urllib.request import urlopen
#many_quakes = urlopen("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.csv").read()

import pandas as pd
#quakes = pd.read_csv('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.csv')

quakes = pd.read_csv('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.csv')
earthquakes = quakes.to_dict('records')

#earthquakes

for x in earthquakes:
        if x['mag'] > 4.0:
            print(eq_to_sentence(x))

"""
Explanation: PART THREE: Doing it in bulk
End of explanation
"""


def other_mag_to_words(quake):
    mag_other = str(quake['mag'])
    return mag_other

def type_of_event(quake):
    return quake['type']

def date_other_events(quake):
    datestring = quake['time']
    date = dateutil.parser.parse(datestring)
    return date.strftime("%B %d")

def location_other_events(quake):
    location_string = str(quake['place'])
    return location_string

def other_events_to_sentence(quake):
    return "There was also a magnitude " + other_mag_to_words(quake) + \
    " " + type_of_event(quake) + " on " + date_other_events(quake) + " " \
    + location_other_events(quake) + "." 

for x in earthquakes:
        if x['type'] != 'earthquake':
            print(other_events_to_sentence(x))

"""
Explanation: PART FOUR: The other bits
End of explanation
"""


