[Function] GetLatestGithubRelease

Post your working scripts, libraries and tools for AHK v1.1 and older
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

[Function] GetLatestGithubRelease

16 Dec 2017, 02:19

Either Gets the latest URL from a github repo or downloads it.

Download might fail if on a slow connection,so take note, I HAVE.

Code: Select all

MsgBox, % GetLatestGithubRelease("aria2", "aria2", "zip", "64bit", "win")		;only gets latest url
MsgBox, % GetLatestGithubRelease("aria2", "aria2", "zip", "64bit", "win", 1)		;gets lates url & downloads it

GetLatestGithubRelease(user, repository, releaseExt:="zip", releaseArch:="64",releaseOS:="win", downloadFile:=0){
dlink:=""
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "https://github.com/" . user . "/" . repository . "/releases/latest", true)
whr.SetTimeouts(0, 60000, 30000, 90000)	; last parameter is ReceiveTimeout
whr.Send()
; Using 'true' above and the call below allows the script to remain responsive.
whr.WaitForResponse(90)	; only in async mode
page := whr.ResponseText
;MsgBox, % page

Loop, Parse, page, `n
	IfInString, A_LoopField, %releaseOS%
		IfInString, A_LoopField, .%releaseExt%
			IfInString, A_LoopField, %releaseArch%
				dlink .= A_LoopField "`n"
Loop, Parse, dlink, `"
	If A_Index = 2
		dLinkActual := "https://github.com" . A_LoopField

If downloadFile		;download the file & return success or failure
	{
	SplitPath, dLinkActual, fileName
	IfNotExist, %A_ScriptDir%\%fileName%
		UrlDownloadToFile, %dLinkActual%, %fileName%
	If !ErrorLevel
		Return, True	;download succeded
	}
Else	;return url
	Return, dLinkActual
}
Last edited by brutus_skywalker on 18 Dec 2017, 06:30, edited 3 times in total.
Outsourcing Clicks & Presses Since 2004.
Guest

Re: [Function] GetLatestGithubRelease

17 Dec 2017, 03:58

Are you sure, a simple URLDownloadToFile, % GetLatestGithubRelease("aria2", "aria2", "zip", "64bit", "win"), Download.zip works for me.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: [Function] GetLatestGithubRelease

17 Dec 2017, 05:59

Guest wrote:Are you sure, a simple URLDownloadToFile, % GetLatestGithubRelease("aria2", "aria2", "zip", "64bit", "win"), Download.zip works for me.
It only downloads partial files for me,github times out or something.
Outsourcing Clicks & Presses Since 2004.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Function] GetLatestGithubRelease

17 Dec 2017, 12:31

Many downloads time out lately on many hosts, including GitHub. I'd kill the imbecil that invented this timeout. I'm on a 3G modem with a fixed quota and when reached I'm down to ~15kB download speed, I can't even download the AHK sources.
Additionally, GitHub does some dodgy redirection that my browser can't follow, so if I try to just right-click -> save the link it'll download a redirect page; I have to open link in new tab and let the bastards do their routine, in order to get the real file.
So that may be your problem: the redirection. Try raising the WinHttpRequest receive timeout (here it's set to 90,000ms):

Code: Select all

whr.Open("GET", "https://github.com/" . user . "/" . repository . "/releases/latest", true)	; true=async, false=sync
whr.SetTimeouts(0, 60000, 30000, 90000)	; last parameter is ReceiveTimeout
whr.Send()
whr.WaitForResponse(90)	; only in async mode
Part of my AHK work can be found here.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: [Function] GetLatestGithubRelease

18 Dec 2017, 04:12

Drugwash wrote:Many downloads time out lately on many hosts, including GitHub. I'd kill the imbecil that invented this timeout. I'm on a 3G modem with a fixed quota and when reached I'm down to ~15kB download speed, I can't even download the AHK sources.
Additionally, GitHub does some dodgy redirection that my browser can't follow, so if I try to just right-click -> save the link it'll download a redirect page; I have to open link in new tab and let the bastards do their routine, in order to get the real file.
So that may be your problem: the redirection. Try raising the WinHttpRequest receive timeout (here it's set to 90,000ms):

Code: Select all

whr.Open("GET", "https://github.com/" . user . "/" . repository . "/releases/latest", true)	; true=async, false=sync
whr.SetTimeouts(0, 60000, 30000, 90000)	; last parameter is ReceiveTimeout
whr.Send()
whr.WaitForResponse(90)	; only in async mode

The timeout does make it a sure thing that the function retrieves the download links, but downloading is still a no go for some reason. Though one can simply just dump updated urls into a url file and use IDM or Free Download Manager, both seem to work, just no native solution. Thx DrugWash, increasing timeout removes any unreliability in getting the download link at least.
Outsourcing Clicks & Presses Since 2004.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Function] GetLatestGithubRelease

18 Dec 2017, 04:54

Guest's feedback is accurate, I just tested the same command and retrieved the 32bit file unharmed. There may be problems on your system, such as a firewall, antivirus or anything that could block the redirection according to the rules set. You may wanna double-check IE's settings first. If you used the same links too many times subsequently it's also possible something on your system or your ISP could've flagged the link or AHK as suspect and it's blocking the access; far streched but possible.

In regard to the timeout setting, I just happened to need some information for a script I've been working on and while searching I found certain versions of Windows (if not all) don't take into account the value set in WaitForResponse() unless the timeout default is changed beforehand through SetTimeouts().
I forgot what the other values stand for but the author said those were the default values so there should be no other change apart from ReceiveTimeout.
And… you're welcome. :)
Part of my AHK work can be found here.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: [Function] GetLatestGithubRelease

18 Dec 2017, 06:09

Guest's feedback is indeed accurate, but the problem was only that i was on a slow connection at the time(Tethered 2G), something to note for others with similar errors, function can now return URL or Download file. Thx all.
Outsourcing Clicks & Presses Since 2004.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: [Function] GetLatestGithubRelease

18 Dec 2017, 06:20

Yeah, all of us on slow connections are F-ed. And they wonder why people get angry…
Glad everything's alright with the script now.
Part of my AHK work can be found here.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 119 guests