#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 精选优质文章与分类/话题种子数据填充脚本 """ import json import uuid import hashlib import subprocess import sys PSQL = r"C:\Program Files\PostgreSQL\16\bin\psql.exe" def run_sql(sql): full_sql = "SET client_encoding = 'UTF8';\nBEGIN;\n" + sql + "\nCOMMIT;" p = subprocess.run( [PSQL, "-h", "127.0.0.1", "-p", "55432", "-U", "postgres", "-d", "article_platform", "-v", "ON_ERROR_STOP=1"], input=full_sql.encode("utf-8"), capture_output=True ) stdout = p.stdout.decode("utf-8", errors="replace") stderr = p.stderr.decode("utf-8", errors="replace") if p.returncode != 0 or "ERROR:" in stdout or "ERROR:" in stderr: print("SQL EXEC ERROR:") print(stderr) print(stdout) sys.exit(1) return stdout def new_uuid(): return str(uuid.uuid4()) def new_mask(): return "art_" + hashlib.md5(uuid.uuid4().bytes).hexdigest()[:16] # 1. 查询用户 ID get_users_sql = "SELECT account, id FROM users;" p = subprocess.run( [PSQL, "-h", "127.0.0.1", "-p", "55432", "-U", "postgres", "-d", "article_platform", "-t", "-A", "-c", get_users_sql], capture_output=True ) user_map = {} for line in p.stdout.decode("utf-8").strip().split("\n"): line = line.strip() if "|" in line: acct, uid = line.split("|") user_map[acct.strip()] = uid.strip() author_demo_id = user_map.get("author_demo") admin_id = user_map.get("admin") lin_id = user_map.get("traveler_lin", author_demo_id) cat_id = user_map.get("foodie_cat", author_demo_id) daily_id = user_map.get("design_daily", author_demo_id) print(f"Users: author_demo={author_demo_id}, admin={admin_id}, lin={lin_id}") # 2. 分类与话题定义 categories = [ {"slug": "travel", "name": "环球旅行", "icon_url": "https://images.unsplash.com/photo-1488646953014-85cb44e25828?auto=format&fit=crop&w=120&q=80", "sort_order": 1}, {"slug": "photography", "name": "光影摄影", "icon_url": "https://images.unsplash.com/photo-1516035069371-29a1b244cc32?auto=format&fit=crop&w=120&q=80", "sort_order": 2}, {"slug": "lifestyle", "name": "生活美学", "icon_url": "https://images.unsplash.com/photo-1513519245088-0e12902e5a38?auto=format&fit=crop&w=120&q=80", "sort_order": 3}, {"slug": "food", "name": "美食探店", "icon_url": "https://images.unsplash.com/photo-1504674900247-0877df9cc836?auto=format&fit=crop&w=120&q=80", "sort_order": 4}, {"slug": "culture", "name": "人文阅读", "icon_url": "https://images.unsplash.com/photo-1497633762265-9d179a990aa6?auto=format&fit=crop&w=120&q=80", "sort_order": 5}, {"slug": "tech", "name": "数码科技", "icon_url": "https://images.unsplash.com/photo-1519389950473-47ba0277781c?auto=format&fit=crop&w=120&q=80", "sort_order": 6}, ] cat_ids = {} cat_sql = [] for c in categories: cid = new_uuid() cat_ids[c["slug"]] = cid cat_sql.append( f"INSERT INTO categories (id, name, slug, sort_order, icon_url, status) " f"VALUES ('{cid}', '{c['name']}', '{c['slug']}', {c['sort_order']}, '{c['icon_url']}', 'active');" ) topics = [ {"name": "秋日银杏漫游记", "cat": "travel", "desc": "记录街头巷尾的金黄秋色与落叶故事", "cover": "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=600&q=80"}, {"name": "胶片摄影的质感", "cat": "photography", "desc": "慢下来的快门,留住颗粒与时光的温存", "cover": "https://images.unsplash.com/photo-1516035069371-29a1b244cc32?auto=format&fit=crop&w=600&q=80"}, {"name": "咖啡馆办公指南", "cat": "lifestyle", "desc": "寻找一座城市里最让人灵感迸发的街角咖啡角", "cover": "https://images.unsplash.com/photo-1501339847302-ac426a4a7cbb?auto=format&fit=crop&w=600&q=80"}, {"name": "藏在巷子里的老字号", "cat": "food", "desc": "探寻几十年如一日的民间地道烟火滋味", "cover": "https://images.unsplash.com/photo-1555396273-367ea4eb4db5?auto=format&fit=crop&w=600&q=80"}, {"name": "私房书单与阅读笔记", "cat": "culture", "desc": "在纸页翻飞间与有趣的灵魂相遇", "cover": "https://images.unsplash.com/photo-1497633762265-9d179a990aa6?auto=format&fit=crop&w=600&q=80"}, {"name": "桌面美学与极客装备", "cat": "tech", "desc": "打造心流涌动的效率工作站与好物分享", "cover": "https://images.unsplash.com/photo-1518770660439-4636190af475?auto=format&fit=crop&w=600&q=80"}, ] topic_ids = {} topic_sql = [] for t in topics: tid = new_uuid() topic_ids[t["name"]] = tid cid = cat_ids[t["cat"]] topic_sql.append( f"INSERT INTO topics (id, name, description, cover_url, category_id, follow_count, article_count, status, creator_id) " f"VALUES ('{tid}', '{t['name']}', '{t['desc']}', '{t['cover']}', '{cid}', 128, 1, 'active', '{author_demo_id}');" ) # 3. 精选文章定义 articles_data = [ { "title": "深秋漫步指南:藏在金陵红墙白桦里的秋日光影", "abstract": "每年十月下旬,整座城市便悄然换上了金色的礼服。从朝天宫的红墙飞檐到颐和路的梧桐深处,这是一份献给秋天的慢行漫步清单。", "cover": "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=900&q=80", "owner": author_demo_id, "cat": "travel", "topic": "秋日银杏漫游记", "views": 3280, "likes": 268, "favs": 89, "shares": 34, "sections": [ { "kind": "text", "payload": { "plain_text": "秋风吹过金陵城的每个角落,红墙、黑瓦与金黄的落叶在斜阳下交织成一幅浓郁的水墨画卷。在快节奏的日常里,总需要这样一个午后,关掉通知,踩在沙沙作响的落叶上慢行。", "text_align": "left" } }, { "kind": "subtitle", "payload": {"text": "一、朝天宫:红墙下的六百年银杏", "bold": True} }, { "kind": "image", "payload": { "images": [ {"url": "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=800&q=80", "caption": "红墙下的金黄银杏叶"}, {"url": "https://images.unsplash.com/photo-1508873696983-2df5293cb32b?auto=format&fit=crop&w=800&q=80", "caption": "古建筑飞檐上的秋日暖阳"} ], "layout": "grid" } }, { "kind": "text", "payload": { "plain_text": "朝天宫崇圣殿前的两株古银杏已有数百年历史。午后两点半的光线打在朱红宫墙上,风一吹,落叶如雨洒在青石板上。偶有古装打扮的游人倚着红柱小憩,光斑落在衣袂间,恍若隔世穿越。", "text_align": "left" } }, { "kind": "location", "payload": { "name": "南京朝天宫", "address": "江苏省南京市秦淮区王府大街朝天宫4号", "latitude": 32.036, "longitude": 118.775 } }, { "kind": "subtitle", "payload": {"text": "二、颐和路:一条民国路,半部民国史", "bold": True} }, { "kind": "text", "payload": { "plain_text": "从朝天宫出来,骑行二十分钟便到了颐和路街区。这里保留着百余栋风格各异的民国公馆建筑。秋意深浓时,高大蔽日的法国梧桐叶逐渐变黄,将狭窄安静的街道染成浓郁的金黄色。", "text_align": "left" } }, { "kind": "image", "payload": { "images": [ {"url": "https://images.unsplash.com/photo-1477959858617-67f30bc75b82?auto=format&fit=crop&w=900&q=80", "caption": "宁静幽深的梧桐大道"} ], "layout": "single" } }, { "kind": "text", "payload": { "plain_text": "【出游漫步贴士】\n1. 最佳光线:下午 15:00 - 17:00,此时西晒光线最为柔和饱满,逆光拍人像发丝带金边。\n2. 穿搭建议:浅米色、卡其色或深焦糖色大衣,与红墙和黄叶对比鲜明。\n3. 交通方式:街区街道狭窄,强烈建议共享单车慢骑或步行体验。", "text_align": "left" } } ] }, { "title": "胶片慢生活:用一台旁轴相机记录城市的呼吸", "abstract": "在数码快门廉价泛滥的今天,每卷36张的仪式感,让我们重新学会观察光线、等待瞬间与拥抱不确定性。", "cover": "https://images.unsplash.com/photo-1516035069371-29a1b244cc32?auto=format&fit=crop&w=900&q=80", "owner": lin_id, "cat": "photography", "topic": "胶片摄影的质感", "views": 2540, "likes": 312, "favs": 142, "shares": 56, "sections": [ { "kind": "text", "payload": { "plain_text": "很多人问我为什么依然随身带着沉甸甸的机械旁轴相机。在按动快门的一瞬间,没有即时的液晶屏预览,没有连拍选片的从容,有的只是取景器里重合的黄斑,以及对光线与构图的专注凝视。", "text_align": "left" } }, { "kind": "subtitle", "payload": {"text": "「机械的纯粹:齿轮与过片扳手」", "bold": True} }, { "kind": "image", "payload": { "images": [ {"url": "https://images.unsplash.com/photo-1526170375885-4d8ecf77b99f?auto=format&fit=crop&w=800&q=80", "caption": "机械机身的金属冷冽与温润触感"}, {"url": "https://images.unsplash.com/photo-1495121605193-b116b5b9c5fe?auto=format&fit=crop&w=800&q=80", "caption": "街角抓拍的自然光影"} ], "layout": "grid" } }, { "kind": "text", "payload": { "plain_text": "柯达色彩的温暖浓郁,富士胶片的青绿通透,甚至伊尔福黑白的颗粒层次,每一种底片都有它自己的脾气与灵魂。你把光线托付给化学乳剂,底片冲洗出来的那一刻,就像拆开一封来自过去的信件。", "text_align": "left" } }, { "kind": "vote", "payload": { "question": "你平时记录生活最常用哪种器材?", "vote_type": "single", "options": [ {"id": "opt_1", "text": "智能手机(随时随地最轻便)", "vote_count": 215}, {"id": "opt_2", "text": "数码微单(画质与追焦担当)", "vote_count": 142}, {"id": "opt_3", "text": "胶片/拍立得(复古仪式感派)", "vote_count": 88} ] } }, { "kind": "text", "payload": { "plain_text": "摄影的真谛不是为了在社交网络上快速消费,而是为了在按下快门的那一刻,让自己真正融入周围的世界。", "text_align": "left" } } ] }, { "title": "城市隐秘角落:老街胡同里的百年手工铜锅与炙子烤肉", "abstract": "真正的市井至味,从不在喧嚣的商场顶层,而在烟火升腾的窄巷深处。炭火清汤,手切鲜羊,一餐抚凡人心。", "cover": "https://images.unsplash.com/photo-1555396273-367ea4eb4db5?auto=format&fit=crop&w=900&q=80", "owner": cat_id, "cat": "food", "topic": "藏在巷子里的老字号", "views": 4120, "likes": 486, "favs": 230, "shares": 78, "sections": [ { "kind": "text", "payload": { "plain_text": "推开挂着厚棉帘的木门,热腾腾的白气迎面扑来,麻酱的浓香混合着炭火的气息,是降温天里最治愈的市井温存。", "text_align": "left" } }, { "kind": "subtitle", "payload": {"text": "「炭火铜锅:清汤见底的手切鲜羊肉」", "bold": True} }, { "kind": "image", "payload": { "images": [ {"url": "https://images.unsplash.com/photo-1544025162-d76694265947?auto=format&fit=crop&w=800&q=80", "caption": "咕嘟冒泡的传统炭火铜锅"}, {"url": "https://images.unsplash.com/photo-1555396273-367ea4eb4db5?auto=format&fit=crop&w=800&q=80", "caption": "红白相间的手切鲜羊上脑"} ], "layout": "grid" } }, { "kind": "text", "payload": { "plain_text": "好的铜锅讲究汤清肉纯。老铜锅里沸水滚滚,几片生姜葱段足矣。手切羊上脑肉质紧实,盘子竖立起来不掉分毫。夹起一箸入水八上八下,肉色微变即捞出,裹上厚实黏稠的二八芝麻酱,鲜甜嫩滑,毫无膻气。", "text_align": "left" } }, { "kind": "location", "payload": { "name": "牛街传统老铜锅", "address": "北京市西城区牛街西里商业街5号楼", "latitude": 39.886, "longitude": 116.362 } }, { "kind": "text", "payload": { "plain_text": "吃铜锅少不了一烧饼。刚出炉的麻酱烧饼外壳金黄酥脆,轻轻一掰掉渣如雪,就着热羊汤落肚,通体舒泰。", "text_align": "left" } } ] }, { "title": "街角灵感站:如何在一家舒适的独立咖啡馆高效沉浸", "abstract": "找到一张高度适中的木桌,一杯微酸的水洗耶加雪菲,以及恰到好处的环境白噪音,心流便自然流淌。", "cover": "https://images.unsplash.com/photo-1501339847302-ac426a4a7cbb?auto=format&fit=crop&w=900&q=80", "owner": author_demo_id, "cat": "lifestyle", "topic": "咖啡馆办公指南", "views": 1960, "likes": 178, "favs": 95, "shares": 22, "sections": [ { "kind": "text", "payload": { "plain_text": "自由职业与远程办公的普及,让咖啡馆成为了现代城市中充满创造力的第三空间。走出沉闷的书房,选一个靠窗的角落,思路往往豁然开朗。", "text_align": "left" } }, { "kind": "subtitle", "payload": {"text": "「舒适工位的三大准则」", "bold": True} }, { "kind": "text", "payload": { "plain_text": "1. 桌椅比例:拒绝低矮茶几与无支撑软沙发,72-75cm高度的实木大桌才是码字神仙配置。\n2. 漫射自然光:侧向采光既能看清屏幕,又不会有刺眼的反射眩光。\n3. 电源与Wi-Fi:随手可及的插座免去电量焦虑。", "text_align": "left" } }, { "kind": "image", "payload": { "images": [ {"url": "https://images.unsplash.com/photo-1501339847302-ac426a4a7cbb?auto=format&fit=crop&w=900&q=80", "caption": "暖色调木质桌面与手冲咖啡"} ], "layout": "single" } }, { "kind": "subtitle", "payload": {"text": "「白噪音心理学」", "bold": True} }, { "kind": "text", "payload": { "plain_text": "环境心理学研究表明,70分贝左右的中等环境噪音能有效打乱固定思维定势,激发抽象想象力与创造力。咖啡馆里磨豆机嗡鸣、杯盘轻磕与细碎低语,正是最天然的专注白噪音。", "text_align": "left" } } ] }, { "title": "重返经典:在碎片化浪潮中,我们为何仍需要深度阅读", "abstract": "当算法源源不断推送15秒短视频时,耐下心来读完一本300页的长卷,是一场抵御精神熵增的静默抵抗。", "cover": "https://images.unsplash.com/photo-1497633762265-9d179a990aa6?auto=format&fit=crop&w=900&q=80", "owner": lin_id, "cat": "culture", "topic": "私房书单与阅读笔记", "views": 2890, "likes": 345, "favs": 198, "shares": 67, "sections": [ { "kind": "text", "payload": { "plain_text": "在信息瀑布日夜倾泻的当下,我们的大脑被快速分泌的多巴胺训练得越来越难以忍受长时间的专注。然而,只有深度阅读才能构建完整的知识框架与深邃的思想内核。", "text_align": "left" } }, { "kind": "subtitle", "payload": {"text": "「纸页翻飞间的立体坐标」", "bold": True} }, { "kind": "image", "payload": { "images": [ {"url": "https://images.unsplash.com/photo-1497633762265-9d179a990aa6?auto=format&fit=crop&w=900&q=80", "caption": "案头堆叠的书籍与柔和台灯"} ], "layout": "single" } }, { "kind": "text", "payload": { "plain_text": "纸质书有着独特的厚度与重量。当你读过三分之一、一半、乃至行将终卷,手指能感知到时间的流逝与思想的沉淀。这种物理触感是大脑构筑空间记忆的重要锚点。", "text_align": "left" } }, { "kind": "subtitle", "payload": {"text": "「秋日私房书单推荐」", "bold": True} }, { "kind": "text", "payload": { "plain_text": "• 《瓦尔登湖》- 亨利·戴维·梭罗:在极简中探寻生命的真谛。\n• 《看不见的城市》- 伊塔洛·卡尔维诺:用诗意重构人类对栖居地的想象。\n• 《澄明之境》- 体验心流与宁静力量的散文集。", "text_align": "left" } } ] }, { "title": "极简效率美学:打造让人心流涌动的桌面工作站", "abstract": "一个干净纯粹的工作台,是对专注力最大的尊重。无杂线遮挡的视野,让思考回归本质,让创作充满乐趣。", "cover": "https://images.unsplash.com/photo-1518770660439-4636190af475?auto=format&fit=crop&w=900&q=80", "owner": daily_id, "cat": "tech", "topic": "桌面美学与极客装备", "views": 3720, "likes": 420, "favs": 265, "shares": 83, "sections": [ { "kind": "text", "payload": { "plain_text": "桌面搭建绝不是为了无意义的硬件堆砌,而是通过合理的动线设计、线缆隐形与光影渲染,为自己打造一个坐下来就能迅速进入心流状态的专属精神基地。", "text_align": "left" } }, { "kind": "subtitle", "payload": {"text": "「视野净化:隐藏所有线缆」", "bold": True} }, { "kind": "image", "payload": { "images": [ {"url": "https://images.unsplash.com/photo-1518770660439-4636190af475?auto=format&fit=crop&w=800&q=80", "caption": "极简原木桌搭与悬浮主屏"}, {"url": "https://images.unsplash.com/photo-1587829741301-dc798b83add3?auto=format&fit=crop&w=800&q=80", "caption": "机械键盘与氛围光影"} ], "layout": "grid" } }, { "kind": "text", "payload": { "plain_text": "桌底安装理线槽与魔术贴,将电源适配器与冗长线缆全部藏于视线盲区。桌面上只保留一块主屏、一把手感温润的机械键盘、一枚人体工学无线鼠标,视觉负担降到最低。", "text_align": "left" } }, { "kind": "subtitle", "payload": {"text": "「灯光的魔力:非对称屏幕挂灯」", "bold": True} }, { "kind": "text", "payload": { "plain_text": "入夜之后,关掉刺眼的吸顶大灯,只点亮屏幕挂灯与背景氛围灯带。非对称光源精准照亮键盘工作区而不照射屏幕引起反光,营造出安静包裹的深夜创作氛围。", "text_align": "left" } } ] } ] # 4. 生成插入 SQL sql_statements = [] sql_statements.extend(cat_sql) sql_statements.extend(topic_sql) for art in articles_data: aid = new_uuid() mask_id = new_mask() cid = cat_ids[art["cat"]] tid = topic_ids[art["topic"]] title = art["title"].replace("'", "''") abstract = art["abstract"].replace("'", "''") cover = art["cover"] owner = art["owner"] views = art["views"] likes = art["likes"] favs = art["favs"] shares = art["shares"] art_sql = f""" INSERT INTO articles ( id, mask_id, title, subtitle, abstract, cover_img_url, visual_config, interaction_config, enable_watermark, enable_comment, privacy, has_reward, owner_id, category_id, topic_id, status, review_status, review_note, reviewed_by, reviewed_at, published_at, view_count, like_count, fav_count, comment_count, share_count, schema_version ) VALUES ( '{aid}', '{mask_id}', '{title}', '', '{abstract}', '{cover}', '{{}}'::jsonb, '{{"enable_comment":true}}'::jsonb, false, true, 'public', false, '{owner}', '{cid}', '{tid}', 'published', 'approved', '优质精选', '{admin_id}', now(), now(), {views}, {likes}, {favs}, 0, {shares}, 1 ); """ sql_statements.append(art_sql) # 插入 sections for idx, sec in enumerate(art["sections"]): sid = new_uuid() kind = sec["kind"] payload_json = json.dumps(sec["payload"], ensure_ascii=False).replace("'", "''") sec_sql = f""" INSERT INTO sections (id, article_id, order_index, kind, payload) VALUES ('{sid}', '{aid}', {idx}, '{kind}', '{payload_json}'::jsonb); """ sql_statements.append(sec_sql) print(f"Total SQL statements to run: {len(sql_statements)}") # 执行批量插入 output = run_sql("\n".join(sql_statements)) print("Execution success!") print(output)