API to get newest headless version string

Anything that prevents you from playing the game properly. Do you have issues playing for the game, downloading it or successfully running it on your computer? Let us know here.
Post Reply
xng
Fast Inserter
Fast Inserter
Posts: 165
Joined: Fri Feb 14, 2014 1:04 pm
Contact:

API to get newest headless version string

Post 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.

PunPun
Long Handed Inserter
Long Handed Inserter
Posts: 69
Joined: Sun Mar 27, 2016 7:08 pm
Contact:

Re: API to get newest headless version string

Post 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.

Code: Select all

2>&1| grep 'filename'


If you want to pipe the result into a file then add

Code: Select all

> 'outputfilename'
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.

User avatar
TruePikachu
Filter Inserter
Filter Inserter
Posts: 978
Joined: Sat Apr 09, 2016 8:39 pm
Contact:

Re: API to get newest headless version string

Post 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.

PunPun
Long Handed Inserter
Long Handed Inserter
Posts: 69
Joined: Sun Mar 27, 2016 7:08 pm
Contact:

Re: API to get newest headless version string

Post 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.

xng
Fast Inserter
Fast Inserter
Posts: 165
Joined: Fri Feb 14, 2014 1:04 pm
Contact:

Re: API to get newest headless version string

Post 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!

xng
Fast Inserter
Fast Inserter
Posts: 165
Joined: Fri Feb 14, 2014 1:04 pm
Contact:

Re: API to get newest headless version string

Post 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

Bilka
Factorio Staff
Factorio Staff
Posts: 3128
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: API to get newest headless version string

Post by Bilka »

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.

xng
Fast Inserter
Fast Inserter
Posts: 165
Joined: Fri Feb 14, 2014 1:04 pm
Contact:

Re: API to get newest headless version string

Post by xng »

Bilka wrote:
Tue May 21, 2019 2:28 pm
There is now an official endpoint https://factorio.com/api/latest-releases, thanks to Sanqui :)
That's awesome, thanks for the info!

mrvn
Smart Inserter
Smart Inserter
Posts: 5699
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: API to get newest headless version string

Post 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.

xng
Fast Inserter
Fast Inserter
Posts: 165
Joined: Fri Feb 14, 2014 1:04 pm
Contact:

Re: API to get newest headless version string

Post 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.

Post Reply

Return to “Technical Help”