FeelingDucky()

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
budRich
Posts: 82
Joined: 29 Sep 2013, 16:52
Location: Wermland
Contact:

FeelingDucky()

24 Feb 2015, 14:54

I've been using a function originally written by Uberi to get the resulting URL from a "Feeling Lucky" search on google. (I mainly used it to get a youtube link to the song playing in my mediaplayer and autospam it on IRC). But since a couple of months, it doesn't work correctly, it returns the search query instead of the URL i want...

I failed to fix it, and I thought: "Hey bud, you've stopped searching with google since long, you know ctrl+c, ctrl+v and duckduckgo is cooler. Make a new script!"

This is what i came up with:

Code: Select all

FeelingDucky(text, site="")
{
    wb := ComObjCreate("InternetExplorer.Application")
    wb.Visible := false  ; This is known to work incorrectly on IE7.

    StringReplace, Text, Text, `%, `%25, All
    FormatInteger := A_FormatInteger, FoundPos := 0
    SetFormat, IntegerFast, Hex
    While, FoundPos := RegExMatch(Text,"S)[^\w-\.~%]",Char,FoundPos + 1)
        StringReplace, Text, Text, %Char%, % "%" . SubStr("0" . SubStr(Asc(Char),3),-1), All
    SetFormat, IntegerFast, %FormatInteger%
    site := site ? ("site:" site " "): ""

    wb.Navigate("http://duckduckgo.com/?q=\" . site . Text)

   IELoad(wb,10)
   sleep, 10
   IELoad(wb,10)
   
   r := wb.LocationURL
   wb.Quit()
   return r
}

IELoad(wb,sl=100)	
{
    If !wb	
        return
    
    while not wb.busy
        Sleep %sl%

    while wb.busy
        Sleep %sl%
}
Below is an example script:
(Thanks SKAN for the inspiration)

Code: Select all

lstSites= ; SiteName     | SearchSiteURL                                 
(      ; ----------------+-----------------------------------------------
         IMDb            | imdb.com/title                                
         OpenSubtitles   | opensubtitles.org/en/search/sublanguageid-eng 
         Addic7ed        | addic7ed.com/show                             
         TVSubtitles     | www.tvsubtitles.net/tvshow                    
         AutoHotkey      | autohotkey.com                                
         MSDN            | msdn.microsoft.com/en-us/library              
         Wikipedia       | en.wikipedia.org/wiki                         
         Dictionary      | dictionary.reference.com/browse               
         UrbanDict       | urbandictionary.com/define.php?term=          
)     ;  ----------------^-----------------------------------------------

StringSplit, BTN, lstSites, `n, %A_Space%
oSites := {}
Loop % ( BTN0 ) {
  StringSplit, Par, BTN%A_Index%, |, %A_Space%
  oSites.Insert(Par1, Par2)
  ddlSites .= (!ddlSites ? par1 "|" : "|"par1)
}

Gui, duck:Default
Gui, +AlwaysOnTop +ToolWindow
hGui := WinExist()
Gui, Add, Edit, w400 vcSearch,
Gui, Add, DropDownList,wp xp y+10 vcSites,% ddlSites
Gui, Add, Button, Default y+10 wp h100 xp gSearch, I'm Feeling Ducky!
Gui, Show, AutoSize, I'm Feeling Ducky!

return

Search:
GuiControlGet, selectedSite,,cSites
GuiControlGet, searchText,,cSearch
if (searchText="") && (selectedSite="")
{
  Run, http://duckduckgo.com
  return
}

if (searchText="") && (selectedSite!="")
  Run,% "http://" oSites[selectedSite]
else
  Run,% FeelingDucky(searchText,oSites[selectedSite])

return

duckGuiEscape:
duckGuiClose:
ExitApp

FeelingDucky(text, site="")
{
	wb := ComObjCreate("InternetExplorer.Application")
  wb.Visible := false  ; This is known to work incorrectly on IE7.

  StringReplace, Text, Text, `%, `%25, All
  FormatInteger := A_FormatInteger, FoundPos := 0
  SetFormat, IntegerFast, Hex
  While, FoundPos := RegExMatch(Text,"S)[^\w-\.~%]",Char,FoundPos + 1)
      StringReplace, Text, Text, %Char%, % "%" . SubStr("0" . SubStr(Asc(Char),3),-1), All
  SetFormat, IntegerFast, %FormatInteger%
  site := site ? ("site:" site " "): ""

	wb.Navigate("http://duckduckgo.com/?q=\" . site . Text)

  IELoad(wb,10)
  sleep, 10
  IELoad(wb,10)
   
  r := wb.LocationURL
  wb.Quit()
  return r
}

IELoad(wb,sl=100)	
{
    If !wb	
        return
    
    while not wb.busy
        Sleep %sl%

    while wb.busy
        Sleep %sl%
}
However, the function is very slow right now, it's the IELoad that takes several seconds. It would be really cool if someone knew a faster way.
Guest

Re: FeelingDucky()

24 Feb 2015, 15:09

Perhaps use httprequest? On http://ahkscript.org/docs/commands/URLD ... ToFile.htm the second example it shows how to download an URL into a variable - I have this function below - not sure where it comes from - if you search for UrlDownloadToVar you will find several versions people have made.

Code: Select all

UrlDownloadToVar(URL)
	{
	 WebRequest:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
	 try WebRequest.Open("GET",URL)
	 catch error
		Return error.Message
	 WebRequest.Send()
	 Return WebRequest.ResponseText
	}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Google [Bot] and 159 guests