API to get newest headless version string
API to get newest headless version string
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.
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
Code: Select all
wget -S --spider https://factorio.com/get-download/latest/headless/linux64 2>&1| grep 'filename'
Code: Select all
"Content-Disposition: attachment; filename=factorio_headless_x64_0.17.32.tar.xz"
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
Code: Select all
2>&1| grep 'filename'
If you want to pipe the result into a file then add
Code: Select all
> 'outputfilename'
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'
- TruePikachu
- Filter Inserter
- Posts: 978
- Joined: Sat Apr 09, 2016 8:39 pm
- Contact:
Re: API to get newest headless version string
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
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.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.
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
Here's the resulting script to compare online latest version with the current version:
You can of course replace one of the echo statements with a call to your update script.
//Cheers
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
//Cheers
Re: API to get newest headless version string
There is now an official endpoint https://factorio.com/api/latest-releases, thanks to Sanqui :)
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Re: API to get newest headless version string
That's awesome, thanks for the info!Bilka wrote: ↑Tue May 21, 2019 2:28 pm There is now an official endpoint https://factorio.com/api/latest-releases, thanks to Sanqui
Re: API to get newest headless version string
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.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.
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
Anyway, here's the same script using the new API:
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.
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 bash app 'jq' to parse the json can be installed with either apt or snap.