import argparse
import glob

import pymongo
import progressbar

import stengel.data.parse as parse


def year(filename):
    return int(filename.split("/")[2])


def get_event_files(first_year, last_year):
    all_files = glob.glob("data/retrosheet/*/*.EV*")
    return [f for f in all_files
            if first_year <= year(f) <= last_year]

# Set up argument parser
arg_parser = argparse.ArgumentParser(description="Parse one or years of Retrosheet event data.")
arg_parser.add_argument("-f", "--first-year", type=int, default=1,
                        help="""First year of Retrosheet events to parse. Default is the earliest
                        event file in data/retrosheet.""")
arg_parser.add_argument("-l", "--last-year", type=int, default=9999,
                        help="""Last year of Retrosheet events to parse. Default is the latest
                        event file in data/retrosheet.""")
args = arg_parser.parse_args()

# Set up connection to database
client = pymongo.MongoClient("mongodb://localhost:23415")
database = client.stengel

# Parse all of the Retrosheet files
print("Parsing Retrosheet game files...")
bar = progressbar.ProgressBar()
files = get_event_files(args.first_year, args.last_year)
for f in bar(files):
    game_parser = parse.RetrosheetGameFileParser(f)
    game_parser.parse()
    game_parser.upload(database)

# Create indices for the games
print("Creating index on metadata.id_...")
database.games.create_index([("metadata.id_", pymongo.ASCENDING)])
print("Creating index on metadata.game_date...")
database.games.create_index([("metadata.game_date", pymongo.ASCENDING)])

95a5a96665120b8aff3e7589f6f169e419bfc48f
<mask token>


def get_event_files(first_year, last_year):
    all_files = glob.glob('data/retrosheet/*/*.EV*')
    return [f for f in all_files if first_year <= year(f) <= last_year]


<mask token>

<mask token>


def year(filename):
    return int(filename.split('/')[2])


def get_event_files(first_year, last_year):
    all_files = glob.glob('data/retrosheet/*/*.EV*')
    return [f for f in all_files if first_year <= year(f) <= last_year]


<mask token>
arg_parser.add_argument('-f', '--first-year', type=int, default=1, help=
    """First year of Retrosheet events to parse. Default is the earliest
                        event file in data/retrosheet."""
    )
arg_parser.add_argument('-l', '--last-year', type=int, default=9999, help=
    """Last year of Retrosheet events to parse. Default is the latest
                        event file in data/retrosheet."""
    )
<mask token>
print('Parsing Retrosheet game files...')
<mask token>
for f in bar(files):
    game_parser = parse.RetrosheetGameFileParser(f)
    game_parser.parse()
    game_parser.upload(database)
print('Creating index on metadata.id_...')
database.games.create_index([('metadata.id_', pymongo.ASCENDING)])
print('Creating index on metadata.game_date...')
database.games.create_index([('metadata.game_date', pymongo.ASCENDING)])

<mask token>


def year(filename):
    return int(filename.split('/')[2])


def get_event_files(first_year, last_year):
    all_files = glob.glob('data/retrosheet/*/*.EV*')
    return [f for f in all_files if first_year <= year(f) <= last_year]


arg_parser = argparse.ArgumentParser(description=
    'Parse one or years of Retrosheet event data.')
arg_parser.add_argument('-f', '--first-year', type=int, default=1, help=
    """First year of Retrosheet events to parse. Default is the earliest
                        event file in data/retrosheet."""
    )
arg_parser.add_argument('-l', '--last-year', type=int, default=9999, help=
    """Last year of Retrosheet events to parse. Default is the latest
                        event file in data/retrosheet."""
    )
args = arg_parser.parse_args()
client = pymongo.MongoClient('mongodb://localhost:23415')
database = client.stengel
print('Parsing Retrosheet game files...')
bar = progressbar.ProgressBar()
files = get_event_files(args.first_year, args.last_year)
for f in bar(files):
    game_parser = parse.RetrosheetGameFileParser(f)
    game_parser.parse()
    game_parser.upload(database)
print('Creating index on metadata.id_...')
database.games.create_index([('metadata.id_', pymongo.ASCENDING)])
print('Creating index on metadata.game_date...')
database.games.create_index([('metadata.game_date', pymongo.ASCENDING)])

import argparse
import glob
import pymongo
import progressbar
import stengel.data.parse as parse


def year(filename):
    return int(filename.split('/')[2])


def get_event_files(first_year, last_year):
    all_files = glob.glob('data/retrosheet/*/*.EV*')
    return [f for f in all_files if first_year <= year(f) <= last_year]


arg_parser = argparse.ArgumentParser(description=
    'Parse one or years of Retrosheet event data.')
arg_parser.add_argument('-f', '--first-year', type=int, default=1, help=
    """First year of Retrosheet events to parse. Default is the earliest
                        event file in data/retrosheet."""
    )
arg_parser.add_argument('-l', '--last-year', type=int, default=9999, help=
    """Last year of Retrosheet events to parse. Default is the latest
                        event file in data/retrosheet."""
    )
args = arg_parser.parse_args()
client = pymongo.MongoClient('mongodb://localhost:23415')
database = client.stengel
print('Parsing Retrosheet game files...')
bar = progressbar.ProgressBar()
files = get_event_files(args.first_year, args.last_year)
for f in bar(files):
    game_parser = parse.RetrosheetGameFileParser(f)
    game_parser.parse()
    game_parser.upload(database)
print('Creating index on metadata.id_...')
database.games.create_index([('metadata.id_', pymongo.ASCENDING)])
print('Creating index on metadata.game_date...')
database.games.create_index([('metadata.game_date', pymongo.ASCENDING)])

import argparse
import glob

import pymongo
import progressbar

import stengel.data.parse as parse


def year(filename):
    return int(filename.split("/")[2])


def get_event_files(first_year, last_year):
    all_files = glob.glob("data/retrosheet/*/*.EV*")
    return [f for f in all_files
            if first_year <= year(f) <= last_year]

# Set up argument parser
arg_parser = argparse.ArgumentParser(description="Parse one or years of Retrosheet event data.")
arg_parser.add_argument("-f", "--first-year", type=int, default=1,
                        help="""First year of Retrosheet events to parse. Default is the earliest
                        event file in data/retrosheet.""")
arg_parser.add_argument("-l", "--last-year", type=int, default=9999,
                        help="""Last year of Retrosheet events to parse. Default is the latest
                        event file in data/retrosheet.""")
args = arg_parser.parse_args()

# Set up connection to database
client = pymongo.MongoClient("mongodb://localhost:23415")
database = client.stengel

# Parse all of the Retrosheet files
print("Parsing Retrosheet game files...")
bar = progressbar.ProgressBar()
files = get_event_files(args.first_year, args.last_year)
for f in bar(files):
    game_parser = parse.RetrosheetGameFileParser(f)
    game_parser.parse()
    game_parser.upload(database)

# Create indices for the games
print("Creating index on metadata.id_...")
database.games.create_index([("metadata.id_", pymongo.ASCENDING)])
print("Creating index on metadata.game_date...")
database.games.create_index([("metadata.game_date", pymongo.ASCENDING)])
