-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_factorio_version.py
More file actions
executable file
·40 lines (31 loc) · 1.18 KB
/
get_factorio_version.py
File metadata and controls
executable file
·40 lines (31 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
import json
import sys
from urllib.parse import urlencode
from urllib.request import urlopen, Request
BASE_URL = 'https://rssthis.itdog.me/xeva'
SRC = 'https://wiki.factorio.com/Main_Page/Latest_versions'
STABLE_XPATH = '//*[@id="mw-content-text"]/div/ul[1]/li[1]/a'
EXPERIMENTAL_XPATH = '//*[@id="mw-content-text"]/div/ul[1]/li[2]/a'
def get_version(experimental=False):
xpath = STABLE_XPATH if not experimental else EXPERIMENTAL_XPATH
req = Request(
'{}?{}'.format(BASE_URL, urlencode({"src": SRC, "xpath": xpath})),
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/51.0.2704.103 Safari/537.36'
}
)
with urlopen(req) as resp:
result = json.loads(resp.read().decode('utf-8'))['result']
version = result[list(result.keys())[0]][0]
return version
if __name__ == '__main__':
exp = len(sys.argv) > 1 and sys.argv[1] == '--experimental'
try:
print(get_version(exp))
exit(0)
except Exception as e:
print('Unexpected result. ' + str(e), file=sys.stderr)
exit(1)