# Path: park_api/db.py
# POOL = None
#     POOL = ThreadedConnectionPool(1, 20,
#             database=u.path[1:],
#             user=u.username,
#             password=u.password,
#             host=u.hostname,
#             port=u.port)
# def setup(url=env.DATABASE_URI):
# def cursor(commit=False):


import argparse
import csv
from time import gmtime
from park_api import db
def get_data(cursor, query):
    
    cursor.execute(query[1])
    count = cursor.fetchone()['count']
    
    cursor.execute(query[0])
    table = []
    for i in range(count):
        table.append(cursor.fetchone())
    
    data = {}
    
    for row in table:
        if not row['city'] in data.keys():
            data[row['city']] = {}
        
        for lot in row['data']['lots']:
            if not lot['id'] in data[row['city']].keys():
                data[row['city']][lot['id']] = []
            data[row['city']][lot['id']].append((row['data']['last_downloaded'], lot['free']))
    
    print(data)

def main(args):
    if args.month and args.week:
        print("Month and Week cannot be specified together.")
        exit(1)
    
    query = create_query(args.city, args.year, args.month, args.week)
    

# Path: park_api/db.py
# POOL = None
#     POOL = ThreadedConnectionPool(1, 20,
#             database=u.path[1:],
#             user=u.username,
#             password=u.password,
#             host=u.hostname,
#             port=u.port)
# def setup(url=env.DATABASE_URI):
# def cursor(commit=False):


# Path: park_api/dump.py
import argparse
import csv
from time import gmtime
from park_api import db

def get_data(cursor, query):
    
    cursor.execute(query[1])
    count = cursor.fetchone()['count']
    
    cursor.execute(query[0])
    table = []
    for i in range(count):
        table.append(cursor.fetchone())
    
    data = {}
    
    for row in table:
        if not row['city'] in data.keys():
            data[row['city']] = {}
        
        for lot in row['data']['lots']:
            if not lot['id'] in data[row['city']].keys():
                data[row['city']][lot['id']] = []
            data[row['city']][lot['id']].append((row['data']['last_downloaded'], lot['free']))
    
    print(data)

def main(args):
    if args.month and args.week:
        print("Month and Week cannot be specified together.")
        exit(1)
    
    query = create_query(args.city, args.year, args.month, args.week)
    

    db.setup()