Retrieve your blog posts from WordPress using REST API


Most of the applications and web services nowadays provide a REST API for interaction with their services. So, it is natural to wonder about REST. To put simply, REST is a simple way to organize interactions between independent systems. What this means is that it allows you to interact with services without much fuss and in a structured way.

The impetus for this post comes from the fact that I was looking for an way to get a list of all posts that I have written in the recent past which are tagged as PowerShell. Sure, I can browse by the tag and get the results; but I wanted to have a complete list at hand. That is where the WordPress API documentation came in handy.

If you have PowerShell V3, then you can the Invoke-RestMethod cmdlet to parse the results into objects and play around with them.

The structure of the URI for GETting posts is of the form:

https://public-api.wordpress.com/rest/v1/sites/$site/posts/

Where $site is the name of your site.

For my site the query is:

https://public-api.wordpress.com/rest/v1/sites/sqlchow.wordpress.com/posts/?number=50

Where 50 is the number of posts that I want to retrieve. How about the tag, you say? The query is only slightly different:

https://public-api.wordpress.com/rest/v1/sites/sqlchow.wordpress.com/posts/?number=50&tag=PowerShell

Since Invoke-RestMethod automatically converts the returned data into objects based on their type, we need not worry about the kind of response we get from the web page.

All we need to do is query the service and get our data.

$sqlchowBlogData = Invoke-RestMethod -uri "https://public-api.wordpress.com/rest/v1/sites/sqlchow.wordpress.com/posts/?number=50&tag=PowerShell"

$sqlchowBlogData | Get-Member
TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
found       NoteProperty System.Int32 found=28
posts       NoteProperty System.Object[] posts=System.Object[]

Let us check, how many posts we found:

$sqlchowBlogData.found
28

Now, let us see we can get all the post titles and the dates they were published. The data that we need is in the posts object. Let’s get the data.

$sqlchowBlogData.Posts | Select Title, @{Name="Published Date"; Expression={Get-Date ($_.Date) -Format "MMM-dd-yyyy"}} | Format-Table -AutoSize

title                                                                              Published Date
-----                                                                              --------------
Windows Azure setup for using with PowerShell                                      Mar-30-2014
Using verbal expressions to make regex easy in PowerShell                          Sep-07-2013
Down the rabbit hole with PowerShell and Windows Azure SQL Database                Sep-02-2013
Google search results using REST (custom search api) in PowerShell                 Aug-19-2013
Filtering data from SQL*Server Errorlogs                                           Aug-18-2013
.......
.......
Reading a tweeters timeline using PowerShell-Take2                                 Dec-27-2011
Reading a tweeters timeline using PowerShell                                       Dec-26-2011

Now that we have the details, we can use ConverTo-HTML to maintain an offline copy of certain posts that we need.

RESTful services are all around us, PowerShell gives us an easy way to interact with them. The possibilities are endless šŸ™‚ .

Cheers!!

Advertisements
About

By profession, Iā€™m a SQL Server Database Administrator. I love to poke my nose into different corners and see how stuff looks in there. I keep looking for new things to do as my mind refuses to settle on one topic.

Tagged with: , , ,
Posted in Fun-Stuff, PowerShell

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

%d bloggers like this: