from xml.etree import ElementTree as ET
import sys
import sqlite_utils

KML = "{http://www.opengis.net/kml/2.2}"


def iterate_kml(filepath):
    fp = open(filepath)
    parser = ET.XMLPullParser(["end"])
    autoincrement = 0
    while True:
        chunk = fp.read(1024 * 8)
        parser.feed(chunk)
        for event, element in parser.read_events():
            assert event == "end"
            if element.tag == f"{KML}Placemark":
                autoincrement += 1
                name = element.find(f".//{KML}name").text
                description = 'soon'
                #description = element.find(f".//{KML}description").text
                longitude, latitude, unknown = map(
                    float, element.find(f".//{KML}coordinates").text.split(",")
                )

                yield {
                        "FMID": autoincrement,
                        "MarketName": name,
                        "Website": "https://deflax.net",
                        "Facebook": "https://facebook.com/page",
                        "Twitter": "tw_user",
                        "Youtube": "yt_user",
                        "OtherMedia": "om_user",
                        "street": "1 afxhq",
                        "city": "none",
                        "County": "none",
                        "State": "none",
                        "zip": "none",
                        "Season1Date": "1970-01-01",
                        "Season1Time": "",
                        "Season2Date": "",
                        "Season2Time": "",
                        "Season3Date": "",
                        "Season3Time": "",
                        "Season4Date": "",
                        "Season4Time": "",
                        "x": latitude,
                        "y": longitude,
                        "Location": description,
                        "Credit": "",
                        "WIC": "",
                        "WICcash": "",
                        "SFMNP": "",
                        "SNAP": "",
                        "Organic": "",
                        "Bakedgoods": "",
                        "Cheese": "",
                        "Crafts": "",
                        "Flowers": "",
                        "Eggs": "",
                        "Seafood": "",
                        "Herbs": "",
                        "Vegetables": "",
                        "Honey": "",
                        "Jams": "",
                        "Maple": "",
                        "Meat": "",
                        "Nursery": "",
                        "Nuts": "",
                        "Plants": "",
                        "Poultry": "",
                        "Prepared": "",
                        "Soap": "",
                        "Trees": "",
                        "Wine": "",
                        "Coffee": "",
                        "Beans": "",
                        "Fruits": "",
                        "Grains": "",
                        "Juices": "",
                        "Mushrooms": "",
                        "PetFood": "",
                        "Tofu": "",
                        "WildHarvested": "",
                        "updateTime": ""
                        }

        if not chunk:
            break


if __name__ == "__main__":
    db = sqlite_utils.Database("fountains.db")
    
    db["locations"].insert_all(iterate_kml(sys.argv[-1]))
