[解决方案] 几十行就能导出博客园文章的 shell 脚本

几十行就能导出博客园文章的 shell 脚本

CNBLOG_USERCNBLOG_TOKEN 不建议粘贴直接放在脚本中,放在环境变量中执行更安全一些,不容易暴露 token。

#!/bin/bash

sync_cnblog() {
    API_URL="https://i.cnblogs.com/api/posts/list"

    # export CNBLOG_USER=xxx
    # export CNBLOG_TOKEN=yyy

    page=1
    page_size=10
    response=$(curl -s -H "Authorization-Type: PAT" -H "Authorization: Bearer $CNBLOG_TOKEN" "$API_URL?s=$page_size&p=$page")
    total_count=$(echo $response | jq -c ".postsCount")
    echo "[ Start ] Download articles count $total_count"

    download_count=0
    while [ $(((page - 1) * page_size)) -lt $total_count ]; do
        response=$(curl -s -H "Authorization-Type: PAT" -H "Authorization: Bearer $CNBLOG_TOKEN" "$API_URL?s=$page_size&p=$page")
        while read -r item; do
            title=$(echo "$item" | jq -r '.title')
            url=$(echo "$item" | jq -r '.url')
            date_published=$(echo "$item" | jq -r '.datePublished')
            is_markdown=$(echo "$item" | jq -r '.isMarkdown')

            if [ "$is_markdown" == "true" ]; then
                # Create filename, replace special char to '_', and add ".md" suffix
                # file_name="${date_published}_${title//[\/\\:*?\"<>|() ]/_}.md"
                file_name="${date_published}_${title}.md"

                # Add '.md' if no suffix
                # Replace '.html' to '.md'
                if [[ "$url" == *".html" ]]; then
                    url="${url%.html}.md"
                else
                    url="${url}.md"
                fi

                # Download
                echo "[ Download ]" "https:$url -> $file_name"
                curl -H "Authorization-Type: PAT" -H "Authorization: Bearer $CNBLOG_TOKEN" -so "$file_name" "https:$url"

                ((download_count++)) || true # set -e
            else
                echo "[ Skip ] $date_published $title"
            fi
        done < <(echo "$response" | jq -c '.postList[]')

        ((page++))
    done
    echo "[ End ] Download articles count $download_count"
}

sync_cnblog

posted on 2024-10-28 11:32  文一路挖坑侠  阅读(9)  评论(0编辑  收藏  举报

导航