Friday Fun Get Beer List

beerWell, another Friday and what goes better with it than beer. Of course I should mix in a little PowerShell as well. I live in the Syracuse, NY area and we have a terrific local brewery, Middle Ages Brewing Company. Not only is there a tasting room, but I can also get growler refills. Middle Ages brews a number of beers throughout the year and beers available for growlers vary. Fortunately they post online what is currently available.

But I can’t be bothered to open a web browser and visit their site everytime I want to see what they have. That’s what PowerShell is for. Specifically PowerShell 3.0 and Invoke-Webrequest. With only a few lines of PowerShell, actually it could be done as a one-liner, I can get the list of beers available for growler refills. Let me break it down.

First, I’ll grab the web page with Invoke-WebRequest

<br />PS Scripts:\> $ma = Invoke-Webrequest http://middleagesbrewing.com/<br />

Invoke-Webrequest “packages” it as a nice object.

invoke-webrequest-middleages

On the page, the growler refills have links pages for the respective beers. This is handy because I can get all the links from the object I just pulled down.

<br />PS Scripts:\> $ma.links[-2]</p><p>innerHTML : <SPAN>Wailing Wench</SPAN><br />innerText : Wailing Wench<br />outerHTML : <A href="http://feedproxy.google.com/beers/wailing-wench"><SPAN>Wailing Wench</SPAN></A><br />outerText : Wailing Wench<br />tagName   : A<br />href      : /beers/wailing-wench<br />

Naturally there are other links as well, but after looking through them I realized all the ones I was interested in had /beers/ in the href property. Knowing that, I can filter.

<br />PS Scripts:\> $ma.links | where {$_.href -match "/beers/"}</p><p>innerHTML : <SPAN>Double Wit</SPAN><br />innerText : Double Wit<br />outerHTML : <A href="http://feedproxy.google.com/beers/double-wit"><SPAN>Double Wit</SPAN></A><br />outerText : Double Wit<br />tagName   : A<br />href      : /beers/double-wit</p><p>innerHTML : <SPAN>Druid Fluid</SPAN><br />innerText : Druid Fluid<br />outerHTML : <A href="http://feedproxy.google.com/beers/druid-fluid"><SPAN>Druid Fluid</SPAN></A><br />outerText : Druid Fluid<br />tagName   : A<br />href      : /beers/druid-fluid<br />...<br />

Excellent. All I need is to grab one of the text properties and I have my list.

<br />PS Scripts:\> $ma.links | where {$_.href -match "/beers/"}  | select -expand InnerText<br />Double Wit<br />Druid Fluid<br />ImPaled Ale<br />Kilt Tilter<br />Middle Ages Pale Ale<br />Session IPA<br />Swallow Wit<br />The Duke of Winship<br />Tripel Crown<br />Wailing Wench<br />

Perfect (and I’m getting a little thirsty). My script, such as it is comes down to two lines.

<br />$ma = Invoke-Webrequest http://middleagesbrewing.com/<br />$ma.links | where {$_.href -match "/beers/"}  | select -expand InnerText<br />

This could even be consolidated down to a one-liner:
<br />((Invoke-Webrequest http://middleagesbrewing.com/).Links | where {$_.href -match "/beers/"}).InnerText<br />

although there’s no practical reason to do so. If you are new to PowerShell that is a little more difficult to understand. Normally I prefer sending objects to the pipeline but all I really need to see are beer names so this works just fine for me.

I see they are filling growlers with the Duke of Winship porter, one of my faves, so I’d better wrap this up. Cheers!