#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import json

from db import commit_db_item
from db.player import Player
from db.team import Team
from utils.player_finder import PlayerFinder


def migrate_players(plr_src_file=None):

    if not plr_src_file:
        plr_src_file = os.path.join(
            os.path.dirname(__file__), 'nhl_players.json')

    migration_data = json.load(open(plr_src_file))

    for player_id in sorted(migration_data.keys())[:]:

        last_name = migration_data[player_id]['last_name']
        first_name = migration_data[player_id]['first_name']
        position = migration_data[player_id]['position']

        alternate_last_names = migration_data[player_id].get(
            'alternate_last_names', None)
        alternate_first_names = migration_data[player_id].get(
            'alternate_first_names', None)
        alternate_positions = migration_data[player_id].get(
            'alternate_positions', None)

        plr = Player(
            player_id, last_name, first_name, position,
            alternate_last_names=alternate_last_names,
            alternate_first_names=alternate_first_names,
            alternate_positions=alternate_positions
        )

        print("Working on %s" % plr)

        commit_db_item(plr)


def search_players(src_type):

    plr_f = PlayerFinder()

    current_teams = Team.find_teams_for_season()
    for team in sorted(current_teams)[:]:
        team_players = plr_f.find_players_for_team(team, src_type)
        print(sorted(team_players))


@@ -63,16 +63,81 @@
 t json%0A%0A
+from concurrent.futures import ThreadPoolExecutor, as_completed%0A%0A
 from db 
@@ -158,16 +158,16 @@
 db_item%0A
-
 from db.
@@ -257,16 +257,93 @@
 rFinder%0A
+from utils.player_data_retriever import PlayerDataRetriever%0A%0AMAX_WORKERS = 8%0A
 %0A%0Adef mi
@@ -1476,16 +1476,50 @@
 Finder()
+%0A    plr_r = PlayerDataRetriever()
 %0A%0A    cu
@@ -1600,16 +1600,18 @@
 teams)%5B:
+10
 %5D:%0A     
@@ -1672,24 +1672,25 @@
 c_type)%0A
+%0A
         
 print(so
@@ -1685,33 +1685,682 @@
     
-print(sorted(team_players))%0A
+# using concurrent threads to speed up the retrieval of single player%0A        # season statistics%0A        with ThreadPoolExecutor(max_workers=MAX_WORKERS) as threads:%0A            future_tasks = %7B%0A                threads.submit(%0A                    plr_r.retrieve_player_seasons,%0A                    plr.player_id): plr for plr in team_players%7D%0A            for future in as_completed(future_tasks):%0A                try:%0A                    # TODO: think of something to do with the result here%0A                    data = future.result()%0A                except Exception as e:%0A                    print%0A                    print(%22Conccurrent task generated an exception: %25s%22 %25 e)
 %0A

Include season statistics retrieval in player search and creation