ChatGPT 帮我发布书摘

我更喜欢用 多看阅读 来读电子书。一方面阅读体验好,另一方面它支持扫描pdf重排。另外,多看的书摘格式即使导出到Email也挺赏心悦目的。许是已经年老的缘故,我最近在收集一些旧文,以及把我历年的读书书摘集中发布在我的blog上。我的blog的编辑器是一个比较简陋的 markdown编辑器,为了确保发布后能有相对美观的格式,我总要手工编辑一下,怪累的。

今天闲来无事,复制粘贴手工发布了几篇书摘后我想,哎,何不让chatGPT帮我写个脚本来简化我的工作。说干就干,于是花了一两个小时,让chatGPT给我写了以下两个脚本:

第一个,负责整理书摘的格式,我给取了个名字就叫 shuzhai

#!/bin/bash

# Check if filename is provided as parameter
if [ $# -ne 1 ]; then
  echo "Usage: $0 filename"
  exit 1
fi

filename="$1"

sed -i '/^$/d' "$filename"
# Add "书摘:" prefix to the first line
sed -i '1s/^/书摘:/' "$filename"
# Add "作者:" prefix to the second line
sed -i '2s/^/作者:/' "$filename"

# Insert an extra empty line before the last non-empty line of the file
sed -i -e ':a' -e '$!{N;ba}' -e 's/\(\S.*\)\n/\1\n\n/' "$filename"

# Run the first sed command to add an extra empty line before the date time
sed -i 's/\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}/\n&/' "$filename"

# Run the second sed command to add two spaces at the end of each non-empty line
sed -i '/^$/!s/$/  /' "$filename"

第二个脚本,我叫它 publish_shuzhai_to_blog, 它调用前面的 shuzhai脚本,并自动将书摘内容发到我的blog站点。

#!/bin/bash

# Check if filename is provided as parameter
if [ $# -ne 1 ]; then
  echo "Usage: $0 filename"
  exit 1
fi

filename="$1"

if [ ! -f "$filename" ]; then
  echo "File not found: $filename"
  exit 1
fi

if ! [ -r "$filename" ]; then
  echo "File not readable: $filename"
  exit 1
fi

# Check that the file is a text file
if ! file "$filename" | grep -q "text"; then
  echo "Not a text file: $filename"
  exit 1
fi

# Create the logs directory if it doesn't exist
LOG_DIR=~/logs
if [ ! -d "$LOG_DIR" ]; then
  mkdir "$LOG_DIR"
fi

# Create an empty log file if it doesn't exist
LOG_FILE="$LOG_DIR/$(basename "$0").log"
if [ ! -f "$LOG_FILE" ]; then
  touch "$LOG_FILE"
fi

# read the text file and extract the first line as the POST_TITLE
POST_TITLE=$(head -n 1 $1)

# Check if the new file's title is the same as the last processed title
if [ -s "$LOG_FILE" ]; then
  LAST_TITLE=$(tail -n 1 "$LOG_FILE")
  if [ "$LAST_TITLE" = "$POST_TITLE" ]; then
    echo "Error: Duplicate title detected: $POST_TITLE"
    exit 1
  fi
fi

# format the file first
~/bin/shuzhai "$filename"

# re-read the first line as title, as we add "书摘:" as a prefix to the title
POST_TITLE=$(head -n 1 $1)

# Log the new title
echo "$POST_TITLE" >> "$LOG_FILE"

# set the URL of the blog site
BLOG_URL='https://blog.shukebeta.com/admin/add_post/'

# read the rest as the POST_BODY
POST_BODY=$(tail -n +2 $filename)

# build the data for the curl request
DATA=$(cat <<EOF
------WebKitFormBoundaryEznVwciBsbYrRkwB
Content-Disposition: form-data; name="title"

${POST_TITLE}
------WebKitFormBoundaryEznVwciBsbYrRkwB
Content-Disposition: form-data; name="body"

${POST_BODY}
------WebKitFormBoundaryEznVwciBsbYrRkwB
Content-Disposition: form-data; name="status"

public
------WebKitFormBoundaryEznVwciBsbYrRkwB
Content-Disposition: form-data; name="slug"


------WebKitFormBoundaryEznVwciBsbYrRkwB
Content-Disposition: form-data; name="created_at"

$(date +"%Y-%m-%d %H:%M:%S")
------WebKitFormBoundaryEznVwciBsbYrRkwB
Content-Disposition: form-data; name="option[comment_status]"

open
------WebKitFormBoundaryEznVwciBsbYrRkwB
Content-Disposition: form-data; name="tags"

中文, 书摘
------WebKitFormBoundaryEznVwciBsbYrRkwB
Content-Disposition: form-data; name="option[category_id]"

3
------WebKitFormBoundaryEznVwciBsbYrRkwB
Content-Disposition: form-data; name="feather"

text
------WebKitFormBoundaryEznVwciBsbYrRkwB
Content-Disposition: form-data; name="hash"

28ccc1f365e2781c9b0877badb3349cc5ce9945a
------WebKitFormBoundaryEznVwciBsbYrRkwB--
EOF
)

# send the curl request
curl -X POST "${BLOG_URL}" \
     -H 'origin: https://blog.shukebeta.com' \
     -H 'referer: https://blog.shukebeta.com/admin/' \
     -H 'cookie: there is only a place-holder, hahahaha' \
     -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryEznVwciBsbYrRkwB" \
     -H "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" \
     --data "${DATA}"

这两个脚本要是我自己来写,怕是要花一整天。感叹!