def create_match_from_json(team1, team2, match_json):
    global total_requests

    try:
        match_ext_id = match_json['id']
        print("Processing match with ID %s" % match_ext_id)
        match = Match.objects.get(external_id=match_ext_id)
    except Match.DoesNotExist:
        # Continue creating match
        print("Creating new match from json..")
        localteam_id = match_json['localteam_id']
        visitorteam_id = match_json['visitorteam_id']
        if int(team1.external_id) == localteam_id and int(
                team2.external_id) == visitorteam_id:
            local_team = team1
            visitor_team = team2
        else:
            local_team = team2
            visitor_team = team1

        match = Match()
        match.external_id = match_ext_id
        match.stage = None
        match.status = Match.FINISHED
        match.stage_detail = match_json['stage']['data']['name'].lower()
        from datetime import datetime
        datetime_str = match_json['time']['starting_at']['date_time']
        datetime_object = datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S')
        match.date = datetime_object
        match.stadium = match_json['venue']['data']['name']
        match.team1 = local_team
        match.team1_score = match_json['scores']['localteam_score']
        match.team2 = visitor_team
        match.team2_score = match_json['scores']['visitorteam_score']
        match.is_live = False
        match.is_history = True  # Historical match <<<
        match.save()
        print("Match saved: %s" % match)
    else:
        print("Match %s already exist (skiped).." % match)
    return match
Пример #2
0
def create_match_from_json(match_json,
                           competition_id,
                           stage_name,
                           set_live=False):
    global total_requests
    try:  # First try to get or create teh country
        print("Looking for competition with ID %s .." % competition_id)
        competition = Competition.objects.get(external_id=competition_id)
    except Exception as e:
        print("Error Looking for competition with ID %s .." % competition_id)
        exit(1)
    try:
        match_ext_id = match_json['id']
        print("Processing match with ID %s" % match_ext_id)
        match = Match.objects.get(external_id=match_ext_id)
    except Match.DoesNotExist:
        # Get or create teams
        try:
            team1_ext_id = match_json['localteam_id']
            print("Looking for team1 ext ID %s.." % team1_ext_id)
            team1 = Team.objects.get(competition=competition,
                                     external_id=team1_ext_id)
        except Team.DoesNotExist:
            print("Team with id %s not found" % team1_ext_id)
            return None
        else:
            print("Team 1 found: %s" % team1)

        try:
            team2_ext_id = match_json['visitorteam_id']
            print("Looking for team1 ext ID %s.." % team2_ext_id)
            team2 = Team.objects.get(competition=competition,
                                     external_id=team2_ext_id)
        except Team.DoesNotExist:
            print("Team with id %s not found" % team2_ext_id)
            return None
        else:
            print("Team 2 found: %s" % team2)

        # Continue creating match
        print("Creating new match from json..")
        match = Match()
        match.external_id = match_ext_id
        match.stage = CompetitionStage.objects.get(competition=competition,
                                                   name=stage_name)
        try:
            group_name = match_json['group']['data']['name']
        except Exception:
            try:
                group_name = match_json['stage']['data']['name']
            except Exception:
                group_name = ""
        match.stage_detail = group_name
        from datetime import datetime
        datetime_str = match_json['time']['starting_at']['date_time']
        datetime_object = datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S')
        match.date = datetime_object
        match.stadium = match_json['venue']['data']['name']
        match.team1 = team1
        match.team1_score = match_json['scores']['localteam_score']
        match.team2 = team2
        match.team2_score = match_json['scores']['visitorteam_score']
        match.is_live = set_live
        match.save()
        print("Match saved: %s" % match)
    else:
        print("Match %s already exist (skiped).." % match)
    return match