Download scripts/blueprints/common.py from beyarkay/factorio-blueprints: direct link, hf CLI and curl.
- Browser
- Download file 3.44 kB
-
https://huggingface.co/datasets/beyarkay/factorio-blueprints/resolve/main/scripts/blueprints/common.py
- Command line
-
hf download hf://datasets/beyarkay/factorio-blueprints/scripts/blueprints/common.py
-
curl -L -o common.py https://huggingface.co/datasets/beyarkay/factorio-blueprints/resolve/main/scripts/blueprints/common.py
3.44 kB
| import gzip, json, os, re, threading, time, random, urllib.request, urllib.error, ssl | |
| RAW = os.environ.get("RAW", "raw") | |
| UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36" | |
| BP_RE = re.compile(r"0eN[A-Za-z0-9+/=]{20,}") | |
| _ctx = ssl.create_default_context(cafile=os.environ.get("SSL_CERT_FILE") or "/root/.ccr/ca-bundle.crt") if os.path.exists("/root/.ccr/ca-bundle.crt") else None | |
| def get(url, tries=5, timeout=60, headers=None, data=None, ok404=True): | |
| h = {"User-Agent": UA, "Accept": "*/*"} | |
| if headers: h.update(headers) | |
| for i in range(tries): | |
| try: | |
| req = urllib.request.Request(url, headers=h, data=data) | |
| with urllib.request.urlopen(req, timeout=timeout, context=_ctx) as r: | |
| return r.read() | |
| except urllib.error.HTTPError as e: | |
| if e.code in (404, 410) and ok404: return None | |
| if e.code in (401, 403) and i >= 1: return None | |
| wait = 2 ** i + random.random() | |
| if e.code == 429: | |
| ra = e.headers.get("Retry-After") | |
| wait = max(wait, float(ra) if ra and ra.isdigit() else 10 * (i + 1)) | |
| time.sleep(wait) | |
| except Exception: | |
| time.sleep(2 ** i + random.random()) | |
| return None | |
| def get_json(url, **kw): | |
| b = get(url, **kw) | |
| if b is None: return None | |
| try: return json.loads(b) | |
| except Exception: return None | |
| def read_jsonl_gz(path): | |
| recs, clean = [], True | |
| try: | |
| with gzip.open(path, "rt") as f: | |
| for line in f: | |
| try: recs.append(json.loads(line)) | |
| except Exception: clean = False | |
| except (EOFError, OSError, EOFError): | |
| clean = False | |
| return recs, clean | |
| class Writer: | |
| """Append-mode gzip jsonl; tracks ids already written so reruns resume.""" | |
| def __init__(self, source): | |
| self.source = source | |
| self.path = os.path.join(RAW, f"{source}.jsonl.gz") | |
| self.seen = set() | |
| if os.path.exists(self.path): | |
| recs, clean = read_jsonl_gz(self.path) | |
| self.seen = {r["id"] for r in recs} | |
| if not clean: # killed mid-write: rewrite so appended members stay readable | |
| with gzip.open(self.path, "wt") as f: | |
| for r in recs: f.write(json.dumps(r, ensure_ascii=False) + "\n") | |
| self.f = gzip.open(self.path, "at") | |
| self.lock = threading.Lock() | |
| self.n = 0 | |
| def write(self, rec): | |
| if not rec.get("string"): return False | |
| rec["source"] = self.source | |
| with self.lock: | |
| if rec["id"] in self.seen: return False | |
| self.seen.add(rec["id"]) | |
| self.f.write(json.dumps(rec, ensure_ascii=False) + "\n") | |
| self.n += 1 | |
| if self.n % 200 == 0: self.f.flush() | |
| return True | |
| def close(self): | |
| self.f.close() | |
| import base64, zlib, html as _html | |
| ANY_BP_RE = re.compile(r"0e[NJFA][A-Za-z0-9+/=]{40,}") | |
| def valid_bp(s): | |
| try: | |
| raw = zlib.decompress(base64.b64decode(s[1:], validate=True)) | |
| return raw[:1] == b"{" | |
| except Exception: | |
| return False | |
| def extract_bps(text): | |
| """All fully-decodable exchange strings in text (HTML-unescaped; escaped-markdown backslashes and whitespace inside long runs are not repaired).""" | |
| text = _html.unescape(text) | |
| return list(dict.fromkeys(m for m in ANY_BP_RE.findall(text) if valid_bp(m))) | |