import nflgame
from nfl import Base,NFLPlayer,NFLPlayerStat
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker



engine = create_engine('sqlite:///players.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

for p,v in nflgame.players.iteritems():
  n = NFLPlayer(p,v.name,v.position,v.birthdate,v.college,v.height,v.weight,v.years_pro)
  try:
    session.add(n)
    session.commit()
  except:
    session.rollback()

#for y in [2014]:
for y in [2009,2010,2011,2012,2013,2014]:
  for w in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]:
    games = nflgame.games(y,week=w)
    players = nflgame.combine_game_stats(games)
    for p in players:
      print p.playerid
      #print p.stats
      for s,v in p.stats.iteritems():
        s = NFLPlayerStat(p.playerid,y,w,s,v)
        try:
          session.add(s)
          session.commit()
        except:
          session.rollback()

