Google search results using REST (custom search api) in PowerShell


Saw a tweet few minutes ago from Peter Kriegel(B|T) about a google query.

Find all people with word #powershell in their headline (bio) with Google: allintitle:”on twitter” “powershell” site:twitter.com

Since, I had the PowerShell console open I put the query in there to see how the data from the web request looked like. As you would have guessed, not much going on there. A quick google search 🙂 revealed that they have a custom search api which uses REST and returns the results as JSON. Bingo!

Before you run off to get you mandatory API key, there are a couple of things I need to point out about the restrictions, although your query can return 1000’s of results, you can only:

  1. You can get only 10 results per query.
  2. You can only make 100 requests per day.

Small consolation is that, you can still get the total number of search results returned.

Here is the quick and dirty code:

$gglquery = 'https://www.googleapis.com/customsearch/v1?key={MANDATORY-API-KEY}&cx=013036536707430787589:_pqjad5hr1a&q=allintitle%3A+PowerShell+site%3Atwitter.com&oq=all&alt=json'
$result = Invoke-RESTMethod -Uri $gglquery
$result.items | Select @{name='twitter_handle';expression={$($_.link.tostring()).replace('https://twitter.com/','@')}}

twitter_handle
--------------
@PowerShellMag
@PowerTip
@PhillyPoSH
@PoshCode
@PowerShellQuiz
@MTLPowershell
@cspshell
@flpsug
@AppVPowerShell
@SANSInstitute/status/361939924188008449 <--don't why this one got returned.

And the search information can be retrieved by doing:

$result.searchInformation | Select searchTime, formattedSearchTime, totalResults

searchTime formattedSearchTime totalResults
---------- ------------------- ------------
  0.067613 0.07                31

Cool stuff! Thanks ‘Peter Kriegel’ for posting the google query.

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
One comment on “Google search results using REST (custom search api) in PowerShell
  1. […] ← Google search results using REST (custom search api) in 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: