Once again, the holiday shopping season is upon us. But perhaps PowerShell can make it a little easier or at least a bit more fun. I'm sure many of you have shopped at NewEgg.com. Perhaps you plan to do so again this year for friends, family or even yourself. So why not let PowerShell make this a bit easier.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
NewEgg is savvy enough to publish RSS feeds for a number of their sales categories. You can find a master list at http://www.newegg.com/RSS/Index.aspx. Let's take their Deal of the Day feed.
$uri = "http://www.newegg.com/Product/RSS.aspx?Submit=RSSDailyDeals&Depa=0"
Using Invoke-RestMethod, it is very easy to retrieve items.
Invoke-RestMethod -Uri $uri
But it is still a bit of a jumble, so let's get a bit more selective.
$data = Invoke-RestMethod -Uri $uri | Select -property @{Name="Published";Expression= {$_.pubDate -as [datetime]}},Title,Link
I also created a new property called Published which takes the original PubDate and treats it as a date which makes the data easier to sort or filter.
Here's a sample of what I retrieved.
A NewEgg Deal of Day item (Image Credit: Jeff Hicks)
With this data, I can use Out-Gridview as an object-picker.
$data | out-gridview -Title "Deal of the Day" -OutputMode Multiple | foreach { Start $_.link }
Links in Out-Gridview (Image Credit: Jeff Hicks)
I can select multiple entries, click OK and each link should open up in my browser.
The online deal (Image Credit: Jeff Hicks)
But let's make things a bit more interesting. The Title property includes the price and description. I have no idea what these links look like in other parts of the world so you may have to adjust the following examples.
First, I'm going to define a regular expression pattern to use named captures to get the currency, in my case $, the price and the item description.
[regex]$rx = "(?<currency>.)(?<price>\d+\.\d{2})\s-\s(?<item>.*)"
I'm going to re-download the data skipping any entry that doesn't have what looks like a price in the title field.
$dl = Invoke-RestMethod -Uri $uri | where {$_.Title -match "\d+\.\d{2}"}
My goal is to use Out-Gridview again with separate properties for the currency and price. I need the price to be numeric so that I can sort on it. I next get the currency symbol using the regex expression.
$c = $rx.match($dl[0].title).groups["currency"].value
Then I can process the rest of the RSS data, using the regex object to parse out the price and description.
$data = $dl | Select -property @{Name="Published";Expression= {$_.pubDate -as [datetime]}}, @{Name=$c;Expression = {$rx.match($_.title).groups["price"].value -as [double]}}, @{Name="Item";Expression = {$rx.match($_.title).groups["item"].value }}, Link
If you notice, I used the Currency value as a property name. Now when I use Out-Gridview I have a more flexible display.
$data | Sort $c –descending | Out-GridView -Title $uri -OutputMode Multiple | foreach { #open each selected link start $_.link }
Reformatted Deals (Image Credit: Jeff Hicks)
If I'm shopping for a new laptop, I can select multiple entries, click OK and review them in my browser.
Viewing my choices (Image Credit: Jeff Hicks)
I can repeat the process by changing the RSS feed, say to their Shell Shocker
$uri = "http://www.newegg.com/Product/RSS.aspx?Submit=ShellShocker"
If I repeat the previous steps, this will fail, which brings up something to keep in mind with regular expressions: know your data. You have to know what you are processing and that it follows a predictable pattern. At least if you want to keep your regular expression patterns relatively simple. The problem here is that there is only a single item. So my code to get the currency figure fails, because I don't have an array. In this situation I could do this:
$c = $rx.match($dl.title).groups["currency"].value
Although it might make more sense to come up with code that I can re-use.
$entry = $dl | select -first 1 $c = $rx.match($dl.title).groups["currency"].value
But from here the code is the same.
Shell Shocker item (Image Credit: Jeff Hicks)
That might be something I want to look into, although sometimes the RSS feeds are bit behind the site. Sadly, in this case, the link works, but the product is something else. But you get the idea.
Enjoy your holiday weekend and be careful out there!
Exactly the stuff I like, Simply awesome!
Very interesting article. I’ve never been good with URI calls, so this is a neat and practical tutorial.