Downloading from complicated page Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
IvanVanko
Posts: 34
Joined: 14 Jul 2016, 11:40

Downloading from complicated page

01 Nov 2016, 11:27

Hello folks!
Some problem here. Let's say I have a link like this:
http://sberbank-ast.ru/Download.aspx?fi ... 150b4aca4a
(link is clickable, and a real file is there)
Downloading is not possible before I click download button on page or run .js (worse way as there would be different file extensions). Using AHK built-in tools for download would just get that page itself. Is there some way to use AHK for automatic downloading files from such links?
Твой софт - говно!
User avatar
IvanVanko
Posts: 34
Joined: 14 Jul 2016, 11:40

Re: Downloading from complicated page

02 Nov 2016, 05:17

Anybody? :|
Твой софт - говно!
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Downloading from complicated page

02 Nov 2016, 11:31

I'm not sure about using AutoHotkey by itself, but you might be able to scrape the information required from the HTML and pass it to cURL:

Code: Select all

curl "http://sberbank-ast.ru/Download.aspx?fid=94c36df6-ece6-4993-971a-72150b4aca4a" -H "Host: sberbank-ast.ru" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" --compressed -H "Referer: http://sberbank-ast.ru/Download.aspx?fid=94c36df6-ece6-4993-971a-72150b4aca4a" -H "Cookie: _sm_au_c=iVVSL6R80rMj0TS70f; ASP.NET_SessionId=e0gsbnasaw52hofbs30cq2pt" -H "DNT: 1" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" -o File.zip --data "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE="%"2FwEPDwULLTE4NjAzMjc1MzIPZBYCZg9kFgICAw9kFgQCAQ8PFgIeB1Zpc2libGVoZGQCAg9kFgQCAw9kFgICAw9kFgICAg8PFgIfAGdkFgICAg8WAh4EVGV4dAVAICjQoNCw0LfQvNC10YA6ICAxOTkgNTU2INCx0LDQudGCOyDQt9Cw0LPRgNGD0LbQtdC9OiAxMy4wNy4yMDE2KWQCBQ8WAh8AaGRkW7YZoZFgkfcLOwS2bYeLoSfOjiY"%"3D&ctl00"%"24xmlData=&ctl00"%"24phDataZone"%"24btnSaveAsFile="%"D0"%"92"%"D1"%"8B"%"D0"%"B3"%"D1"%"80"%"D1"%"83"%"D0"%"B7"%"D0"%"B8"%"D1"%"82"%"D1"%"8C+"%"D0"%"BA"%"D0"%"B0"%"D0"%"BA+"%"D1"%"84"%"D0"%"B0"%"D0"%"B9"%"D0"%"BB"
http://www.confusedbycode.com/curl/#downloads

If you were able to provide a link to several more files it would make it easier to help build this.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Downloading from complicated page  Topic is solved

02 Nov 2016, 14:45

Code: Select all

#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
SetBatchLines -1

URL := "http://sberbank-ast.ru/Download.aspx?fid=94c36df6-ece6-4993-971a-72150b4aca4a"

Gui, Add, ActiveX, w600 h300 vwb, Shell.Explorer
Gui, Show

ComObjConnect(wb, "wb_")
wb.navigate(URL)
while WB.ReadyState != 4
	Sleep 100

wb.document.getElementById("phDataZone_FileGeneralData1_lbDownload").click()
return

GuiClose:
ExitApp

wb_BeforeNavigate2(pDisp, url, Flags, TargetFrameName, PostData, Headers, Cancel) {
	if PostData {
		Cancel[] := true

		pDisp.document.open()
		pDisp.document.write("Downloading...")

		XMLHTTP_Download(url, "download_test.pdf", PostData, Headers)

		pDisp.document.open()
		pDisp.document.write("Download Finished! Saved to download_test.pdf")
	}
}

XMLHTTP_Download(URL, Filename:="", PostData:="", Headers:="") {
	static req := ComObjCreate("Msxml2.XMLHTTP")
	req.open(PostData ? "POST" : "GET", URL, true)
	
	Loop, Parse, Headers, `n, `r
	{
		if RegExMatch(A_LoopField, "(.+?)\s*:\s*(.+)", m)
			req.SetRequestHeader(m1, m2)
	}

	req.send(PostData)
	while req.readyState != 4
		sleep 100

	if (req.status == 200 || req.status == 304) {
		if (Filename = "") {
			if !RegExMatch( req.getResponseHeader("Content-Disposition"), "filename=\K.+", Filename )
				Filename := A_Now ".XMLHTTP_Download"
		}

		ADO := ComObjCreate("ADODB.Stream")
		ADO.Type := 1 ; adTypeBinary
		ADO.Open()
		ADO.Write(req.responseBody)
		ADO.SaveToFile(FileName, 2)
		ADO.Close()
	} else {
		throw, "Status " req.status
	}
}
Just an example of showing how to cancel a POST request in a WebBrowser, and then invoke the POST request with Msxml2.XMLHTTP.
However the better way to do this task, is to extract the post data from the HTML source, then POST directly.
User avatar
IvanVanko
Posts: 34
Joined: 14 Jul 2016, 11:40

Re: Downloading from complicated page

02 Nov 2016, 15:22

tmplinshi, wow, you are truly an almighty wizard! Just one small question: is it possible to do the same without any gui?
Твой софт - говно!
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Downloading from complicated page

02 Nov 2016, 20:57

This approach require GUI, but you can delete Gui, Show to not show GUI.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, Bing [Bot], Draken, Spawnova and 141 guests