Page 1 of 1
API to get newest headless version string
Posted: Sat Apr 20, 2019 7:38 am
by xng
I know you can download the latest headless version through
https://factorio.com/get-download/lates ... ss/linux64 , but is there any API/URI to fetch just the latest version string?
It feels a little bit wasteful to always have to download the whole package to just check if there's a new version.
Re: API to get newest headless version string
Posted: Sun Apr 21, 2019 9:06 pm
by PunPun
Code: Select all
wget -S --spider https://factorio.com/get-download/latest/headless/linux64 2>&1| grep 'filename'
This currently gives me
Code: Select all
"Content-Disposition: attachment; filename=factorio_headless_x64_0.17.32.tar.xz"
Incase someone needs to know how this works.
This part will only fetch the header of the request without actually downloading the file.
Code: Select all
wget -S --spider https://factorio.com/get-download/latest/headless/linux64
And the latter part will pipe it through grep and get a single line containing the filename of the file that would be sent if you made a full http request.
If you want to pipe the result into a file then add
Then ofcourse you need some code to compare the result to what you got last time.
Also note that if the request fails for some reason this will result in an empty line being output so you need to check for that too.
Edit:
You can also use
Code: Select all
wget -S --spider https://factorio.com/get-download/latest/headless/linux64 2>&1| grep 'Last-Modified'
to get the time the file was last modified. Incase you use this approach on an url that always returns an identically named file.
Re: API to get newest headless version string
Posted: Fri Apr 26, 2019 7:29 pm
by TruePikachu
IIRC grep can be made to return a nonzero status code if the pattern doesn't match, and wget/curl will return nonzero if it fails.
Re: API to get newest headless version string
Posted: Sun Apr 28, 2019 1:51 am
by PunPun
TruePikachu wrote: ↑Fri Apr 26, 2019 7:29 pm
IIRC grep can be made to return a nonzero status code if the pattern doesn't match, and wget/curl will return nonzero if it fails.
wget(and cURL) will return a fail message yes but that message contains neither "filename" nor "Last-Modified" so grep will filter everything out and return an empty line. It can indeed be made to output a failure to locate any statement when that happens but this is unneccessary as that only replaces the check if the line was empty to check if we recieved the failure message(which is actually more code) and would not make a difference to the functionality of the script.
Personally I would just add a comment in the script stating "if wget fails to retrieve the header we get an empty line so we just check if the length of the output is longer than 0 before we try to parse the version string." and return a failure code of my own from there if it failed. No need to overcomplicate the script if you know what it is doing and why.
Re: API to get newest headless version string
Posted: Fri May 17, 2019 7:41 am
by xng
PunPun wrote: ↑Sun Apr 21, 2019 9:06 pm
I don't know why I didn't get a notification for this response, but thanks a lot @PunPun for your help. It's a perfect solution for me!
Re: API to get newest headless version string
Posted: Sat May 18, 2019 9:54 am
by xng
Here's the resulting script to compare online latest version with the current version:
Code: Select all
#!/bin/bash
online=$( wget -S --spider https://factorio.com/get-download/latest/headless/linux64 2>&1 | grep --color=never -Po '(?<=filename=factorio_headless_x64_).*(?=.tar.xz)' )
installed=$( ./bin/x64/factorio --version | grep --color=never -Po '(?<=Version\: )[0-9.]+' )
if [ "$online" == "$installed" ]
then
echo "Installed version $installed is the same as the most recent online."
else
echo "There's another version $online than your current version $installed."
fi
You can of course replace one of the echo statements with a call to your update script.
//Cheers
Re: API to get newest headless version string
Posted: Tue May 21, 2019 2:28 pm
by Bilka
There is now an official endpoint
https://factorio.com/api/latest-releases, thanks to Sanqui :)
Re: API to get newest headless version string
Posted: Thu May 23, 2019 11:38 am
by xng
That's awesome, thanks for the info!
Re: API to get newest headless version string
Posted: Thu May 23, 2019 11:53 am
by mrvn
TruePikachu wrote: ↑Fri Apr 26, 2019 7:29 pm
IIRC grep can be made to return a nonzero status code if the pattern doesn't match, and wget/curl will return nonzero if it fails.
The error from wget won't be visible unless you use bash and set the pipe fail option or check the individual return codes for all piped commands.
For posix shell you have to redirect wget output into a tempfile or variable, check the return code and then grep separately.
Re: API to get newest headless version string
Posted: Tue May 28, 2019 11:17 am
by xng
Anyway, here's the same script using the new API:
Code: Select all
#!/bin/bash
online=$( curl -s https://factorio.com/api/latest-releases | jq -crM '.experimental.headless' )
installed=$( ./bin/x64/factorio --version | grep --color=never -Po '(?<=Version\: )[0-9.]+' )
if [ "$online" == "$installed" ]
then
echo "Installed version $installed is the same as the most recent online."
else
echo "There's another version $online than your current version $installed."
fi
The main difference is that it responds slightly quicker and that it is the recommended way of retrieving the version number.
The bash app 'jq' to parse the json can be installed with either apt or snap.