/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Search with MSNSearch Web API from MSH (Part 1) "," about_Switch Documentation "," Switch -regEx "," Dir, Dir and more Dir in MSH "," Simple Show-Image function for Monad "," MSH out-zip Function "," get a Users LastLogontime and Domaincontroller fro... "," Some fun With Monads Add-Member, MP3 files and COM "," MSH snap-in to Translate IADsLargeInteger to Int64 "," Commandline editing with MSH. "," ")

Thursday, February 02, 2006

 


Search with MSNSearch Web API from MSH (Part 2)



In this second posting about the MSN Search Web Service from MSH,
in last post Search with MSNSearch Web API from MSH (Part 1) ,

We got an Application ID,
Generated the DLL to reference in MSH,
and generated A Web search Object from MSH, and did your first Search with it.

(If you do not have the DLL or AppID you cannot run the Script, see Part 1 on how to make them)
We will build upon this by making a function Using the MSN Search Objects,

as said in last post there where 2 thing I would like it to do,
let me go to the Next results and Making the Links Usefull,

To do this I had to ask the user for input, I decided to use the $host.ui.PromptForChoice() Method for this.

this is the Same as used by the CMDlets, so it is constistent and also provides a Help system.

I build it up to take N for Next, Q to Quit and the numbers 0-9 to select the links from the results.

The Choices now look like this :

Choice
Press Number to Start Page, Q to Quit
[00  [11  [22  [33  [44  [55  [66  [77  [88  [99  [N] Next  [Q] Quit  [?] Help (default is "N"): ?
0 -
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9 -
N - Next 10
Q - Quit
[00  [11  [22  [33  [44  [55  [66  [77  [88  [99  [N] Next  [Q] Quit  [?] Help (default is "N"):


after that I check the choices made,

for the Next I needed to get the Next results,
this is very easy by setting the Offset of the SourceRequest, and doing the search again.

then I Needed a way to open a link from the results, I just use the Number returned from the $choice if it was not Next or Quit, to get the URL and use the Shell.Application COM Object to start it.

Also I added a bit of Color to make it easier to read.

with this done, we can do a Paged MSN search from MSH,
just by typing search-MSH and the test to search, you get 10 pages at a time, and you can start them by typeing there number.

the function is still a bit RAW (e.g. It will open explorer in you current folder as you choose a non existing link, and you can't browse back for example) but I think its allready very handy for a Quick Search from MSH.

Enjoy,

gr /\/\o\/\/
Tags :

# Search-MSH 
# MSN Web search Function, using Web API  
# /\/\o\/\/ 2006 
# http://mow001.blogspot.com 

[System.Reflection.Assembly]::LoadFile("g:\mowsh\MSNSearchService.dll")
set-Alias new = new-object
Function Search-MSN {

  # Build Choices Menu

  $Next = ([System.Management.Automation.Host.ChoiceDescription]"&Next")
  $Next.helpmessage = "Next 10"

  $Quit = ([System.Management.Automation.Host.ChoiceDescription]"&Quit")
  $Quit.helpmessage = "Quit"

  $Start = [System.Management.Automation.Host.ChoiceDescription[]]("&0","&1","&2","&3","&4","&5","&6","&7","&8","&9")

  $Cl = $start  
  $cl += ($Next,$Quit)

  Function Prompt-Continue ($Caption = "Choice", $Message = "Press Number to Start Page, N or Enter for next page, Q to Quit",$choices = $cl){
    $Choice = $host.ui.PromptForChoice($caption,$Message,[System.Management.Automation.Host.ChoiceDescription[]]$choices,10)
    return $Choice
  } 

  # build-Up MSN Search Service Object 

  $MSN = new MSNSearchService

  $s = new SourceRequest
  $s.source = [sourcetype]::web
  $s.ResultFields = ([ResultFieldMask]::All)

  $sr = new SearchRequest
  $sr.AppID = "YOUR AppID HERE !!!!!!!"
  $sr.Requests = $S
  $sr.Flags = [SearchFlags]::None
  $sr.CultureInfo = "en-US"

  # Do Inital Search
 
  $sr.query = "$args"
  $r = new sourceResponse
  $r = $MSN.Search($sr)
  $i = 0 ; $r.responses[0].results | foreach { 
    write-host -fore yellow "[$i] $($_.Title)"
    write-host $_.Description
    write-host -fore White $_.Url
    write-host 
    $i += 1
  }

  # Process Choices till Quit is chosen

  $Continue  = $true
  while ($Continue) {
    $Choice = Prompt-Continue
    switch -regEx($choice) {
      10 {
           $S.Offset += 10
           $R = $MSN.Search($sr)
           $i = 0 ; $r.responses[0].results | foreach { 
             write-host -fore yellow "[$i] $($_.Title)"
             write-host $_.Description
             write-host -fore White $_.Url
             write-host 
             $i += 1
           }
           Break
         }
      11 {$Continue = $false;break}
      [0-9] {(new-object -com shell.application).ShellExecute($r.responses[0].results[($_)].url)}
    }
  }
}



Comments: Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?