Beispiel #1
0
    def players(self):
        players = []

        if self.status == 'Played':
            tree = gsm.get_tree('en',
                                self.sport,
                                'get_matches',
                                type=self.tag,
                                id=self.gsm_id,
                                detailed=True,
                                retry=3)

            for parent in ('lineups', 'lineups_bench'):
                for parent_e in gsm.parse_element_for(tree.getroot(), parent):
                    for e in parent_e:
                        players.append({
                            'name': e.attrib['person'],
                            'gsm_id': e.attrib['person_id'],
                        })

        if not players:
            for team_id in [self.oponnent_A.gsm_id, self.oponnent_B.gsm_id]:
                tree = gsm.get_tree('en',
                                    self.sport,
                                    'get_squads',
                                    type='team',
                                    id=team_id,
                                    retry=3)
                for e in gsm.parse_element_for(tree.getroot(), 'person'):
                    players.append({
                        'name': e.attrib['name'],
                        'gsm_id': e.attrib['person_id'],
                    })

        return players
Beispiel #2
0
    def update(self, e, parent=None):
        if 'person_A1_name' in e.attrib.keys():
            return

        model = self.element_to_model(e)
        self.log('debug', 'Updating %s:%s' % (model.tag, model.gsm_id))

        try:
            self.set_model_name(model, e)
        except:
            self.log('debug',
                     'Failed, passing on %s:%s' % (model.tag, model.gsm_id))
            return

        if not self.names_only:
            self.set_model_area(model, e)

            for key in self.autocopy:
                if key in e.attrib.keys() and hasattr(model, key):
                    self.copy_attr(model, e, key)

            method_key = model.__class__.__name__.lower()
            method = 'update_%s' % method_key
            if hasattr(self, method):
                getattr(self, method)(model, e, parent)

            method = 'update_%s_%s' % (self.sport.slug, method_key)
            if hasattr(self, method):
                getattr(self, method)(model, e, parent)
        model.save()

        def update_children(e):
            for child in e.getchildren():
                if 'double_A_id' in e.attrib.keys():
                    continue  # important !!

                if self.skip(child):
                    update_children(child)
                else:
                    self.update(child, parent=model)

        update_children(e)

        if e.tag == 'season':
            tree = self.get_tree('get_matches',
                                 type='season',
                                 id=model.gsm_id,
                                 detailed='yes')
            for sube in gsm.parse_element_for(tree, 'match'):
                if not self.skip(sube):
                    if 'double_A_id' in sube.attrib.keys():
                        continue  # important !!
                    self.update(sube, model)

        time.sleep(self.cooldown)
Beispiel #3
0
    def resync(self, element=None):
        if not element:
            detailed = self.sport.slug == 'tennis'
            tree = gsm.get_tree('en',
                                self.sport,
                                'get_matches',
                                type='match',
                                id=self.gsm_id,
                                detailed=detailed,
                                update=True,
                                retry=True)
            for e in gsm.parse_element_for(tree.getroot(), 'match'):
                element = e

        s = Sync(self.sport, False, False, False)
        s.update(e)
Beispiel #4
0
    def handle(self, *args, **options):
        self.status_path = os.path.join(settings.VAR_ROOT, 'gsm_last_updated')
        status = self.get_status()

        now = local.localize(datetime.datetime.now())
        last_updated = status['last_updated'] or now - datetime.timedelta(
            hours=6)
        if not last_updated.tzinfo:
            last_updated = local.localize(last_updated)
        minimal_date = now - datetime.timedelta(days=365 * 5)
        if not minimal_date.tzinfo:
            minimal_date = local.localize(minimal_date)

        for sport in Sport.objects.all():
            if args and sport.slug not in args:
                continue

            sync = Sync(sport,
                        last_updated,
                        minimal_date,
                        logger,
                        language='en')

            if last_updated < now - datetime.timedelta(hours=23):
                start_date = now - datetime.timedelta(hours=22)
            else:
                start_date = last_updated

            root = sync.get_tree('get_deleted', start_date=start_date)
            for child in root.getchildren():
                if child.tag not in sync._tag_class_map:
                    continue

                children = child.getchildren()
                if children:
                    gsm_ids = [x.attrib['source_id'] for x in children]
                    sync._tag_class_map[child.tag].objects.filter(
                        tag=child.tag, gsm_id__in=gsm_ids,
                        sport=sport).delete()

            if sport.slug == 'soccer':
                root = sync.get_tree('get_matches_live',
                                     update=True,
                                     now_playing='yes')
                for e in gsm.parse_element_for(root, 'match'):
                    sync.update(e)

            for code, language in settings.LANGUAGES:
                sync = Sync(sport,
                            last_updated,
                            minimal_date,
                            logger,
                            language=code,
                            names_only=code != 'en')

                root = sync.get_tree('get_seasons',
                                     update=True,
                                     last_updated=last_updated)

                if not root:
                    logger.error('Did not get tree for get_seasons')
                    return

                for e in root.getchildren():
                    if e.tag == 'method' or sync.skip(e):
                        continue

                    if e.tag in sync._tag_class_map.keys():
                        sync.update(e)

        delta = local.localize(
            datetime.datetime.now()) + datetime.timedelta(minutes=7) - now
        status['last_updated'] = now - delta

        buggy_sessions = Session.objects.filter(
            status='Fixture',
            start_datetime__lte=datetime.datetime.now() -
            datetime.timedelta(hours=2))
        for session in buggy_sessions:
            tree = gsm.get_tree('en',
                                session.sport,
                                'get_matches',
                                retry=30,
                                update=True,
                                type=session.tag,
                                id=session.gsm_id,
                                detailed=True)
            for element in gsm.parse_element_for(tree.getroot(), 'match'):
                break
            d = datetime.datetime.now() - datetime.timedelta(days=6)
            sync = Sync(session.sport, d, d, logger)
            sync.update(element)

        if not args:
            self.store_status(status)

        print "DONE UPDATING", status
Beispiel #5
0
def correct_for_session(session, element=None):
    if element is None:
        tree = gsm.get_tree('en',
                            session.sport,
                            'get_matches',
                            retry=30,
                            type=session.tag,
                            id=session.gsm_id,
                            detailed=True)

        for element in gsm.parse_element_for(tree.getroot(), 'match'):
            break

    from bet.models import *
    User = get_model('auth', 'user')
    BetType = get_model('bookmaker', 'bettype')

    if element.attrib['status'] in ('Fixture', 'Playing'):
        return

    if element.attrib['status'] == 'Cancelled':
        Bet.objects.filter(session=session).update(
            correction=BET_CORRECTION_CANCELED)
        return

    rewrite = (
        'fs_A',
        'fs_B',
        'hts_A',
        'hts_B',
        'ets_A',
        'ets_B',
        'p1s_A',
        'p1s_B',
        'p2s_A',
        'p2s_B',
        'p3s_A',
        'p3s_B',
        'p4s_A',
        'p4s_B',
        'ps_A',
        'ps_B',
        'eps_A',
        'eps_B',
    )

    attrib = {}
    for k, v in element.attrib.items():
        if v.isdigit():
            attrib[k] = float(v)
        else:
            attrib[k] = v

    to_update = User.objects.filter(ticket__bet__session=session).distinct()
    to_correct = BetType.objects.filter(bet__session=session,
                                        variable_type=None).distinct()

    for t in to_correct:
        if t.cancel_condition:
            try:
                condition = t.cancel_condition
                if condition is None:
                    raise
                for var in rewrite:
                    condition = condition.replace(var, 'attrib["%s"]' % var)
                result = eval(condition)
                if result:
                    Bet.objects.filter(
                        session=session,
                        bettype=t).update(correction=BET_CORRECTION_CANCELED)
                    continue
            except:
                pass

        for c in t.betchoice_set.all():
            bets = Bet.objects.filter(session=session, bettype=t, choice=c)

            try:
                condition = c.condition
                if condition is None:
                    raise
                for var in rewrite:
                    condition = condition.replace(var, 'attrib["%s"]' % var)

                set = []
                for child in element.getchildren():
                    if child.tag == 'set':
                        set.append(element.attrib)

                result = eval(condition)
                if result:
                    correction = BET_CORRECTION_WON
                else:
                    correction = BET_CORRECTION_LOST
                bets.update(correction=correction)
            except:
                bets.update(flagged=True)

    for u in to_update.values_list('pk', flat=True):
        refresh_betprofile_for_user_nospool({'userpk': u})

    # the next part uses bet.save() which should trigger profile refresh

    to_correct = Bet.objects.filter(session=session).exclude(
        bettype__variable_type=None).distinct()

    goalers = []
    goalers_with_extra = []
    goal_elements = element.findall('goals/goal/event') or []
    for e in goal_elements:
        if e.attrib['code'] != 'G':
            pass

        if not e.attrib.get('minute_extra', None):
            goalers.append(float(e.attrib['person_id']))

        goalers_with_extra.append(float(e.attrib['person_id']))

    for bet in to_correct:
        try:
            condition = bet.choice.condition
            if condition is None:
                raise
            for var in rewrite:
                condition = condition.replace(var, 'attrib["%s"]' % var)

            set = []
            for child in element.getchildren():
                if child.tag == 'set':
                    set.append(element.attrib)

            result = eval(condition)
            if result:
                correction = BET_CORRECTION_WON
            else:
                correction = BET_CORRECTION_LOST
            bet.correction = correction
        except:
            bet.flagged = True
        bet.save()