Advertisement
cristianpark

Untitled

Mar 13th, 2023 (edited)
1,238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | Source Code | 0 0
  1. import requests
  2. import json
  3. import datetime
  4.  
  5. # Set up API endpoint and headers
  6. url = "https://v3.football.api-sports.io/"
  7. endpoint = "fixtures"
  8. headers = {
  9.     "x-rapidapi-host": "v3.football.api-sports.io",
  10.     "x-rapidapi-key": "YOUR_API_KEY"
  11. }
  12.  
  13. # Set up query parameters
  14. params = {
  15.     "league": "301",
  16.     "season": "2023",
  17.     "dateFrom": "START_DATE",
  18.     "dateTo": "END_DATE",
  19.     "timezone": "America/Bogota"
  20. }
  21.  
  22. # Make the API request
  23. response = requests.get(url + endpoint, headers=headers, params=params)
  24.  
  25. # Convert the response to a JSON object
  26. data = json.loads(response.text)
  27.  
  28. # Parse the data and extract relevant information
  29. for match in data["response"]:
  30.     # Get the match ID
  31.     match_id = match["fixture"]["id"]
  32.    
  33.     # Get the match date and convert it to a datetime object
  34.     match_date_str = match["fixture"]["date"]
  35.     match_date = datetime.datetime.strptime(match_date_str, "%Y-%m-%dT%H:%M:%S+00:00")
  36.    
  37.     # Get the home and away team IDs
  38.     home_team_id = match["teams"]["home"]["id"]
  39.     away_team_id = match["teams"]["away"]["id"]
  40.    
  41.     # Get the scores for the match
  42.     home_score = match["goals"]["home"]
  43.     away_score = match["goals"]["away"]
  44.    
  45.     # Get the ball possession for the home and away teams
  46.     home_possession = match["statistics"]["possession"]["home"]
  47.     away_possession = match["statistics"]["possession"]["away"]
  48.    
  49.     # Get the yellow cards for the home and away teams
  50.     home_yellowcards = match["statistics"]["yellowcards"]["home"]
  51.     away_yellowcards = match["statistics"]["yellowcards"]["away"]
  52.    
  53.     # Determine which team had more yellow cards and more ball possession
  54.     if home_yellowcards > away_yellowcards:
  55.         most_yellowcards_team_id = home_team_id
  56.     elif away_yellowcards > home_yellowcards:
  57.         most_yellowcards_team_id = away_team_id
  58.     else:
  59.         most_yellowcards_team_id = None
  60.    
  61.     if home_possession > away_possession:
  62.         more_possession_team_id = home_team_id
  63.     elif away_possession > home_possession:
  64.         more_possession_team_id = away_team_id
  65.     else:
  66.         more_possession_team_id = None
  67.    
  68.     # Insert the match data into the database
  69.     # INSERT INTO gnspartido (partido_id, partido_fecha, partido_marcadorlocal,
  70.     # partido_marcadorvisitante, partido_resultado, equipo_local_id, equipo_visitante_id,
  71.     # equipo_id_amarillas, equipo_id_posesion, partido_zonah, partido_automatizacionid)
  72.     # VALUES (match_id, match_date, home_score, away_score, 'L' if home_score < away_score else 'V' if home_score > away_score else 'E',
  73.     # home_team_id, away_team_id, most_yellowcards_team_id, more_possession_team_id, -5, 'API-{}'.format(match_id))
  74.     print(f"Match ID: {match_id}")
  75.     print(f"Match Date: {match_date}")
  76.     print(f"Home Team ID: {home_team
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement