from urllib.request import urlopen
import sqlite3

def fetch():

    link = "https://www.fs.usda.gov/colville"

    f = urlopen(link)
    myfile = f.read()
    s=myfile.decode()
    t=s.find('''var fireLevel2= {"dangerLevelAt"''')
    s=s[t:]
    x=s.find('</script>')
    s=s[:x]
    return s

def parse(s):
    zones = ['678 W', '678 E', '684', '685', '686', '687', '688']
    pval = []

    for zone in range(len(zones)):
        t=s.find(zones[zone])
        temp = s[t:]
        x=s.find('''}''')
        temp = temp[:x]

        t1=temp.find('''"col3"''')
        temp = temp[t1:]

        x1=temp.find('''"]''')
        if temp[9:x1] == '1':
            temp1 = 'LOW'
        elif temp[9:x1] == '2':
            temp1 = 'MODERATE'
        elif temp[9:x1] == '3':
            temp1 = 'HIGH'
        elif temp[9:x1] == '4':
            temp1 = 'VERY HIGH'
        elif temp[9:x1] == '5':
            temp1 = 'EXTREME'
        else:
            temp1 = 'N/A'

        t2=temp.find('''"col4"''')
        temp = temp[t2:]

        x2=temp.find('''"]''')
        if temp[9:x2] == '1':
            temp2 = 'IFPL I'
        elif temp[9:x2] == '2':
            temp2 = 'IFPL II'
        elif temp[9:x2] == '3':
            temp2 = 'IFPL III'
        elif temp[9:x2] == '4':
            temp2 = 'IFPL IV'
        else:
            temp2 = 'N/A'

        temp3 = {'zone':zones[zone],'firedanger':temp1,'ifpl':temp2}
        pval = pval + [temp3]

    return pval

def main(fc):
    s = fetch()
    pval = parse(s)
    pvals = []
    for i in range(len(fc)):
        for j in range(len(pval)):
            if fc[i] == pval[j]['zone']:
                pvals = pvals + [pval[j]]

    return pvals

#main()
#input()
