Files
EMERGENCY/skel/pdfs/register.py
T
2026-06-30 18:30:24 +00:00

72 lines
2.6 KiB
Python

#!/usr/bin/env python3
import os, json, uuid, re
from datetime import datetime
PDF_DIR = "/opt/hub/pdfs/files"
META = os.path.join(PDF_DIR, ".metadata.json")
TAG_RULES = [
(r'fm[-_ ]?\d|atp[-_ ]?\d|tm[-_ ]?\d|tc[-_ ]?\d|army|military|combat|tactical|sniper|infantry|guerrilla|special.forces', 'defense'),
(r'medic|first.aid|surgery|trauma|nursing|hesperian|midwif|dentist|disease|emergency.med|tccc|where.there.is.no.doctor', 'medical'),
(r'survival|prepper|shtf|bug.out|wilderness|evasion', 'survival'),
(r'radio|ham|antenna|arrl|fcc|comms|aprs|signal', 'comms'),
(r'food|cook|cann|preserv|recipe|kitchen|baking|ferment', 'food'),
(r'farm|garden|crop|seed|livestock|chicken|poultry|cattle|sheep|goat|beekeep|honey|agriculture|permaculture', 'agriculture'),
(r'vet|animal|dog|cat|horse|pig|pet', 'veterinary'),
(r'navig|map|compass|gps|bowditch|topo', 'navigation'),
(r'solar|wind|battery|grid|power|nrel|energy|generator', 'energy'),
(r'electric|plumb|carpent|hvac|welding|machinist|trade|repair|audel', 'trades'),
(r'chem|biolog|physic|crc.handbook|merck.index', 'chemistry'),
(r'engineer|mechanic|machine|handbook', 'engineering'),
(r'law|legal|constitution|statute|code|court|right', 'law'),
(r'finance|tax|irs|money|invest|insurance', 'finance'),
(r'history|war|civil|world.war|geopolit', 'history'),
]
def infer_tag(filename):
name = filename.lower()
for pattern, tag in TAG_RULES:
if re.search(pattern, name):
return tag
return 'general'
def clean_title(filename):
name = filename.rsplit('.', 1)[0]
name = re.sub(r'[_-]+', ' ', name).strip()
return name[:200]
def main():
if os.path.exists(META):
with open(META) as f:
meta = json.load(f)
else:
meta = {}
registered = {info['filename'] for info in meta.values()}
added = 0
skipped = 0
for fname in sorted(os.listdir(PDF_DIR)):
if not fname.lower().endswith('.pdf'): continue
if fname.startswith('.'): continue
if fname in registered:
skipped += 1
continue
path = os.path.join(PDF_DIR, fname)
if os.path.getsize(path) < 50000: continue
fid = uuid.uuid4().hex[:12]
meta[fid] = {
'filename': fname,
'title': clean_title(fname),
'tag': infer_tag(fname),
'uploaded': datetime.utcnow().isoformat(),
}
added += 1
with open(META, 'w') as f:
json.dump(meta, f, indent=2)
print(f"Added: {added} Already registered: {skipped} Total in library: {len(meta)}")
if __name__ == '__main__':
main()