Page 1 of 1

FirstImageOnGoogle() - Google Image Search

Posted: 11 Mar 2018, 13:18
by Gicu
born to find the first one, name remains, now you can choose from 1 to 100 (first shown without scrolling the page).

Code: Select all

; Test
wb			:= ComObjCreate("InternetExplorer.Application")
wb.Visible	:= true

Start := A_TickCount

for i, Name in Animals := ["dog", "cat", "bear", "penguin"]
{
	Index := (Name = "cat") ? 10 : 1

	Fimage := FirstImageOnGoogle(Name, Index, wb, Timeout := 20000
	, SizeL 			:= 0
	, SizeM 			:= 0
	, Icon 				:= 0
	, Larger400x300 	:= 0
	, Larger640×480 	:= 0
	, Larger800×600 	:= 0
	, Larger1024×768 	:= 0
	, Larger1600×1200 	:= 0
	, Larger2272×1704 	:= 0
	, SizeW 			:= 0
	, SizeH 			:= 0

	, Color 			:= 0
	, Gray 				:= 0
	, SpecificColor 	:= 0

	, Face 				:= 0
	, Photo 			:= 0
	, Clipart 			:= 0
	, lineart 			:= 0
	, animated 			:= 1

	, GroupBySubject 	:= 0
	, ShowImageSize 	:= 0)
	
	;UrlDownloadToFile, % Fimage.Url, % File := a_desktop "\" Name "." Fimage.ext
	;Run % File

	if Fimage.Url
		wb.Navigate(Fimage.Url)

	msgbox % "Url: " Fimage.Url "`nExt: " Fimage.ext
}

MsgBox, % A_TickCount - Start
wb.quit()

; 11-03-2018
; https://stenevang.wordpress.com/2013/02/22/google-advanced-power-search-url-request-parameters/
; ===============================================================================================
FirstImageOnGoogle(Search, ImageNum, Wb, Timeout := 20000
, SizeL 			:= 0
, SizeM 			:= 0
, Icon 				:= 0
, Larger400x300 	:= 0
, Larger640×480 	:= 0
, Larger800×600 	:= 0
, Larger1024×768 	:= 0
, Larger1600×1200 	:= 0
, Larger2272×1704 	:= 0
, SizeW 			:= 0
, SizeH 			:= 0

, Color 			:= 0
, Gray 				:= 0
, SpecificColor 	:= 0

, Face 				:= 0
, Photo 			:= 0
, Clipart 			:= 0
, lineart 			:= 0
, animated 			:= 0

, GroupBySubject 	:= 0
, ShowImageSize 	:= 0){
	
	GoogleUrl := "https://www.google.com/search?q=" . UriEncode(Search) "&tbm=isch" . "&tbs="
	. ",isz:" . (SizeL ? "l" : SizeM ? "m" : Icon ? "i" : Larger400x300 ? "lt,islt:qsvga" : Larger640×480 ? "lt,islt:vga" : Larger800×600 ? "lt,islt:svga" : Larger1024×768 ? "lt,islt:xga" : Larger1600×1200 ? "lt,islt:2mp" : Larger2272×1704 ? "lt,islt:4mp" : (SizeW && SizeH) ? "ex,iszw:" SizeW ",iszh" SizeH : "")
	. ",ic:"  . (Color ? "color" : Gray ? "gray" : SpecificColor ? "specific,isc:" SpecificColor : "") ; [orange, yellow, green, teal, blue, purple, pink, white, gray, black, brown]
	. ",itp:" . (Face ? "Face" : Photo ? "Photo" : Clipart ? "Clipart" : lineart ? "lineart" : animated ? "animated" : "") . (GroupBySubject ? ",isg:to" : "") . (ShowImageSize  ? ",imgo:1" : "")
	
	if (ImageNum >= 100)
		Return
		
	if !(ComObjType(Wb, "IID") ? 1 : 0) ; !WbExist(wb) ; https://autohotkey.com/board/topic/76918-check-to-see-if-a-com-object-still-exists/
	{
		wb			:= ComObjCreate("InternetExplorer.Application")
		wb.Visible	:= False
		Close		:= 1
	}

	wb.Navigate(GoogleUrl)
	
	;WbWait(wb)
	start := A_TickCount
	Loop
	{
		time := (A_TickCount - start)
		
		if (timeout && (time = timeout))
			Break

		if wb.busy
			Continue

		if (wb.document.readyState = "complete")
			Break
	}

	RegExMatch(UriDecode(wb.document.getElementById("rg_s").getelementsbytagname("a")[ImageNum - 1].href), "imgurl=\K.*", Url)
	RegExMatch(Url, "i)^.*\.(JPEG|JPG|BMP|GIF|PNG|TIFF)", Url)
	RegExMatch(Url, "\.\K[\w\d]+$", Ext)

	wb.stop()

	if Close
		wb.quit()

	Return {Url: Url, Ext: Ext, GoogleUrl: GoogleUrl}
}

; https://autohotkey.com/board/topic/75390-ahk-l-unicode-uri-encode-url-encode-function/
; ======================================================================================
UriEncode(Uri)
{
    oSC := ComObjCreate("ScriptControl")
    oSC.Language := "JScript"
    Script := "var Encoded = encodeURIComponent(""" . Uri . """)"
    oSC.ExecuteStatement(Script)
    Return, oSC.Eval("Encoded")
}

UriDecode(Uri)
{
    oSC := ComObjCreate("ScriptControl")
    oSC.Language := "JScript"
    Script := "var Decoded = decodeURIComponent(""" . Uri . """)"
    oSC.ExecuteStatement(Script)
    Return, oSC.Eval("Decoded")
}

Re: FirstImageOnGoogle() - Google Image Search

Posted: 10 Jun 2022, 03:36
by Jasbir_Singh
getElementById("rg_s") is not working.

please help to correct it.