Skip to content

Instantly share code, notes, and snippets.

@1RedOne
Last active March 3, 2019 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1RedOne/24c5622b8e005ed14956d98b74b3be21 to your computer and use it in GitHub Desktop.
Save 1RedOne/24c5622b8e005ed14956d98b74b3be21 to your computer and use it in GitHub Desktop.
Parsing out foodtruck times with PowerShell and REST

Getting foodtruck time and locations in Bellvue

We got a request on FoxDeploy a few days ago asking the following:

I know its been awhile but im looking to write a fun little script that pulls the current food truck and perhaps the next one or two as well from seattlefoodtruck.com. For instance “https://www.seattlefoodtruck.com/schedule/plaza-east” The javascript that runs outputs the details but no matter what i do i cannot access the contents of the output. Any help on what im missing?

First off, we loaded up the URL in Chrome and then opened up devtools and went to the network tab, then refreshed. We're looking for XHR requests

Filtering down to XHR requests (which is what an AJAX request will basically always be) we see just one request. We have a good idea its going to be AJAX, as a nice website like this will most of the time break their components up into reusable modules or Partial Views, and will composite the whole thing together with a few requests.

Clicking into the request, we see a number of the restaurants listed here....looks like we're in the right neighborhood!

We can then copy the request like this...

And paste it into an Invoke-RestMethod cmdlet and then assign the results to a variable and play with them a bit...

$FoodTruckEvents = Invoke-RestMethod https://www.seattlefoodtruck.com/api/events?page=1&for_locations=51&with_active_trucks=true&include_bookings=true&with_booking_status=approved'
#this endpoint gives us a JSON response with multiple events, so we parse out each event
ForEach ($event in $FoodTruckEvents.events){
#each event seems to be one day, so we resolve the .start_time and make it human readable
$date = Get-Date $event.start_time | Select-Object -ExpandProperty DateTime
#each event could have more than one truck, so we step through them
ForEach ($booking in $event.bookings){
#create a new PowerShell object to output the info we need
[pscustomobject]@{Date=$date;TruckName=$booking.truck.name;FoodType=$booking.truck.food_categories -join ','}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment