extract_html_full / supabase_utils.py
Mazenbs's picture
Update supabase_utils.py
6b84275 verified
from supabase import create_client, Client
import os
import re
import json
# ---------------------------------------------
# تحميل مفاتيح Supabase من Secrets
# ---------------------------------------------
SUPABASE_URL = os.environ.get("SUPABASE_URL")
SUPABASE_KEY = os.environ.get("SUPABASE_SERVICE_ROLE_KEY")
if not SUPABASE_URL or not SUPABASE_KEY:
raise ValueError("❌ لم يتم العثور على مفاتيح Supabase في Secrets")
# إنشاء العميل
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
# ---------------------------------------------
# حفظ / استبدال القانون في قاعدة البيانات
# ---------------------------------------------
def save_law_to_supabase(law_json: dict):
"""
law_json: dict يحتوي على المفاتيح
title, preamble, number, year, sections
"""
title = law_json.get("title", "عنوان غير معروف")
preamble = law_json.get("preamble", "")
law_number = law_json.get("number")
law_year = law_json.get("year")
sections = law_json.get("sections", [])
# ---------------------------------------------------------
# 1) التحقق من وجود القانون سابقًا
# ---------------------------------------------------------
existing = (
supabase.table("laws")
.select("id")
.eq("title", title)
.execute()
)
if existing.data:
law_id = existing.data[0]["id"]
print(f"⚠ القانون موجود مسبقاً، يتم حذفه: {title}")
# حذف المواد التابعة للأقسام
secs = supabase.table("sections").select("id").eq("law_id", law_id).execute()
for s in secs.data:
supabase.table("articles").delete().eq("chapter_id", s["id"]).execute()
# حذف الأقسام
supabase.table("sections").delete().eq("law_id", law_id).execute()
# حذف القانون نفسه
supabase.table("laws").delete().eq("id", law_id).execute()
# ---------------------------------------------------------
# 2) إضافة القانون الجديد
# ---------------------------------------------------------
res_law = (
supabase.table("laws")
.insert({
"title": title or "",
"number_law": law_number,
"year_law": law_year,
"preamble": preamble or "",
"law_type": "def",
})
.execute()
)
law_id = res_law.data[0]["id"]
print(f"✔ تم إدراج القانون الجديد: {title}")
# ---------------------------------------------------------
# 3) إضافة الأقسام والمواد
# ---------------------------------------------------------
for sec in sections:
sec_title = sec.get("title", "")
sec_content = sec.get("content", "")
sec_tables = sec.get("tables", [])
# استخراج رقم القسم من العنوان
chapter_number = 0
match = re.search(r"\d+", sec_title or "")
if match:
chapter_number = int(match.group(0))
res_sec = (
supabase.table("sections")
.insert({
"law_id": law_id,
"title": sec_title or "",
"content": sec_content or "",
"chapter_number": chapter_number,
"tables": sec_tables or [],
})
.execute()
)
section_id = res_sec.data[0]["id"]
# ----------------------- إضافة المواد -----------------------
for art in sec.get("articles", []):
article_number = int(art["number"]) if art.get("number") else None
article_content = (art.get("text") or "").strip()
article_tag = (art.get("tag") or "").strip()
article_tables = art.get("tables", [])
# ❌ تجاهل المادة إذا كانت فارغة تمامًا
if not article_content and not article_tag:
continue
supabase.table("articles").insert({
"chapter_id": section_id,
"article_number": article_number,
"content": article_content,
"tag": article_tag,
"lowid": law_id,
"tables": article_tables or [],
}).execute()
print(f"✔ تم حفظ جميع الأقسام والمواد للقانون: {title}")