Store data in JSON file

dev
Laura Nguyen 1 year ago
parent 8c305397b9
commit 1cbb02b3fa
  1. 51
      data.json
  2. 29
      main.py

@ -0,0 +1,51 @@
{
"Arch": {
"out_of_date_since": null,
"out_of_date_interval": 86400,
"csc": "archlinux/",
"upstream": "http://arch.mirror.constant.com/",
"file": "lastupdate"
},
"Ceph": {
"out_of_date_since": null,
"out_of_date_interval": 86400,
"csc": "ceph/",
"upstream": "https://download.ceph.com/",
"file": "timestamp"
},
"Debian": {
"out_of_date_since": null,
"out_of_date_interval": 86400,
"csc": "",
"upstream": "https://ftp-master.debian.org/",
"file": "debian/project/trace/master"
},
"Eclipse": {
"out_of_date_since": null,
"out_of_date_interval": 172800,
"csc": "eclipse/",
"upstream": "http://download.eclipse.org/",
"file": "TIME"
},
"GNU": {
"out_of_date_since": null,
"out_of_date_interval": 86400,
"csc": "",
"upstream": "https://mirrors.kernel.org/",
"file": "gnu/mirror-updated-timestamp.txt"
},
"Kernel": {
"out_of_date_since": null,
"out_of_date_interval": 86400,
"csc": "kernel.org/",
"upstream": "https://mirrors.edge.kernel.org/pub/",
"file": "linux/kernel/next/sha256sums.asc"
},
"OpenBSD": {
"out_of_date_since": null,
"out_of_date_interval": 86400,
"csc": "",
"upstream": "https://ftp.openbsd.org/pub/",
"file": "OpenBSD/timestamp"
}
}

@ -4,6 +4,7 @@
This mirror status checker determines whether CSC mirror is up-to-date with upstream
"""
import time
import requests
from arch import Arch
from ceph import Ceph
@ -51,11 +52,33 @@ if __name__ == "__main__":
distro.print_output(distro.check())
except requests.exceptions.RequestException as err:
print(f"Error: {distro.name()}\n{err}")"""
distros = json.load(open('distros.json',))
"""distros = json.load(open('distros.json',))
print(distros)
for distro in distros:
print(distro[0] + ":")
print("CSC mirror: " + checker(distro[1], distro[3]))
print("Official distro: " + checker(distro[2], distro[3]))
print("Official distro: " + checker(distro[2], distro[3]))"""
with open("data.json", "r", encoding = "utf-8") as file:
data = json.load(file)
current_time = int(time.time())
for distro in data:
csc_url = "http://mirror.csclub.uwaterloo.ca/" + data[distro]["csc"] \
+ data[distro]["file"]
upstream_url = data[distro]["upstream"] + data[distro]["file"]
try:
if requests.get(csc_url).text == requests.get(upstream_url).text:
data[distro]["out_of_date_since"] = None
elif data[distro]["out_of_date_since"] is None:
data[distro]["out_of_date_since"] = current_time
elif current_time - data[distro]["out_of_date_since"] \
> data[distro]["out_of_date_interval"]:
print(f"Failure: {distro} out-of-sync")
continue
print(f"Success: {distro} up-to-date")
except requests.exceptions.RequestException as err:
print(f"Error: {distro}\n{err}")
with open("data.json", "w", encoding = "utf-8") as file:
json.dump(data, file, indent = 4)

Loading…
Cancel
Save