from urllib.request import urlopen
import geojson

def currentw(userloc):
    link = "https://forecast.weather.gov/MapClick.php?lon="+str(userloc[1])+"&lat="+str(userloc[0])+"#"
    f = urlopen(link)
    myfile = f.read()
    s = myfile.decode()

    cs = s.find('<!-- Current Conditions -->')
    cf = s.find('<!-- /Current Conditions -->')
    c = s[cs:cf]

    ts = c.find('<h2 class="panel-title">')
    tf = c.find('</h2>')
    loc = c[ts+24:tf]
    # TEMP
    wx = [0]
    ts = c.find('<p class="myforecast-current-lrg">')
    tf = c.find('&deg;F')
    wx[0] = c[ts+34:tf] + u"\u00b0" + 'F'
    # HUMID
    ts = c.find('<td>')
    c = c[ts:]
    temp = c[4:c.find('</td>')]
    wx = wx + [temp]
    c = c[c.find('</td>')+5:]
    # WINDSPEED
    ts = c.find('<td>')
    c = c[ts:]
    temp = c[4:c.find('</td>')]
    wx = wx + [temp]
    c = c[c.find('</td>')+5:]
    # BAROMETER
    ts = c.find('<td>')
    c = c[ts:]
    temp = c[4:c.find('</td>')]
    wx = wx + [temp]
    c = c[c.find('</td>')+5:]
    # DEWPOINT
    ts = c.find('<td>')
    c = c[ts:]
    temp = c[4:c.find('&deg;F')] + u"\u00b0" + 'F'
    wx = wx + [temp]
    c = c[c.find('</td>')+5:]
    # VISIBILITY
    ts = c.find('<td>')
    c = c[ts:]
    temp = c[4:c.find('</td>')]
    wx = wx + [temp]
    c = c[c.find('</td>')+5:]
    # LASTUPDATE
    ts = c.find('<td>\n')
    c = c[ts:]
    temp = c[21:c.find('            </td>')]
    wx = wx + [temp]
    c = c[c.find('</td>')+5:]

    cwx = [{'location':loc},{'temperature':wx[0],'humidity':wx[1],'windspeed':wx[2],'barometer':wx[3],'dewpoint':wx[4],'visibility':wx[5],'lastupdate':wx[6]}]

    return cwx

def forecastw(userloc):
    link = "https://api.weather.gov/points/"+str(userloc[0])+","+str(userloc[1])
    f = urlopen(link)
    myfile = f.read()
    s = geojson.loads(myfile)

    city = s['properties']['relativeLocation']['properties']['city']
    state = s['properties']['relativeLocation']['properties']['state']
    d = (s['properties']['relativeLocation']['properties']['distance']['value'])/1609.34

    if d%1 < 0.5:
        dist = int(d)
    else:
        dist = int(d+1)
    b = s['properties']['relativeLocation']['properties']['bearing']['value']
    if b >= 0 and b <= 22:
        bearing = 'N'
    elif b > 22 and b < 68:
        bearing = 'NE'
    elif b >= 68 and b <= 112:
        bearing = 'E'
    elif b > 112 and b < 158:
        bearing = 'SE'
    elif b >= 158 and b <= 202:
        bearing = 'S'
    elif b > 202 and b < 248:
        bearing = 'SW'
    elif b >= 248 and b <= 292:
        bearing = 'W'
    elif b > 292 and b < 338:
        bearing = 'NW'
    elif b >= 338 and b <= 360:
        bearing = 'N'

    loc = str(dist) + ' Miles ' + bearing + ' of ' + city + ', ' + state

    link = s['properties']['forecast']
    f = urlopen(link)
    myfile = f.read()
    s = geojson.loads(myfile)
    t = s['properties']['periods']

    weather = []
    for i in range(len(t)):
        if t[i]['isDaytime'] == True:
            hl = 'High: '
        else:
            hl = 'Low: '
        weather = weather + [[t[i]['name'],hl,str(t[i]['temperature'])+'\u00b0'+t[i]['temperatureUnit'],t[i]['detailedForecast']]]

    fwx = [['7-Day forecast for: ' + loc,'Detailed forecast for: ' + loc],weather]

    return fwx

def main(userloc):
    c = currentw(userloc)
    f = forecastw(userloc)
    weather = c + f
    return weather


#main()
#input()
