Skip to content

Commit

Permalink
params -o and -n
Browse files Browse the repository at this point in the history
  • Loading branch information
gfody committed Jul 12, 2016
1 parent 028380e commit 090686e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
Binary file modified PsGoogle.psd1
Binary file not shown.
24 changes: 12 additions & 12 deletions PsGoogle.psm1
@@ -1,15 +1,15 @@
Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Management.Automation

function google([Parameter(Position=0,Mandatory=$true,ValueFromRemainingArguments=$true)][string]$q, [int]$o) {
function google([Parameter(Position=0, Mandatory=$true, ValueFromRemainingArguments = $true)][string]$q, [int]$n = 4, [switch]$o) {
function parse-results([string]$html) {
$reg = new-object Text.RegularExpressions.Regex('class="r"><a href="/url\?q=(.*?)&amp;sa=.*?>(.*?)</a.*?<span class="st">(.*?)</span',
@([Text.RegularExpressions.RegexOptions]::IgnoreCase, [Text.RegularExpressions.RegexOptions]::Singleline, [Text.RegularExpressions.RegexOptions]::Compiled))
$reg.Matches($html) | % {
new-object PSObject -prop @{
'url' = [Web.HttpUtility]::UrlDecode([Web.HttpUtility]::UrlDecode($_.groups[1].value))
'title' = [Web.HttpUtility]::HtmlDecode($_.groups[2].value)
'summary' = [Web.HttpUtility]::HtmlDecode(($_.groups[3].value -replace '(?!</?b>)<.*?>', '')).Replace("`n", "")
'summary' = [Web.HttpUtility]::HtmlDecode(($_.groups[3].value -replace '(?!</?b>)<.*?>', '')).Replace("`n", "").Trim()
}
}
}
Expand All @@ -21,25 +21,25 @@ function google([Parameter(Position=0,Mandatory=$true,ValueFromRemainingArgument
write-host
}

$page = 0; $i = 1
while (!$o -or $o -gt $page) {
$raw = irm "https://www.google.com/search?q=$([Web.HttpUtility]::UrlEncode($q))&start=$(($page++)*10)"
$num = if ($n -gt 100) { 100 } else { $n }; $start = 0
while (!$o -or $start -lt $n) {
$raw = irm "https://www.google.com/search?q=$([Web.HttpUtility]::UrlEncode($q))&start=$start&num=$num"
$stats = ([regex]'id="resultStats">(.*?)<').match($raw).groups[1].value
if (!$stats) { if (!$o -and $i -eq 1) { write-host -foreground red "`nno results.`n" }; break }
if (!$stats) { if (!$o -and $start -eq 0) { write-host -foreground red "`nno results.`n" }; break }
$i = $start + 1; $start += $num
if ($o) { parse-results $raw } else {
$info = ([regex]'id="topstuff">(?:<(?!h3)[^>]*?>)+(?![<[])(.+?)</div>').match($raw)
if ($info.success) { write-host -foreground green "`n$([Web.HttpUtility]::HtmlDecode(($info.groups[1].value -replace '<.*?>', ' ' -replace ' {2,}', ' ')).Trim())" }
write-host -foreground yellow "`n$stats`n"
parse-results $raw | % {
write-bold "$(($i++)). $($_.title)"
write-host -foreground darkcyan $_.url
write-bold $_.summary
if (![string]::isNullOrWhitespace($_.summary)) { write-bold $_.summary }
write-host
if ($i % 5 -eq 0) {
write-host "any key for more results, q to quit.."
if ([Console]::ReadKey($true).Key -eq "q") { break }
write-host
}
}
if (([regex]'>Next</span>').match($raw).success) {
write-host "any key for more results, q to quit..`n"
if ([Console]::ReadKey($true).Key -eq "q") { break }
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -2,7 +2,7 @@ A "google" command for powershell, e.g.:

![Screenshot](screenshot.png)

specify `-o` to return objects, the parameter is how many pages to retrieve:
specify `-o` to return objects, `-n` results to retrieve (default is 4)

![Screenshot](screenshot2.png)

Expand Down
Binary file modified screenshot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshot2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 090686e

Please sign in to comment.