smodusermc/offline-games / _tools /restore_upstream_audio.py
smodusermc's picture
download
raw
2.36 kB
"""Restore exact-name audio assets from upstream when the catalog mirror returns 404.
Provenance is explicit; equivalence with the missing mirror bytes is not asserted.
"""
import os,json,hashlib,requests,tempfile
from pathlib import Path
from huggingface_hub import HfApi
R=Path('/home/user/game-archive/reports');B='smodusermc/offline-games';api=HfApi(token=os.environ['HF_TOKEN']);report=[]
plans=[(6,'games/30dolar/assets/'+n+'.wav','https://thirtydollar.website/assets/'+n+'.wav') for n in ['you_deadass_built_like_a','white_people_be_like']]
plans +=[(101,'games/cookie-clicker/snd/'+n+'.mp3','https://orteil.dashnet.org/cookieclicker/snd/'+n+'.mp3') for n in ['tickOff','clickOn2','clickOff2','squeak1','squeak2','squeak3','squeak4']]
manifests={i:json.loads((R/f'game-{i}.json').read_text()) for i in [6,101]}
with tempfile.TemporaryDirectory(prefix='upstream-audio-') as tmp:
for i,path,url in plans:
response=requests.get(url,timeout=25);response.raise_for_status();body=response.content
assert len(body)<5*1024**2 and not body.lstrip().lower().startswith((b'<html',b'<!doctype'))
local=Path(tmp)/'audio';local.write_bytes(body);key='site/'+path;sha=hashlib.sha256(body).hexdigest()
api.batch_bucket_files(B,add=[(local,key)]);back=Path(tmp)/'back';api.download_bucket_files(B,[(key,back)],raise_on_missing_files=True)
assert hashlib.sha256(back.read_bytes()).hexdigest()==sha
f={'path':key,'url':url,'bytes':len(body),'sha256':sha,'content_type':'audio/wav' if path.endswith('.wav') else 'audio/mpeg','provenance':{'original_url':'https://garbsoftball.com/'+path,'original_status':404,'replacement':'same filename on upstream project/creator asset host','byte_equivalence_to_missing_original':'not established'}}
records={x['path']:x for x in manifests[i]['files']};records[key]=f;manifests[i]['files']=list(records.values());report.append({'game_id':i,**f});local.unlink();back.unlink()
for i,m in manifests.items():(R/f'game-{i}.json').write_text(json.dumps(m,indent=2))
(R/'upstream-audio-restorations.json').write_text(json.dumps(report,indent=2))
api.batch_bucket_files(B,add=[(R/'upstream-audio-restorations.json','_verification/upstream-audio-restorations.json')]+[(R/f'game-{i}.json',f'_reports/games/{i:04d}.json') for i in manifests])
print('Restored and downloaded-back SHA256 verified',len(report),'upstream audio files')

Xet Storage Details

Size:
2.36 kB
·
Xet hash:
076b977b4149a1c627f42677edab38ee10e3070e5d0540206a7edf7469fdc94d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.