MaxKB+WeWeRSS搭建公众号新闻小助手

一、 部署WeWeRSS

项目官网:https://github.com/cooderl/wewe-rss
离线镜像:百度网盘 请输入提取码 提取码: wzw8
访问地址:http://ip:4000
具体的部署方式可以参考官方文档,导入镜像后使用docker-compose方式部署比较方便。

二、 创建公众号RSS源

通过微信读书APP进行扫码:

扫码后在“公众号源”中添加一个 MaxKB 微信公众号的文章分享链接,会自动读取公众号中的其他文章。

点击“RSS”获取RSS链接(更多RSS源过滤规则参考项目官网):

三、创建应用

添加开场白:

添加函数获取新闻列表:

import requests
import json
import xml.etree.ElementTree as ET
 
def get_rss_feed():
    """
    读取并解析RSS源
    :return: RSS条目列表(每个条目包含标题、链接和发布日期)
    """
    rss_url = "http://119.29.xx.xx:4000/feeds/all.atom"
    news_list = []
 
    try:
        # 发送GET请求
        response = requests.get(rss_url)
 
        # 确保使用正确的字符编码
        response.encoding = 'utf-8'
 
        # 检查请求是否成功
        if response.status_code == 200:
            # 解析RSS(Atom格式)XML内容
            root = ET.fromstring(response.text)
 
            # 遍历所有条目
            for entry in root.findall('{http://www.w3.org/2005/Atom}entry'):
                # 提取标题
                title = entry.find('{http://www.w3.org/2005/Atom}title').text if entry.find('{http://www.w3.org/2005/Atom}title') is not None else 'No title'
                # 提取链接
                link = entry.find('{http://www.w3.org/2005/Atom}link').attrib['href'] if entry.find('{http://www.w3.org/2005/Atom}link') is not None else 'No link'
                # 提取发布日期
                date = entry.find('{http://www.w3.org/2005/Atom}updated').text if entry.find('{http://www.w3.org/2005/Atom}updated') is not None else 'No date'
 
                # 保存新闻信息
                news_list.append({'title': title, 'link': link, 'date': date})
 
        else:
            print(f"请求失败,状态码: {response.status_code}")
     
    except requests.RequestException as e:
        print(f"请求错误: {e}")
     
    # 将列表转换为JSON格式的字符串,确保字段值用双引号括起来
    return json.dumps(news_list, ensure_ascii=False)


添加函数组装HTML模板:

from datetime import datetime
import json
 
def generate_news_panel(news_list_str, n):
    """
    生成新闻面板HTML页面
     
    参数:
    news_list_str (str): 包含新闻字典的JSON字符串,每个字典应包含title、link、date字段
    n (int): 需要展示的新闻数量
     
    返回:
    str: 完整的HTML内容字符串
    """
    # 处理数据限制
    news_list = json.loads(news_list_str)
    display_news = news_list[:n]
     
    # 处理日期格式和数据结构
    processed_news = []
    for item in display_news:
        try:
            # 解析原始日期格式(ISO 8601)
            dt = datetime.strptime(item['date'], "%Y-%m-%dT%H:%M:%S.%fZ")
            # 格式化为中文日期(示例:2024年3月15日 15:30)
            formatted_date = dt.strftime("%Y年%m月%d日 %H:%M")
        except:
            formatted_date = "日期未知"
         
        processed_news.append({
            "title": item["title"],
            "link": item["link"],
            "date": formatted_date
        })
     
    # 生成HTML模板
    news_items_html = "".join([f"""
                <li class="news-item">
                    <a href="{item['link']}" target="_blank" class="news-title" rel="noopener noreferrer">
                        {item['title']}
                    </a>
                    <span class="news-date">{item['date']}</span>
                </li>
            """ for item in processed_news])
 
    html_template = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MaxKB 最新动态</title>
    <style>
        /* 基础样式 */
        * {{
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }}
 
        body {{
            font-family: -apple-system, BlinkMacSystemFont,
                        'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell,
                        'Microsoft YaHei', sans-serif;
            line-height: 1.6;
            background-color: #f5f7fa;
            padding: 2rem;
        }}
 
        .news-container {{
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            overflow: hidden;
        }}
 
        .news-header {{
            padding: 1.5rem;
            background: #2c3e50;
            color: white;
        }}
 
        .news-header h1 {{
            font-size: 1.5rem;
            font-weight: 600;
        }}
 
        .news-list {{
            list-style: none;
            padding: 1.5rem;
        }}
 
        .news-item {{
            padding: 1rem;
            margin-bottom: 1rem;
            border-radius: 8px;
            transition: all 0.2s ease;
            border: 1px solid #eee;
        }}
 
        .news-item:hover {{
            transform: translateY(-2px);
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
            border-color: #3498db;
        }}
 
        .news-title {{
            color: #2c3e50;
            text-decoration: none;
            font-weight: 500;
            display: block;
            margin-bottom: 0.5rem;
            font-size: 1.1rem;
        }}
 
        .news-title:hover {{
            color: #3498db;
        }}
 
        .news-date {{
            color: #7f8c8d;
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }}
 
        .news-date::before {{
            content: "📅";
            font-size: 0.8em;
        }}
 
        @media (max-width: 480px) {{
            body {{
                padding: 1rem;
            }}
             
            .news-container {{
                border-radius: 0;
            }}
        }}
    </style>
</head>
<body>
    <div class="news-container">
        <div class="news-header">
            <h1>最新动态 · 共 {len(processed_news)} 条</h1>
        </div>
        <ul class="news-list">
            {news_items_html}
        </ul>
    </div>
</body>
</html>"""
    return f"<html_rander>\n{html_template}\n</html_rander>"

四、演示效果