Download from a changing url

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Flipscript
Posts: 15
Joined: 18 Apr 2017, 10:38

Download from a changing url

22 Apr 2019, 12:14

I want to download a file from an url with a changing path. To be exact, i always want to download the current version of the program "XMedia Recode".

The download site (https://www.xmedia-recode.de/download.html) has different download options and i want to download the option "XMedia Recode Portable (64 bit)" which has the download-url http www.xmedia-recode.de /download/XMediaRecode3459_x64.zip. Broken Link for safety If there's an update for the program then the url changes according to its new version number.

I tried to use the following script which didn't work most likely because you can't use wildcards (*) in an url:

Code: Select all

UrlDownloadToFile, "http www.xmedia-recode.de/download/XMediaRecode*_x64.zip", XMediaRecode.zip
So i would be very thankful if you guys could help me and tell me what I have to do to write an ahk-script for downloading a file from an url that changes from time to time.
User avatar
tank
Posts: 3127
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: Download from a changing url

22 Apr 2019, 13:26

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

whr := ComObjCreate("Msxml2.XMLHTTP")
whr.Open("GET", "https://www.xmedia-recode.de/download.html", false)
whr.Send()
links := whr.ResponseText

FoundPos := RegExMatch(links, "<a class=""download_link"" href=""(.+?)""", OutputVar)
msgbox % OutputVar1
???
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
Flipscript
Posts: 15
Joined: 18 Apr 2017, 10:38

Re: Download from a changing url

23 Apr 2019, 13:03

Oh man, tank, you live up to your name, i got steamrolled by your tankiness :salute: . This one was brutal for me to comprehend, in fact i did not fully understand your code. I never worked with regular expressions so i struggled a lot with nearly every line you wrote.

But most importantly i wanna thank you :bravo: . Thanks for your time and effort helping me with my issue, you rock man :thumbup: , can’t believe you wrote a script that fast after i posted my question. Usually I find a solution just by googling some comparable codes but this time I’ve run out of ideas.

Since I’m totally new to regex, may I ask some beginner questions i still could’t answer for myself after skimming through several regex-tutorials and the ahk quick reference.

I couldn’t find information about the created com object „Msxml2.XMLHTTP“ and i don’t understand what „false“ means at the end of line 7 of your code, setting this value to true seems to not affect the result. Though i honestly have to say i even don’t know what „Com Object“ is at all. I see it has something to do with external libraries according to the ahk-documentation. So i guess that ComObject-thing maybe is something like a reference to another coding language (respectively „library“) and insofar „external“?

Nevertheless, I gave my best to understand your code. I guess your idea was to read the website source code and search for strings. I think that’s it, the outputvar of your code is the download-url but still not an absolute path. Perfect, that’s what i needed.

However, and a piece of cake compared to your work, i needed a regex match for the portable verison of the app, which is the download url that ends with „XMediaRecode3459_x64.zip“.

So, my code works as follows:

Code: Select all

whr := ComObjCreate("Msxml2.XMLHTTP")
whr.Open("GET", "https www.xmedia-recode.de /download.html",  Broken Link for safety false)
whr.Send()
links := whr.ResponseText

FoundPos := RegExMatch(links, "<a class=""download_link"" href=""(http www.xmedia-recode.de /download/XMediaRecode[0-9]*_x64\.zip)""",  Broken Link for safety OutputVar)

msgbox, %OutputVar1%

UrlDownloadToFile, %OutputVar1%, %Userprofile%\Desktop\XmediaRecode-Update.zip
I have to say i struggled a lot getting a regular expression to work in the regexmatch command in ahk whereas i could achieve working results fast by using some online regex testers. This was probably because I was pretty confused placing all the quotation marks.

Is there a better regular expression than the one i used in the code i posted above? For instance, the following regex does not work and i don‘t know why:

Code: Select all

FoundPos := RegExMatch(links, "<a class=""download_link"" href=""((.*?)([0-9]*)(_x64\.zip))", OutputVar)
Thanks again for the help and maybe you can answer some of my questions :angel:
User avatar
tank
Posts: 3127
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: Download from a changing url

23 Apr 2019, 14:27

Component
Object
Model
Basically a Microsoft enabled technology for which applications can communicate with each other

true vs false == asynchronous vs synchronous. synchronous operates each step in order waiting for the previous step to complete. This web site loads very quickly which is why you get away with true.
Flipscript wrote:
23 Apr 2019, 13:03
Msxml2.XMLHTTP
See one of the examples from https://autohotkey.com/docs/commands/URLDownloadToFile.htm
this is an object defined in the MSDN (microsofts documentation) https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms759148(v%3Dvs.85)
I will fix the regex when i get home my battery is dead
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
User avatar
tank
Posts: 3127
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: Download from a changing url

23 Apr 2019, 16:01

FoundPos := RegExMatch(links, "(http:\/\/www\.xmedia-recode\.de\/download\/XMediaRecode\d+?\_x64\.zip)", OutputVar)
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
Flipscript
Posts: 15
Joined: 18 Apr 2017, 10:38

Re: Download from a changing url

26 Apr 2019, 11:22

Perfect tank, the regular expression you posted is smarter than my working one, so i will use your solution.

Although i also liked your first idea using the regex „<a class="download_link" href="“ as a fix matching reference point to ensure searching in the correct line that is designated for the downloads.

This issue and your solution, man, you kind of opened Pandora’s Box :?, that was a masterclass for me :geek:. All my previous codes are a piece of cake compared to the enormous potential i can see using the regex coding language. What a powerful tool. And what a pity it seems you have to work into very hard to use it. Well, at least this issue was a beginning for me.

With your help I can finish my script to an extent that when you start the script the app gets updated automatically (= download the update.zip to %temp%, extract it there, then replace the old app-files with the update-files). I will add a progress bar as well by using the examples in the ahk manual.

Thank you so much tank! :superhappy:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], mikeyww and 243 guests