As a huge NY Mets and Twitter fan, I thought it would be fun to create a Twitter bot that tweets whenever a play from the Mets hits a home run.
I was able to easily authenticate my bot using tweepy. No issues here.
I then started researching a real-time Major League Baseball API connection. I was able to find the following API on Github Repo. Through this API library I was able to create variables and functions that accessed scoring data from todays game.
Problem #1: The function that retrieves scoring play data from the API returns all scoring play data from the game. The function I built below, uses re.search to look for the word 'homer' in todays_game_log which is a variable created off of the aforementioned function.
def mets_homerun_finder(todays_game_log): if(re.search(mets_players_saved+'homer',todays_game_log)): api.update_status("Mets homerun") else: pass
If you look at the data returned by printing the variable you receive a formatted string of all scoring plays. (ignore team names)
print(todays_game_log) Nick Castellanos doubles (16) on a sharp line drive to right fielder Juan Soto. Kyle Schwarber scores. Bryce Harper scores.Top 1 - Philadelphia Phillies: 2, Washington Nationals: 0Odubel Herrera grounds out, second baseman Cesar Hernandez to first baseman Josh Bell. Bryce Harper scores. Nick Castellanos to 3rd.Top 3 - Philadelphia Phillies: 3, Washington Nationals: 0Alec Bohm doubles (9) on a sharp fly ball to right fielder Juan Soto. Nick Castellanos scores.Top 3 - Philadelphia Phillies: 4, Washington Nationals: 0
However I would only want my function to reference the newest update and not prior updates I need help understanding how to make sure my function is only checking for new updates. not completely sure how to do this. There is JSON data that I could access from the API that has realtime data which I could potentially pivot into a new function but as I'm still a beginner that could get messy.
Problem #2: I only want to look for homeruns by Mets players and not other teams. My initial solve was to access the team's roster data from the API and create a variable list to store each players names. Then I would be able to have the function check for each player's name followed by string 'homer'. I was successful in creating the list but I am unsure how to use re.search to scan through the list and find matches in the passed back data.
print(mets_players_saved)
['Adam Ottavino', 'Adonis Medina', 'Brandon Nimmo', 'Carlos Carrasco', 'Chasen Shreve', 'Chris Bassitt', 'David Peterson', 'Dominic Smith', 'Drew Smith', 'Eduardo Escobar', 'Edwin Diaz', 'Francisco Lindor', 'J.D. Davis', 'James McCann', 'Jeff McNeil', 'Joely Rodriguez', 'Luis Guillorme', 'Mark Canha', 'Nick Plummer', 'Pete Alonso', 'Seth Lugo', 'Starling Marte', 'Taijuan Walker', 'Tomas Nido', 'Tommy Hunter', 'Trevor Williams']
Should I be thinking about how to create the trigger differently? Or do I need to just learn more Python to solve this?