1#!/usr/bin/env nix-shell
2#!nix-shell -I nixpkgs=./. -i python3 -p python3.pkgs.requests python3.pkgs.lxml nix
3
4from lxml import html
5import json
6import os.path
7import re
8import requests
9import subprocess
10
11def nix_prefetch_sha256(name):
12 return subprocess.run(['nix-prefetch-url', '--type', 'sha256', 'https://optifine.net/download?f=' + name], capture_output=True, text=True).stdout.strip()
13
14# fetch download page
15sess = requests.session()
16page = sess.get('https://optifine.net/downloads')
17tree = html.fromstring(page.content)
18
19# parse and extract main jar file names
20href = tree.xpath('//tr[@class="downloadLine downloadLineMain"]/td[@class="colMirror"]/a/@href')
21expr = re.compile('(OptiFine_)([0-9.]*)(.*)\.jar')
22result = [ expr.search(x) for x in href ]
23
24# format name, version and hash for each file
25catalogue = {}
26for i, r in enumerate(result):
27 index = r.group(1).lower() + r.group(2).replace('.', '_')
28 version = r.group(2) + r.group(3)
29 catalogue[index] = {
30 "version": version,
31 "sha256": nix_prefetch_sha256(r.group(0))
32 }
33
34# latest version should be the first entry
35if len(catalogue) > 0:
36 catalogue['optifine-latest'] = list(catalogue.values())[0]
37
38# read previous versions
39d = os.path.dirname(os.path.abspath(__file__))
40with open(os.path.join(d, 'versions.json'), 'r') as f:
41 prev = json.load(f)
42
43# `maintainers/scripts/update.py` will extract stdout to write commit message
44# embed the commit message in json and print it
45changes = [ { 'commitMessage': 'optifinePackages: update versions\n\n' } ]
46
47# build a longest common subsequence, natural sorted by keys
48for key, value in sorted({**prev, **catalogue}.items(), key=lambda item: [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', item[0])]):
49 if key not in prev:
50 changes[0]['commitMessage'] += 'optifinePackages.{}: init at {}\n'.format(key, value['version'])
51 elif value['version'] != prev[key]['version']:
52 changes[0]['commitMessage'] += 'optifinePackages.{}: {} -> {}\n'.format(key, prev[key]['version'], value['version'])
53
54# print the changes in stdout
55print(json.dumps(changes))
56
57# write catalogue to file
58with open(os.path.join(d, 'versions.json'), 'w') as f:
59 json.dump(catalogue, f, indent=4)
60 f.write('\n')