52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
import re
|
|
import csv
|
|
import sys
|
|
|
|
csv.field_size_limit(sys.maxsize)
|
|
|
|
def parse_sql_dump(filepath, table_name):
|
|
rows = []
|
|
columns = []
|
|
in_insert = False
|
|
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
|
for line in f:
|
|
if line.startswith(f"INSERT INTO `{table_name}`"):
|
|
col_match = re.search(r'\((.*?)\)', line)
|
|
if col_match:
|
|
columns = [c.strip('` ') for c in col_match.group(1).split(',')]
|
|
in_insert = True
|
|
continue
|
|
|
|
if in_insert:
|
|
if line.startswith('('):
|
|
val_str = line.strip().rstrip(',;')
|
|
if val_str.endswith(')'):
|
|
inner = val_str[1:-1]
|
|
inner = inner.replace("\\'", "''")
|
|
|
|
try:
|
|
for parsed_row in csv.reader([inner], quotechar="'", escapechar="\\", skipinitialspace=True):
|
|
if len(parsed_row) == len(columns):
|
|
rows.append(dict(zip(columns, parsed_row)))
|
|
except Exception:
|
|
pass
|
|
|
|
if line.strip().endswith(';'):
|
|
in_insert = False
|
|
|
|
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.")
|
|
|
|
types = {}
|
|
for p in posts:
|
|
pt = p.get('post_type', 'unknown')
|
|
types[pt] = types.get(pt, 0) + 1
|
|
|
|
for t, c in types.items():
|
|
print(f" {t}: {c}")
|
|
|