Download app.py from blueellia/casting: direct link, hf CLI and curl.
- Browser
- Download file 9.98 kB
-
https://huggingface.co/spaces/blueellia/casting/resolve/main/app.py
- Command line
-
hf download hf://spaces/blueellia/casting/app.py
-
curl -L -o app.py https://huggingface.co/spaces/blueellia/casting/resolve/main/app.py
9.98 kB
| import io, re, time, requests, gc | |
| import pandas as pd | |
| import streamlit as st | |
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| from openpyxl import Workbook | |
| from openpyxl.styles import Font, PatternFill, Alignment, Border, Side | |
| from openpyxl.utils import get_column_letter | |
| st.set_page_config(page_title="์ฌ์ ์ฅ ์ฃผ์ยท์ ํ๋ฒํธ ๋ณด์", page_icon="๐", layout="centered") | |
| st.markdown("""<style> | |
| #MainMenu,footer,header{visibility:hidden} | |
| .block-container{max-width:660px!important;padding:2rem 1rem!important} | |
| div.stDownloadButton>button{background:#10b981!important;color:#fff!important;border:none!important;border-radius:8px!important;font-weight:600!important;width:100%!important} | |
| </style>""", unsafe_allow_html=True) | |
| FSC_KEY = "2446da601452d231f1ff77a4c5ac9598896028fb96e8be586bf7f4be58b6231c" | |
| KAKAO_KEY = "e8eef7246a2cd0f3d2d55af8a806e3ef" | |
| NAV_HDR = {"User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15","Referer":"https://map.naver.com/","Accept-Language":"ko-KR,ko;q=0.9"} | |
| WORKERS = 20 | |
| def kakao_keyword(name, addr): | |
| try: | |
| r = requests.get("https://dapi.kakao.com/v2/local/search/keyword.json", | |
| headers={"Authorization":f"KakaoAK {KAKAO_KEY}"}, | |
| params={"query":f"{name} {addr[:20]}","size":5}, timeout=5) | |
| docs = r.json().get("documents",[]) | |
| gu = re.search(r'[\w]+๊ตฌ|[\w]+๊ตฐ', addr) | |
| gu = gu.group() if gu else "" | |
| if gu: | |
| local = [d for d in docs if gu in d.get("address_name","") or gu in d.get("road_address_name","")] | |
| if local: | |
| return next((d for d in local if name[:4] in d.get("place_name","")), local[0]) | |
| return next((d for d in docs if name[:4] in d.get("place_name","")), docs[0] if docs else None) | |
| except: return None | |
| def fsc_info(name, biz_no): | |
| try: | |
| r = requests.get( | |
| "https://apis.data.go.kr/1160100/service/GetCorpBasicInfoService_V2/getCorpOutline_V2", | |
| params={"serviceKey":FSC_KEY,"pageNo":1,"numOfRows":5,"resultType":"json","corpNm":name}, timeout=5) | |
| items = r.json().get("response",{}).get("body",{}).get("items",{}).get("item",[]) | |
| if not items: return None | |
| if isinstance(items, dict): items = [items] | |
| bc = re.sub(r'\D','',str(biz_no)) | |
| m = next((i for i in items if re.sub(r'\D','',str(i.get("bzno",""))) == bc), items[0]) | |
| return {"phone":m.get("enpTlno",""), "addr":m.get("enpBsadr","") or m.get("enpDtadr","")} | |
| except: return None | |
| def naver_phone(name, addr): | |
| clean = re.sub(r'์ฃผ์ํ์ฌ|์ ํํ์ฌ|\(์ฃผ\)|\(์ \)|[(){}\[\]]','',name).strip() | |
| gu = re.search(r'[\w]+๊ตฌ', addr); gu = gu.group() if gu else "" | |
| try: | |
| r = requests.get("https://m.map.naver.com/search2/search.nhn", | |
| headers=NAV_HDR, params={"query":f"{clean} {gu}","type":"all"}, timeout=8) | |
| if r.status_code == 429: time.sleep(30); return naver_phone(name, addr) | |
| for pat in [r'tel:(0\d{1,2}-?\d{3,4}-?\d{4})',r'(?<!\d)(0\d{1,2}-\d{3,4}-\d{4})(?!\d)']: | |
| f = re.findall(pat, r.text) | |
| if f: return f[0] | |
| except: pass | |
| return "" | |
| def process_one(row_data): | |
| idx, name, addr, biz_no, is_corp, gu = row_data | |
| phone=""; full_addr=addr; lat=None; lon=None | |
| if is_corp: | |
| info = fsc_info(name, biz_no) | |
| if info: | |
| phone = info.get("phone","") | |
| # FSC ์ฃผ์๋ ๋ณธ์ฌ ์ฃผ์๋ผ ๋ฎ์ด์ฐ์ง ์์ | |
| time.sleep(0.1) | |
| kk = kakao_keyword(name, addr) | |
| if kk: | |
| if not phone: phone = kk.get("phone","") | |
| road = kk.get("road_address_name","") or kk.get("address_name","") | |
| # ์นด์นด์ค ๊ฒฐ๊ณผ๊ฐ ๊ฐ์ ๊ตฌ/๊ตฐ์ผ ๋๋ง ์ฃผ์ ์ ๋ฐ์ดํธ | |
| if road and gu and gu in road: | |
| full_addr = road | |
| lat = float(kk.get("y") or 0) or None | |
| lon = float(kk.get("x") or 0) or None | |
| elif not road or (gu and gu not in road): | |
| # ์๋ฑํ ์ง์ญ ๊ฒฐ๊ณผ โ ์๋ณธ ์ฃผ์ ์ ์ง, ์ขํ๋ง ์ทจ๋ | |
| lat = float(kk.get("y") or 0) or None | |
| lon = float(kk.get("x") or 0) or None | |
| if not phone: phone = naver_phone(name, full_addr) | |
| return idx, {"phone":phone, "addr":full_addr, "lat":lat, "lon":lon} | |
| def make_excel(df): | |
| wb = Workbook(); ws = wb.active; ws.title = "๋ณด์์๋ฃ" | |
| def thin(): | |
| s = Side(style="thin",color="D0D0D0") | |
| return Border(left=s,right=s,top=s,bottom=s) | |
| hdrs = list(df.columns) | |
| widths = {"์ฌ์ ์ฅ๋ช ":26,"์ ์ข ":18,"๋ฒ์ธ์ฌ๋ถ":8,"์ฃผ์":42,"์ ํ๋ฒํธ":14,"๊ฐ์ ์์":8,"๋น์๊ณ ์ง๊ธ์ก":14,"์๋":12,"๊ฒฝ๋":12} | |
| CENTER = {"๋ฒ์ธ์ฌ๋ถ","๊ฐ์ ์์","๋น์๊ณ ์ง๊ธ์ก","์๋","๊ฒฝ๋","์ ํ๋ฒํธ"} | |
| for c,h in enumerate(hdrs,1): | |
| cell = ws.cell(1,c,h) | |
| cell.fill = PatternFill("solid",start_color="1F3864") | |
| cell.font = Font(name="Arial",size=10,bold=True,color="FFFFFF") | |
| cell.alignment = Alignment(horizontal="center",vertical="center") | |
| cell.border = thin() | |
| ws.column_dimensions[get_column_letter(c)].width = widths.get(h,12) | |
| ws.row_dimensions[1].height = 18 | |
| ws.freeze_panes = "A2" | |
| ws.auto_filter.ref = f"A1:{get_column_letter(len(hdrs))}1" | |
| for ri,row in enumerate(df.itertuples(index=False),2): | |
| for ci,(h,v) in enumerate(zip(hdrs,row),1): | |
| if pd.isna(v) or str(v) in ("nan","None"): v="" | |
| cell = ws.cell(ri,ci,v) | |
| cell.font = Font(name="Arial",size=10) | |
| cell.border = thin() | |
| cell.fill = PatternFill("solid",start_color="FFFFFF" if ri%2==0 else "F8FAFC") | |
| cell.alignment = Alignment(horizontal="center" if h in CENTER else "left",vertical="center") | |
| buf = io.BytesIO(); wb.save(buf); return buf.getvalue() | |
| # โโ UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| st.title("๐ ์ฌ์ ์ฅ ์ฃผ์ยท์ ํ๋ฒํธ ๋ณด์") | |
| st.caption("์ง์ญ ํ์ผ ์ ๋ก๋ โ ์๋ ์ฒ๋ฆฌ โ ๋ค์ด๋ก๋") | |
| st.divider() | |
| uploaded = st.file_uploader("ํ์ผ ์ ํ (๊ตญ๋ฏผ์ฐ๊ธ_XX์_XX๊ตฌ_์ฌ์ ์ฅ.xlsx)", type=["xlsx"]) | |
| if uploaded: | |
| df = pd.read_excel(uploaded) | |
| df.columns = [c.strip() for c in df.columns] | |
| gc.collect() | |
| name_col = next((c for c in df.columns if "์ฌ์ ์ฅ๋ช " in c or "์ํธ" in c), None) | |
| addr_col = next((c for c in df.columns if "์ฃผ์" in c or "๋๋ก๋ช " in c), None) | |
| biz_col = next((c for c in df.columns if "์ฌ์ ์" in c and "๋ฒํธ" in c), None) | |
| type_col = next((c for c in df.columns if "ํํ" in c or "๋ฒ์ธ" in c), None) | |
| emp_col = next((c for c in df.columns if "๊ฐ์ ์" in c), None) | |
| pay_col = next((c for c in df.columns if "๊ณ ์ง" in c), None) | |
| ind_col = next((c for c in df.columns if "์ ์ข " in c), None) | |
| for col in ["์ ํ๋ฒํธ"]: | |
| if col not in df.columns: df[col] = "" | |
| else: df[col] = df[col].fillna("").astype(str).replace("nan","") | |
| for col in ["์๋","๊ฒฝ๋"]: | |
| if col not in df.columns: df[col] = None | |
| else: df[col] = pd.to_numeric(df[col], errors="coerce") | |
| total = len(df) | |
| fname = uploaded.name.replace(".xlsx","") | |
| st.success(f"โ {fname} โ {total:,}๊ฐ ๋ก๋ ์๋ฃ") | |
| prog = st.progress(0, text="์ฒ๋ฆฌ ์ค...") | |
| phone_ok = coord_ok = completed = 0 | |
| # ๊ตฌ/๊ตฐ ์ถ์ถ (์๋ฑํ ์ง์ญ ํํฐ๋ง์ฉ) | |
| gu = "" | |
| if addr_col: | |
| sample = df[addr_col].dropna().iloc[0] if len(df) > 0 else "" | |
| m = re.search(r'[\w]+๊ตฌ|[\w]+๊ตฐ', str(sample)) | |
| gu = m.group() if m else "" | |
| tasks = [] | |
| for idx in df.index: | |
| row = df.loc[idx] | |
| tasks.append((idx, | |
| str(row.get(name_col,"")) if name_col else "", | |
| str(row.get(addr_col,"")) if addr_col else "", | |
| str(row.get(biz_col,"")) if biz_col else "", | |
| str(row.get(type_col,"")) in ["๋ฒ์ธ","1"] if type_col else False, | |
| gu)) | |
| with ThreadPoolExecutor(max_workers=WORKERS) as executor: | |
| futures = {executor.submit(process_one, t): t[0] for t in tasks} | |
| for future in as_completed(futures): | |
| try: | |
| idx, res = future.result() | |
| df.loc[idx,"์ ํ๋ฒํธ"] = str(res["phone"]) | |
| df.loc[idx,"์๋"] = res["lat"] | |
| df.loc[idx,"๊ฒฝ๋"] = res["lon"] | |
| if addr_col and res["addr"]: df.loc[idx,addr_col] = res["addr"] | |
| if res["phone"]: phone_ok += 1 | |
| if res["lat"]: coord_ok += 1 | |
| except: pass | |
| completed += 1 | |
| prog.progress(completed/total, text=f"๐{phone_ok} ๐{coord_ok} | {completed}/{total}") | |
| prog.empty() | |
| col_map = {} | |
| if name_col: col_map[name_col]="์ฌ์ ์ฅ๋ช " | |
| if type_col: col_map[type_col]="๋ฒ์ธ์ฌ๋ถ" | |
| if addr_col: col_map[addr_col]="์ฃผ์" | |
| col_map["์ ํ๋ฒํธ"]="์ ํ๋ฒํธ" | |
| if emp_col: col_map[emp_col]="๊ฐ์ ์์" | |
| if pay_col: col_map[pay_col]="๋น์๊ณ ์ง๊ธ์ก" | |
| if ind_col: col_map[ind_col]="์ ์ข " | |
| col_map["์๋"]="์๋"; col_map["๊ฒฝ๋"]="๊ฒฝ๋" | |
| out = df[[c for c in col_map if c in df.columns]].rename(columns=col_map) | |
| # ์ ํ๋ฒํธ/์ฃผ์ ์์ผ๋ฉด ์ญ์ + ํด๋น ๊ตฌ/๊ตฐ ์๋ ์ฃผ์๋ ์ญ์ | |
| mask = (out["์ ํ๋ฒํธ"].notna() & (out["์ ํ๋ฒํธ"]!="") & | |
| out["์ฃผ์"].notna() & (out["์ฃผ์"]!="")) | |
| if gu: | |
| mask = mask & out["์ฃผ์"].str.contains(gu, na=False) | |
| out = out[mask].reset_index(drop=True) | |
| del df; gc.collect() | |
| st.success(f"โ ์๋ฃ! ๐{phone_ok:,}๊ฐ | ๐{coord_ok:,}๊ฐ | ์ต์ข {len(out):,}๊ฐ") | |
| st.balloons() | |
| st.download_button("โฌ๏ธ ๋ณด์ ์๋ฃ ์์ ๋ค์ด๋ก๋", | |
| make_excel(out), fname+"_๋ณด์์๋ฃ.xlsx", | |
| "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | |
| use_container_width=True) | |