Spaces:
Sleeping
Sleeping
Update supabase_utils.py
Browse files- supabase_utils.py +37 -30
supabase_utils.py
CHANGED
|
@@ -2,25 +2,17 @@ from supabase import create_client, Client
|
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
import json
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
-
|
| 7 |
|
| 8 |
# ---------------------------------------------
|
| 9 |
-
# ุช
|
| 10 |
# ---------------------------------------------
|
| 11 |
-
#load_dotenv()
|
| 12 |
SUPABASE_URL = os.environ.get("SUPABASE_URL")
|
| 13 |
SUPABASE_KEY = os.environ.get("SUPABASE_SERVICE_ROLE_KEY")
|
| 14 |
|
| 15 |
-
if not SUPABASE_URL or not
|
| 16 |
raise ValueError("โ ูู
ูุชู
ุงูุนุซูุฑ ุนูู ู
ูุงุชูุญ Supabase ูู Secrets")
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
#SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
|
| 20 |
-
|
| 21 |
-
#if not SUPABASE_URL or not SUPABASE_KEY:
|
| 22 |
-
# raise Exception("ูุฌุจ ุชุนุฑูู SUPABASE_URL ู SUPABASE_SERVICE_ROLE_KEY ูู .env")
|
| 23 |
-
|
| 24 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 25 |
|
| 26 |
|
|
@@ -32,20 +24,28 @@ def save_law_to_supabase(law_json: dict):
|
|
| 32 |
law_json: dict ูุญุชูู ุนูู ุงูู
ูุงุชูุญ
|
| 33 |
title, preamble, number, year, sections
|
| 34 |
"""
|
|
|
|
| 35 |
title = law_json.get("title", "ุนููุงู ุบูุฑ ู
ุนุฑูู")
|
| 36 |
preamble = law_json.get("preamble", "")
|
| 37 |
law_number = law_json.get("number")
|
| 38 |
law_year = law_json.get("year")
|
| 39 |
sections = law_json.get("sections", [])
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
if existing.data:
|
| 45 |
law_id = existing.data[0]["id"]
|
| 46 |
print(f"โ ุงููุงููู ู
ูุฌูุฏ ู
ุณุจูุงูุ ูุชู
ุญุฐูู: {title}")
|
| 47 |
|
| 48 |
-
# ุญุฐู ุงูู
ูุงุฏ ุงู
|
| 49 |
secs = supabase.table("sections").select("id").eq("law_id", law_id).execute()
|
| 50 |
for s in secs.data:
|
| 51 |
supabase.table("articles").delete().eq("chapter_id", s["id"]).execute()
|
|
@@ -56,7 +56,9 @@ def save_law_to_supabase(law_json: dict):
|
|
| 56 |
# ุญุฐู ุงููุงููู ููุณู
|
| 57 |
supabase.table("laws").delete().eq("id", law_id).execute()
|
| 58 |
|
| 59 |
-
#
|
|
|
|
|
|
|
| 60 |
res_law = (
|
| 61 |
supabase.table("laws")
|
| 62 |
.insert({
|
|
@@ -68,44 +70,49 @@ def save_law_to_supabase(law_json: dict):
|
|
| 68 |
})
|
| 69 |
.execute()
|
| 70 |
)
|
|
|
|
| 71 |
law_id = res_law.data[0]["id"]
|
| 72 |
print(f"โ ุชู
ุฅุฏุฑุงุฌ ุงููุงููู ุงูุฌุฏูุฏ: {title}")
|
| 73 |
|
| 74 |
-
#
|
|
|
|
|
|
|
| 75 |
for sec in sections:
|
| 76 |
sec_title = sec.get("title", "")
|
| 77 |
sec_content = sec.get("content", "")
|
| 78 |
sec_tables = sec.get("tables", [])
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
res_sec = (
|
| 81 |
supabase.table("sections")
|
| 82 |
.insert({
|
| 83 |
"law_id": law_id,
|
| 84 |
"title": sec_title or "",
|
| 85 |
"content": sec_content or "",
|
| 86 |
-
"chapter_number":
|
| 87 |
-
if re.search(r"\d+", sec_title or "") else 0,
|
| 88 |
"tables": sec_tables or [],
|
| 89 |
})
|
| 90 |
.execute()
|
| 91 |
)
|
|
|
|
| 92 |
section_id = res_sec.data[0]["id"]
|
| 93 |
|
|
|
|
| 94 |
for art in sec.get("articles", []):
|
| 95 |
-
article_number
|
| 96 |
-
article_content
|
| 97 |
-
article_tables
|
| 98 |
|
| 99 |
supabase.table("articles").insert({
|
| 100 |
-
"chapter_id":
|
| 101 |
"article_number": article_number,
|
| 102 |
-
"content":
|
| 103 |
-
"tables":
|
| 104 |
}).execute()
|
| 105 |
|
| 106 |
-
print(f"โ ุชู
ุญูุธ ุฌู
ูุน ุงูุฃูุณุงู
ูุงูู
ูุงุฏ ูููุงููู: {title}")
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
# ---------------------------------------------
|
| 110 |
-
# ู
ุซุงู ุงุณุชุฎุฏุงู
(ูู
ูู ุญุฐูู ุฃู ุชุนูููู)
|
| 111 |
-
# ---------------------------------------------
|
|
|
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
import json
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# ---------------------------------------------
|
| 7 |
+
# ุชุญู
ูู ู
ูุงุชูุญ Supabase ู
ู Secrets
|
| 8 |
# ---------------------------------------------
|
|
|
|
| 9 |
SUPABASE_URL = os.environ.get("SUPABASE_URL")
|
| 10 |
SUPABASE_KEY = os.environ.get("SUPABASE_SERVICE_ROLE_KEY")
|
| 11 |
|
| 12 |
+
if not SUPABASE_URL or not SUPABASE_KEY:
|
| 13 |
raise ValueError("โ ูู
ูุชู
ุงูุนุซูุฑ ุนูู ู
ูุงุชูุญ Supabase ูู Secrets")
|
| 14 |
|
| 15 |
+
# ุฅูุดุงุก ุงูุนู
ูู
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 17 |
|
| 18 |
|
|
|
|
| 24 |
law_json: dict ูุญุชูู ุนูู ุงูู
ูุงุชูุญ
|
| 25 |
title, preamble, number, year, sections
|
| 26 |
"""
|
| 27 |
+
|
| 28 |
title = law_json.get("title", "ุนููุงู ุบูุฑ ู
ุนุฑูู")
|
| 29 |
preamble = law_json.get("preamble", "")
|
| 30 |
law_number = law_json.get("number")
|
| 31 |
law_year = law_json.get("year")
|
| 32 |
sections = law_json.get("sections", [])
|
| 33 |
|
| 34 |
+
# ---------------------------------------------------------
|
| 35 |
+
# 1) ุงูุชุญูู ู
ู ูุฌูุฏ ุงููุงููู ุณุงุจููุง
|
| 36 |
+
# ---------------------------------------------------------
|
| 37 |
+
existing = (
|
| 38 |
+
supabase.table("laws")
|
| 39 |
+
.select("id")
|
| 40 |
+
.eq("title", title)
|
| 41 |
+
.execute()
|
| 42 |
+
)
|
| 43 |
|
| 44 |
if existing.data:
|
| 45 |
law_id = existing.data[0]["id"]
|
| 46 |
print(f"โ ุงููุงููู ู
ูุฌูุฏ ู
ุณุจูุงูุ ูุชู
ุญุฐูู: {title}")
|
| 47 |
|
| 48 |
+
# ุญุฐู ุงูู
ูุงุฏ ุงูุชุงุจุนุฉ ููุฃูุณุงู
|
| 49 |
secs = supabase.table("sections").select("id").eq("law_id", law_id).execute()
|
| 50 |
for s in secs.data:
|
| 51 |
supabase.table("articles").delete().eq("chapter_id", s["id"]).execute()
|
|
|
|
| 56 |
# ุญุฐู ุงููุงููู ููุณู
|
| 57 |
supabase.table("laws").delete().eq("id", law_id).execute()
|
| 58 |
|
| 59 |
+
# ---------------------------------------------------------
|
| 60 |
+
# 2) ุฅุถุงูุฉ ุงููุงููู ุงูุฌุฏูุฏ
|
| 61 |
+
# ---------------------------------------------------------
|
| 62 |
res_law = (
|
| 63 |
supabase.table("laws")
|
| 64 |
.insert({
|
|
|
|
| 70 |
})
|
| 71 |
.execute()
|
| 72 |
)
|
| 73 |
+
|
| 74 |
law_id = res_law.data[0]["id"]
|
| 75 |
print(f"โ ุชู
ุฅุฏุฑุงุฌ ุงููุงููู ุงูุฌุฏูุฏ: {title}")
|
| 76 |
|
| 77 |
+
# ---------------------------------------------------------
|
| 78 |
+
# 3) ุฅุถุงูุฉ ุงูุฃูุณุงู
ูุงูู
ูุงุฏ
|
| 79 |
+
# ---------------------------------------------------------
|
| 80 |
for sec in sections:
|
| 81 |
sec_title = sec.get("title", "")
|
| 82 |
sec_content = sec.get("content", "")
|
| 83 |
sec_tables = sec.get("tables", [])
|
| 84 |
|
| 85 |
+
# ุงุณุชุฎุฑุงุฌ ุฑูู
ุงููุณู
ู
ู ุงูุนููุงู
|
| 86 |
+
chapter_number = 0
|
| 87 |
+
match = re.search(r"\d+", sec_title or "")
|
| 88 |
+
if match:
|
| 89 |
+
chapter_number = int(match.group(0))
|
| 90 |
+
|
| 91 |
res_sec = (
|
| 92 |
supabase.table("sections")
|
| 93 |
.insert({
|
| 94 |
"law_id": law_id,
|
| 95 |
"title": sec_title or "",
|
| 96 |
"content": sec_content or "",
|
| 97 |
+
"chapter_number": chapter_number,
|
|
|
|
| 98 |
"tables": sec_tables or [],
|
| 99 |
})
|
| 100 |
.execute()
|
| 101 |
)
|
| 102 |
+
|
| 103 |
section_id = res_sec.data[0]["id"]
|
| 104 |
|
| 105 |
+
# ----------------------- ุฅุถุงูุฉ ุงูู
ูุงุฏ -----------------------
|
| 106 |
for art in sec.get("articles", []):
|
| 107 |
+
article_number = int(art["number"]) if art.get("number") else None
|
| 108 |
+
article_content = art.get("text", "")
|
| 109 |
+
article_tables = art.get("tables", [])
|
| 110 |
|
| 111 |
supabase.table("articles").insert({
|
| 112 |
+
"chapter_id": section_id,
|
| 113 |
"article_number": article_number,
|
| 114 |
+
"content": article_content or "",
|
| 115 |
+
"tables": article_tables or [],
|
| 116 |
}).execute()
|
| 117 |
|
| 118 |
+
print(f"โ ุชู
ุญูุธ ุฌู
ูุน ุงูุฃูุณุงู
ูุงูู
ูุงุฏ ูููุงููู: {title}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|