Advertisement
Guest User

Help

a guest
Jun 7th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import re
  4. import time
  5.  
  6. game_urls = [
  7. "https://www.espn.com/mlb/boxscore/_/gameId/401569423",
  8. ]
  9.  
  10. players_to_track = [
  11. {"name": "M. Betts", "stat": "hits", "threshold": 1},
  12. {"name": "A. Volpe", "stat": "hits", "threshold": 1},
  13. {"name": "A. Judge", "stat": "hits", "threshold": 1},
  14. ]
  15.  
  16. headers = {
  17. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
  18. }
  19.  
  20. def get_player_stats(player_info, html_content):
  21. player_name = player_info['name']
  22. stat_to_track = player_info['stat']
  23. threshold = player_info['threshold']
  24.  
  25. # Construct regular expression pattern to match player stats
  26. pattern = r'<div class="full-name">.*?' + re.escape(player_name) + r'.*?<div class="info">(.*?)</div>'
  27.  
  28. # Find all instances of player stats
  29. player_stats = re.findall(pattern, html_content, re.DOTALL)
  30.  
  31. for stat in player_stats:
  32. # Extract hits using regular expression
  33. hits_match = re.search(r'<span class="number">(\d+)</span>', stat)
  34.  
  35. if hits_match:
  36. hits = int(hits_match.group(1))
  37.  
  38. if stat_to_track == "hits":
  39. return hits
  40.  
  41. return 0
  42.  
  43. def print_player_performance(player_name, stat_to_track, stat_value, threshold):
  44. if stat_value >= threshold:
  45. print("\033[92m" + f"{player_name}: {stat_to_track.replace('_', ' ').capitalize()} - {stat_value}" + "\033[0m")
  46. else:
  47. print("\033[91m" + f"{player_name}: {stat_to_track.replace('_', ' ').capitalize()} - {stat_value}" + "\033[0m")
  48.  
  49. def get_game_stats(game_urls, players_to_track):
  50. while True:
  51. for game_url in game_urls:
  52. try:
  53. response = requests.get(game_url, headers=headers)
  54. response.raise_for_status() # Raise an exception for bad status codes
  55. html_content = response.text
  56. for player_info in players_to_track:
  57. hits_home_runs = get_player_stats(player_info, html_content)
  58. print_player_performance(player_info['name'], player_info['stat'], hits_home_runs, player_info['threshold'])
  59.  
  60. except requests.exceptions.RequestException as e:
  61. print("Failed to retrieve game stats from", game_url, ". Error:", e)
  62. except Exception as e:
  63. print("An error occurred while processing", game_url, ". Error:", e)
  64.  
  65. time.sleep(60)
  66.  
  67. get_game_stats(game_urls, players_to_track)
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement