How do I win free Pepsi?
April 7, 2022 4:07 PM   Subscribe

This is not a joke: Everytime the KC Royals get a pick off, local grocery stores give out a free six-pack of Pepsi. Throughout the season they have similar giveaways. Are there any MLB APIs I can use (paid or free) so I can setup an AWS Lambda or something similar to tweet/email/etc. when this happens? I tried last year with a similar promotion but I could not find any MLB APIs. I know how stat heavy baseball is so somewhere these APIs exist. This is for a friend's kids to teach them how to program.

I'm not concerned with actually putting together a curriculum if I can get the APIs. The free pepsi offer is only good the following day so if the API is only updated at the end of the week or whatever it wouldn't really be helpful.

Today was the first game so I thought I'd give it a try again to see if I can get something together. It really is just to give the kids a chance to build something and actually see something work over a period of time. Who knows they may get creative and think of better ways of delivering it than a tweet.

Thanks!
posted by geoff. to Computers & Internet (14 answers total) 3 users marked this as a favorite
 
The problem here is a lot of this data cost $$$.

I think https://sportsdata.io/developers/data-dictionary/mlb#teamgame have "caught stealing" stat available.

So if you poll it before the game, and then after the game, and look for a delta in the the value. Positive delta would mean there's a pick off?

No idea if that specific stat cost money though.
posted by kschang at 4:25 PM on April 7, 2022


Response by poster: PK or Pick Off will be distinct from Caught Stealing, but it looks like it has it:

The result of the play. Possible values include: Batter's Interference, Bunted into Double Play, Catcher's Interference, Double, Error, Fielder's Choice, Fly into Double Play, Fly Out, Foul Out, Fouled into Double Play, Ground into Double Play, Ground Out, Hit by Pitch, Home Run, Infield Fly Out, Intentional Walk, Line into Double Play, Lineout, Pop Out, Popped into Double Play, Sacrifice, Sacrifice Fly, Single, Strikeout Bunting, Strikeout Looking, Strikeout Swinging, Triple, Triple Play, Walk, Stolen Base, Caught Stealing, Passed Ball, Wild Pitch, Pick Off, Balk, Error, Fielders Indifference

The fact I cannot get a cost estimate without even contacting them is not very comforting. Thanks! I was hoping the cost would be tied to API calls and I could get away with $20/mo or something knowing that data is expensive.
posted by geoff. at 4:45 PM on April 7, 2022


Looking forward to your FanFare post about the Pepsi commercial in which Michael Jackson’s hair caught fire.
posted by ejs at 5:18 PM on April 7, 2022 [5 favorites]


MySportsFeeds promises a free non-live data feed, though they won't say how often is their "non-live" feed updated.

Link
posted by kschang at 5:19 PM on April 7, 2022


It’s not an API, but this page at Baseball Reference will have links to the box scores after each game (you can use the 2021 results as an example). From the box scores, it’s a little bit of regex to see if there were any pickoffs in the game.

(Caveat: I’m not a programmer, so this may be way harder than I think)
posted by griseus at 5:52 PM on April 7, 2022


If we're going down the road of parsing a web page, Beautiful Soup may be helpful.
posted by icebergs at 6:20 PM on April 7, 2022


Best answer: You want this Python Library.

Alternatively, you can make raw calls to statsapi.mlb.com, which has all of its endpoints laid out in that python code. This is the backend that powers GameCast.

This is all free.
posted by bowbeacon at 6:24 PM on April 7, 2022 [1 favorite]


Response by poster: Thanks! That looks close and it looks like from this undocumented SwaggerAPI isPickOff might be what I need:

Insert isPickoff to return either all GUIDS corresponding to pickoffs{true} or non-pickoff events{false}.\n1. Insert isPickoff true:

https://statsapi.mlb.com/api/v1/game/565798/guids?gameModeId=2&isPickoff=true\n2.

Insert isPickoff false:

https://statsapi.mlb.com/api/v1/game/565798/guids?gameModeId=2&isPickoff=false\n


There appears to be no formal data dictionary even with the Python library and it'll involve some guessing. Hopefully it is free and somewhat accurate as it looks like with some educated guesses and looking at past games I can create a wrapper and maybe a simple API call that's more or less, "Did a Pick Off Happen?"

Out of curiosity does anyone know how grocery stores get this information in general? I'm assuming they use the honor system and their ERP system doesn't update anything but I could be wrong.

The reason I ask is that another API I happened to find right before this had pitcher stats that showed pick offs as in, "Pitcher got a pick off" which may or may not encompass pitcher and catcher getting a pickoff or just the pitcher. I don't know how that's scored in baseball but I wouldn't be surprised if a catcher throwing a pickoff would get scored under a pitcher for some reason.
posted by geoff. at 6:43 PM on April 7, 2022


Response by poster: Hmmmm not so fast. First the API is sort of available. It is completely undocumented and not all endpoints return something without logging in. The Python wrapper is a good start to kind of begin guessing but nothing is obvious.

Taking this game with a known pick off (4/13/2021, Royals C Perez ends game with pickoff, KC beats Angels 3-2), then finding the six digit "gameId" by going to the MLB.com website and finding the game yields: 632210 in the query string.

However the stats don't seem to indicate if a pickoff was successful:

Game 632210 JSON payload... given that it is the last play and a pickoff it should be simple but the last play listed:

"result" : {
"type" : "atBat",
"event" : "Runner Out",
"eventType" : "other_out",
"description" : "Angels challenged (tag play), call on the field was upheld: David Fletcher out at home, catcher Salvador Perez to third baseman Hunter Dozier.",
"rbi" : 0,
"awayScore" : 2,
"homeScore" : 3
},


No mention of a pickoff. There seems to be "Pickoff Attempt 1B/2B/3B" which I could build a query on but I don't see how you see if a Pickoff Attempt is successful. This particular play may have just been coded wrong.

Here's another pickoff that's conveniently the last play: 9/29/2016 Royals lose on pickoff play where Gore was tying run in 7-6 game.

Following similar logic yields 449249, which in the box score shows a pickoff! And in the JSON payload appears correct:


"result" : {
"type" : "atBat",
"event" : "Pickoff 1B",
"eventType" : "pickoff_1b",
"description" : "Twins challenged (tag play), call on the field was overturned: pitcher Brandon Kintzler picks off Terrance Gore at on throw to first baseman Kennys Vargas.",
"rbi" : 0,
"awayScore" : 7,
"homeScore" : 6
},
"about" : {
"atBatIndex" : 82,
"halfInning" : "bottom",
"isTopInning" : false,
"inning" : 9,
"startTime" : "2016-09-30T02:42:49.350Z",
"endTime" : "2016-09-30T02:30:10.271Z",
"isComplete" : true,
"isScoringPlay" : false,
"hasReview" : true,
"hasOut" : true,
"captivatingIndex" : 16
},


If anyone has any insight as to why the first one I randomly chose didn't show the pickoff? It does look like I can simply look for "Pickoff 1B/2B/3B" without attempt and maybe the very first one I tried was bad data or the report from USA Today was wrong. MLB does have a video on their website for the 4/13/2021 game labeling it as a pickoff. I'd assume if it was automated the stats data would align.

I did one last check from today's game (4/7/2022 Royals vs the Cleveland Guardians neé Indians) and the JSON payload is available! It is a bit funky how the data is delivered as the data structure changes if the pickoff is successful or not apparently. I think I'm there but if anyone has any other insights or something I missed let me know.
posted by geoff. at 7:24 PM on April 7, 2022


Best answer: While this may be overthinking it, I think you need to know how the contest defines a pickoff and how the MLB does too. Pitcher to any base is an obvious pickoff. Catcher throwing behind the runner or to the base at which the runner started the play before the pitch that got the catcher the ball would be a pickoff too. The rules seem to agree.

What if the runner gets into a rundown? It is a caught stealing if he is tagged out going to the next base, but what if he is tagged out sliding back into the originating base? What about an extra assist? What if the catcher throws to first behind the runner after a pitch is thrown and the ball is overthrown but goes right to the right fielder who is backing up the play. The runner momentarily hesitates and then tried to get back to first but the right fielder throws back to the first baseman who easily applies the tag. Is that a pickoff?

But forget about all that. Reading through the rules, it seems as if Price Chopper does this for you. They will send an email and post it in their app. You need to "clip" the coupon and then buy at least $25 worth of goods to get the "free" Pepsi. Why not focus on getting notified when Price Chopper posts the change to their app? The change comes when they post the coupon for the free Pepsi. Set up something to get notified and then CLIP the new coupon. Then write a reminder to shop 12 hours (or whenever) the coupon expires.

Beating Price Chopper to the change in stats (a pickoff) does you no good anyway. You still need to wait for the coupon to be posted. I know this is an exercise for a friend's kid, but it can still be a good exercise if you focus the program or query to after it is in the app rather than when it happens on the field.

Going back to your original idea, I use the NY Times site to follow the box score of the NY Yankees in near real time. It updates once a minute I think. At the bottom of their bax score pages, they say "Powered By" and Stats Perform. I went to the site and it appears to be what you want or a really good lead. Probably have to pay for their data.

I know nothing about programming so if this is offbase (pun intended) apologies.
posted by JohnnyGunn at 7:54 PM on April 7, 2022 [2 favorites]


Response by poster: But forget about all that. Reading through the rules, it seems as if Price Chopper does this for you. They will send an email and post it in their app. You need to "clip" the coupon and then buy at least $25 worth of goods to get the "free" Pepsi. Why not focus on getting notified when Price Chopper posts the change to their app? The change comes when they post the coupon for the free Pepsi. Set up something to get notified and then CLIP the new coupon. Then write a reminder to shop 12 hours (or whenever) the coupon expires.

What?! I missed that because they have run a version of this promotion for years and the commercials by the announcers also didn't mention the minimum purchase, likely for the same reason I didn't even notice the new requirements. Well thanks and now I'm disappointed. Again, this has run for years and it is always some stat.

They're going to ruin a lot of kids afternoons after school. It used to be a single bottle of coke. Oh well, it might still be fun to do this and then just buy the kids a coke. Though it takes some fun out of it.
posted by geoff. at 8:26 PM on April 7, 2022


You can set this up yourself as the giveaway arbiter. The first time it happens, buy 5 or 6 six packs of Pepsi which is probably about $25. Get the extra "Free" one. Then, whenever it happens as friend's kid is notified by his own app/query, you give him one or two of the Pepsi's. He can still write the program. You can confirm it in the Price Chopper app, and you save the trip to the Chopper.

Btw, I am not so sure the minimum purchase is a new requirement. Free is never free.

Oh, this is a bs promotion anyway. As far as I can see, the KC Royals had a total of 8 pickoffs in 2021. 2nd column from the far right. Salvador Perez had 2 and six others had one. (By comparison, the Dodgers had 11 of which 4(!) of them were Clayton Kershaw. TB had 11 and the Astros 7, etc.) Chopper is not taking a lot of financial risk.
posted by JohnnyGunn at 9:09 PM on April 7, 2022 [1 favorite]


Response by poster: Yeah so for some history it used to be "Two-hit Whit" and supported Whit Merrifield's charity of the same name. Anytime he got two hits in any game you'd receive a 20 oz Pepsi. Apparently it was with a purchase which was never a problem as I'd go in buy cigarettes for my dad and get the free Pepsi. Probably a pretty good deal for them as I'd not go in otherwise. A cursory look and a couple years ago Whit was getting 40+ games where he'd get 2 hits. They changed it in the middle of the year to $25 minimum purchase. I'm guessing my local cashier did not notice or care.

Edit: They're not even full cans! Pepsi Mini 6-pk product
posted by geoff. at 10:01 PM on April 7, 2022 [1 favorite]


Response by poster: And pickoffs are really confusing, you're right. The most widely accepted definition of a pickoff seems to be that the runner does not try to advance and the pitcher gets the player out since it is not an attempted stolen base. From the official MLB rules under "Stolen Bases" there's no formal definition for the scorer to count a pickoff. A pickoff is not an actual stat apparently. And of note the MLB rules sometimes refer to it as a "pick-off" and other times as a "pickoff."

Looking at the play in question it is hard to tell if Fletcher actually tried to advanced, "caught stealing," and I didn't notice it originally as scored simply "other_out."

So this is a rather unusual play I just happened to randomly stumble upon as my first one unfortunately. I think by the MLB's own somewhat murky stat but not a stat definition it was not a pickoff due to the wild pitch at least.
posted by geoff. at 10:49 PM on April 7, 2022 [1 favorite]


« Older Is it legal for my landlord to increase my rent...   |   ISO simple screw top, leak proof travel mug that... Newer »
This thread is closed to new comments.