Ejemplo n.º 1
0
class PbpPushStatPrinter(ObjectPrinter):
    """
    helper print examples of real-time (socket pushed) Play-by-Play objects.
    """

    def __init__(self):
        super().__init__()
        self.dataden = DataDen()
        self.examples = 2  # number of printed examples per type
        self.categories = [
            #  db,  coll,  parent_api,  distinct types
            # ('nba','event','pbp',       'event_type'),
            ('nhl', 'event', 'pbp', 'event_type'),
            # 'player',
            # 'boxscores'
        ]

    def print_examples(self):
        for db, coll, parent_api, distinct in self.categories:
            event_types = self.dataden.find(db, coll, parent_api).distinct(distinct)

            # print the array of event types
            print('')
            print('[%s] example object(s) for distinct values of field: %s ...' % (str(self.examples), distinct))
            self.print(event_types, distinct)

            # print the unique values for the 'distinct' field
            for event_type in event_types:
                # get the objects of this type (probably many thousands!)
                events = self.dataden.find(db, coll, parent_api, target={distinct: event_type})
                # print X examples
                for n, event in enumerate(events):
                    if n >= self.examples: break
                    # now print it
                    self.print(event, '%s example %s' % (event_type, str(n + 1)))
Ejemplo n.º 2
0
 def __init__(self):
     super().__init__()
     self.dataden = DataDen()
     self.examples = 2  # number of printed examples per type
     self.categories = [
         #  db,  coll,  parent_api,  distinct types
         # ('nba','event','pbp',       'event_type'),
         ('nhl', 'event', 'pbp', 'event_type'),
         # 'player',
         # 'boxscores'
     ]
Ejemplo n.º 3
0
    def cleanup_injuries(self):
        """
        When injury objects are parsed, they connect players with injuries.

        However, there needs to be a method which removes injuries objects
        from players who are no longer injured. This is that process,
        and it should be run rather frequently - or at least about as
        frequently as injury feeds are parsed.
        :return:
        """

        #
        # get an instance of DataDen - ie: a connection to mongo db with all
        # the stats
        dd = DataDen()

        #
        # injury process:
        # 1) get all the updates (ie: get the most recent dd_updated__id, and
        # get all objects with that value)
        injury_objects = list(dd.find_recent('nba', 'injury', 'injuries'))
        print(str(len(injury_objects)), 'recent injury updates')

        # 2) get all the existing players with injuries
        # players = list( Player.objects.filter( injury_type__isnull=False, injury_id__isnull=False ) )
        all_players = list(Player.objects.all())

        # 3) for each updated injury, remove the player from the all-players list
        for inj in injury_objects:
            #
            # wrapped=False just means the obj isnt wrapped by the oplogwrapper
            i = Injury(wrapped=False)
            i.parse(inj)
            try:
                all_players.remove(i.get_player())
            except ValueError:
                pass  # thrown if player not in the list.

        # 5) with the leftover existing players,
        #    remove their injury since theres no current injury for them
        ctr_removed = 0
        for player in all_players:
            if player.remove_injury():
                ctr_removed += 1
        print(str(ctr_removed), 'leftover/stale injuries removed')
Ejemplo n.º 4
0
    def handle(self, *args, **options):
        """
        generate a salary pool with a default config

        :param args:
        :param options:
        :return:
        """

        msg = 'updating The Sports Xchange news, injury info, and transactions'
        self.stdout.write(msg)

        dd = DataDen()

        site_sport = None
        for sport in options['sport']:
            # # print the # of content items for this sport total to test
            # items = dd.find( sport, 'item', 'content' )
            # self.stdout.write('')
            # self.stdout.write('')
            # self.stdout.write('-------------------------- items -------------------------')
            # self.stdout.write('%+6s ... %+6s items'% (sport, str(items.count())))
            # # debug print the items
            # self.print_objects( items )

            # the objects in the 'content' collection are the master objects with a list of all the item ids for a day
            # all_content = dd.find( sport, 'content', 'content' )
            # self.stdout.write('')
            # self.stdout.write('')
            # self.stdout.write('-------------------------- content -------------------------')
            # self.stdout.write('%+6s ... %+6s content master lists'% (sport, str(all_content.count())))
            # # debug print the items
            # self.print_objects( all_content )

            #
            # TODO - make the models
            #       --> test the models we made

            #
            # TODO - parse the objects into their distinct things
            #       --> in progress
            # content_parser = TsxContentParser( sport )
            # for content_obj in all_content:
            #     msg = content_parser.parse( content_obj )
            #     self.stdout.write('')
            #     self.stdout.write( msg )
            #     self.stdout.write('')

            #
            # TODO - figure out the best way and/or place to hook up content text to sports.<sport>.models.Team / Player objects
            #  ----> api call     (leave existing injury parsing doing what its doing)

            #
            # this is simply a test that parses ALL the content in DataDen/mongo for the sport
            p = DataDenParser()
            p.setup(sport, force_triggers=DataDenParser.CONTENT_TRIGGERS)
Ejemplo n.º 5
0
    def handle(self, *args, **options):
        """

        :param args:
        :param options:
        :return:
        """

        msg = 'updating season stats'
        self.stdout.write( msg )

        dd = DataDen()

        site_sport  = None
        for sport in options['sport']:


            # p = DataDenParser()
            # p.setup( sport, force_triggers=DataDenParser.SEASON_STATS_TRIGGERS )
            seasons = dd.find(sport, 'TODO_collection', 'TODO_parent_api')
Ejemplo n.º 6
0
    def get(request, draft_group_id):

        dgm = DraftGroupManager()
        draft_group = dgm.get_draft_group(draft_group_id)
        boxscores = dgm.get_game_boxscores(draft_group)

        dd = DataDen()
        game_srids = []
        for b in boxscores:
            game_srids.append(b.srid_game)

        game_events = dd.find('nba', 'event', 'pbp',
                              {'game__id': {
                                  '$in': game_srids
                              }})
        events = []
        for e in game_events:
            events.append(e)

        return HttpResponse(json.dumps(events),
                            content_type='application/json')
Ejemplo n.º 7
0
    def handle(self, *args, **options):
        """
        generate a salary pool with a default config

        :param args:
        :param options:
        :return:
        """

        msg = 'updating Fantasy Points Per Game (FPPG)'
        self.stdout.write(msg)

        dd = DataDen()

        site_sport = None
        for sport in options['sport']:
            print('   %s' % sport)
            #
            # get the <Sport>Season class
            season = Season.factory(sport)
            game_ids = season.get_game_ids_regular_season(2015)
Ejemplo n.º 8
0
        :param sport:
        :param async:   False   - runs inline.
                        True    - requires celery workers running to handle each object's parsing

        :param replay:  False   - default.
                        True    - if the replay is doing the setup.

        :param force_triggers: set a list of 3-tuples of the triggers to use to setup stats.
                                this effectively updates postgres with objects from dataden/mongo
                                only for the specified triggers, ie: ('nba','game','boxscore')
        :return:
        """

        self.__valid_sport(sport)  # make sure we can set it up
        self.setup_triggers(sport)
        dataden = DataDen(no_cursor_timeout=True)  # dont let cursor timeout for setup()

        parser = self.__get_parser(sport)
        triggers = parser.get_triggers()

        #
        # if force_triggers is set, it overrides
        if force_triggers is not None:
            triggers = force_triggers

        #
        # get and parse the corresponding objects
        for t in triggers:
            #
            # as a debug, print out the 'ns' and the 'parent_api', and count
            db = t[0]
Ejemplo n.º 9
0
    def handle(self, *args, **options):
        i = 0
        play_srid = None
        words = []
        send = False
        for arg in options['srids']:
            if i == 0:
                play_srid = arg
            if i == 1:
                send = True
            words.append(arg)
            i += 1

        phrase = ' '.join(words)

        self.stdout.write('+++ nflo play objects | %s +++' % play_srid)
        self.stdout.write('-------------------------------------------------------')
        #self.stdout.write('description must contain:     "%s"' % str(phrase))
        self.stdout.write('')

        from dataden.classes import DataDen
        dd = DataDen()

        db = 'nflo'
        parent_api = 'pbp'

        # get the play object
        play = None
        target = {
            'id': play_srid,
            # 'description': {
            #     '$regex': phrase
            # }
        }
        plays = dd.find(db, 'play', parent_api, target=target)
        for obj in plays:
            play = obj

        # play_srid = play.get('id')
        #play_srid = srid # because were passing it into the command now

        sl = None  # start location object
        sp = None  # start possession object
        el = None  # end location object
        ep = None  # end possession object

        # start situation data
        situation_list_id = 'start_situation__list'
        situation = play.get(situation_list_id, {})

        sl_srid = situation.get('location', '')
        locations = dd.find(db, 'location', parent_api,
                            {'id': sl_srid, 'play__id': play_srid, 'parent_list__id': situation_list_id})
        for obj in locations:
            sl = obj

        sp_srid = situation.get('possession', '')
        possessions = dd.find(db, 'possession', parent_api,
                              {'id': sp_srid, 'play__id': play_srid, 'parent_list__id': situation_list_id})
        for obj in possessions:
            sp = obj

        # end situation data
        situation_list_id = 'end_situation__list'
        situation = play.get(situation_list_id, {})

        el_srid = situation.get('location', '')
        locations = dd.find(db, 'location', parent_api,
                            {'id': el_srid, 'play__id': play_srid, 'parent_list__id': situation_list_id})
        for obj in locations:
            el = obj

        ep_srid = situation.get('possession', '')
        possessions = dd.find(db, 'possession', parent_api,
                              {'id': ep_srid, 'play__id': play_srid, 'parent_list__id': situation_list_id})
        for obj in possessions:
            ep = obj

        sport_db = 'nflo'
        parent_api = 'pbp'
        self.stdout.write("sport_db = '%s'" % sport_db)
        self.stdout.write("parent_api = '%s'" % parent_api)
        self.stdout.write('')

        self.print_obj('start_location', sl, 'location', sport_db=sport_db, parent_api=parent_api, send=send)
        self.print_obj('start_possession', sp, 'possession', sport_db=sport_db, parent_api=parent_api, send=send)
        self.print_obj('end_location', el, 'location', sport_db=sport_db, parent_api=parent_api, send=send)
        self.print_obj('end_possession', ep, 'possession', sport_db=sport_db, parent_api=parent_api, send=send)
        self.print_obj('play', play, 'play', sport_db=sport_db, parent_api=parent_api, send=send)

        self.stdout.write('') # one more space at the end of output