import re import csv from ast import literal_eval def parse_sql_dump(filepath, table_name): # This is a robust parser for phpMyAdmin SQL dumps # It finds INSERT INTO `table_name` and extracts rows. in_insert = False columns = [] rows = [] with open(filepath, 'r', encoding='utf-8') as f: for line in f: if line.startswith(f"INSERT INTO `{table_name}`"): # Extract columns col_match = re.search(r'\((.*?)\)', line) if col_match: columns = [c.strip('` ') for c in col_match.group(1).split(',')] # Extract values part val_str = line[line.find("VALUES") + 6:].strip().rstrip(';') # We need to split by `), (` but be careful about strings. # A robust way is to use a state machine state = 'out' current_row = [] current_val = [] escape = False i = 0 while i < len(val_str): c = val_str[i] if state == 'out': if c == '(': state = 'in_row' current_row = [] current_val = [] elif state == 'in_row': if c == "'": if escape: current_val.append(c) escape = False else: state = 'in_string' elif c == ',': current_row.append("".join(current_val).strip()) current_val = [] elif c == ')': current_row.append("".join(current_val).strip()) rows.append(dict(zip(columns, current_row))) state = 'out' elif c != ' ': current_val.append(c) elif state == 'in_string': if c == "'": if i + 1 < len(val_str) and val_str[i+1] == "'": current_val.append("'") i += 1 # skip next else: state = 'in_row' elif c == '\\': escape = True else: if escape: if c == 'n': current_val.append('\n') elif c == 'r': current_val.append('\r') elif c == 't': current_val.append('\t') else: current_val.append(c) escape = False else: current_val.append(c) i += 1 return rows print("Parsing sis_posts...") posts = parse_sql_dump("sisvietnamvn_Trang chính thức hiện tại/Database/sisvietnam_db.sql", "sis_posts") print(f"Found {len(posts)} posts.") # Let's count by post_type types = {} for p in posts: pt = p.get('post_type', 'unknown') if pt.startswith("'") and pt.endswith("'"): pt = pt[1:-1] types[pt] = types.get(pt, 0) + 1 for t, c in types.items(): print(f" {t}: {c}")