Get latest 1000 google reader starred items

Google announced a refresh of its RSS reader and allows to export your data to a Json format.
http://googlereader.blogspot.com/2011/10/upcoming-changes-to-reader-new-look-new.html

This is also an opportunity to test some of the new cmdlets of Powershell 3.0 CTP2.
http://www.microsoft.com/download/en/details.aspx?id=27548

Unfortunately, the export page of the google reader activity can only be reached after being authenticated.
Nevertheless, there’s a public page that comes into play and that can do the trick but there’s a limitation as it can only get a maximum of 1000 items.

Here is the full script that uses the new built-in cmdlets ConvertFrom-JSON and Invoke-WebRequest.

#Requires -Version 3.0

<#
    
.SYNOPSIS    
    Get latest 1000 starred items of my profile
   
.DESCRIPTION  
    Get latest 1000 starred items of my profile
     
.NOTES    
    Name: Get-GoogleStarredItems.ps1
    Author: Emin Atac
    DateCreated: 04/01/2012
     
.LINK    
    https://p0w3rsh3ll.wordpress.com
     
.EXAMPLE    
    Get-GoogleStarredItems.ps1 | fl -property *
    Pipe it into format-list and show all the returned properties

.EXAMPLE    
    (& { .\Get-GoogleStarredItems.ps1})[0]
    Show the first element in the array

.EXAMPLE    
    (.\Get-GoogleStarredItems.ps1 | Where-Object {$_.PublishDate -Match "\d{2}/\d{2}/2011"}).Count   
    Count how many starred items have been tagged in 2011 using the V2 syntax

.EXAMPLE
    (.\Get-GoogleStarredItems.ps1 | Where-Object PublishDate -Match "\d{2}/\d{2}/2011").Count
    Count how many starred items have been tagged in 2011 using the V3 syntax

.EXAMPLE
    .\Get-GoogleStarredItems.ps1 | Select-Object -Property URL | Where URL -match "sans"
    Get all the URL that contains SANS using the V3 syntax
#>


# http://thepowershellguy.com/blogs/posh/archive/2008/02/29/hey-powershell-guy-how-to-translate-a-unix-timestamp-to-a-local-time.aspx
Function Get-Unixdate ($UnixDate)
{
    [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'01/01/1970 00:00:00').AddSeconds($UnixDate))
}

# Maximum item that can be returned by this URL is 1000
$url = "http://www.google.com/reader/public/javascript/user/17544532755091407225/state/com.google/starred?n=1000"
    
try
{
    $wr = Invoke-WebRequest -Method GET -Uri $url -ErrorAction SilentlyContinue
}
catch
{
    $_
}
finally {}
    
if ($wr.StatusCode -eq 200)
{
    $content  = $wr.Content
} 
else
{
    Write-Warning -Message "Error code ($wr.StatusCode) returned"
    exit
}

# Convert the content to Json
$allitems = (("[$content]" | ConvertFrom-JSON) | Select-Object -ExpandProperty items)

# Prepare an empty array
$finalarray = @()

foreach ($item in $allitems)
{
    $finalarray += New-Object PSObject -Property @{
        Title = $item.title
        Summary = $item.summary
        BlogName = $item.origin.title
        BlogUrl = $item.origin.htmlUrl
        Origin = $item.origin.streamId
        URL = $item.alternate.href
        PublishDate = (Get-Unixdate -UnixDate ($item.published))
        }
} # end of foreach

# Return our array
return $finalarray

As a bonus, there are examples that show the syntax differences between V2 and V3 for the ‘foreach’ loop. Here you go 😉

Get-Help .\Get-GoogleStarredItems.ps1 -Full
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s