Posts tagged with “闲言碎语”

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}"

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

我的开心网记录-2013

2013-01-10 15:09 我想大家都遭遇过各种手机App闪退的情况…… 我想不通的是,是现在的程序都流行不处理异常么?抽时间俺实践下。:)


2013-01-10 11:07 分享微博 //@firstduty:真正的职场高手,给人的第一印象绝不是“谁的人”,而是“踏实肯干”,这种人不管哪个老总都喜欢,也都需要。


2013-01-10 09:10 分享:天文望远镜最全的总结攻略!|星空之夜…全文03 附链接 http://user.qzone.qq.com/731783630/blog/1298878399#!app=2&via=QZ.HashRefresh&pos=1298878399


2013-01-08 12:49 Hello, fcitx。 再见,ibus。


2013-01-08 10:40 淡定是一种境界。


2013-01-08 09:18 真心觉得苹果把鼠标滚轮默认设成和人们的习惯相反脑残。肯定有个地方能把这个设置改回来。


2013-01-08 00:02 分享微博 //@shukebeta:转贴:他86岁重新选经济学的时候,他并不知道自己将来有一天在经济学上的成就比之前86年的成就还要大。他不可能知道这件事情的,他只是好奇,想学就学了,之后他做出了这样一个成绩, ... 全文03附链接 https://www.xnw.com/blog/156987


…more

我的开心网记录-2012

2012-12-31 21:48 过去的这一年,我一天也没有虚度。 有一点小小的自豪。 2012,再见啦。


2012-12-31 08:43 我对小帅说:“今儿是2012年的最后一天了。那明天是什么日子?”小帅说:“明天就是2013年1月1日了,今天是12月31日,有31天。1,3,5,7,8,10,12月都是31天。”“谁教你的?”“我们在幼儿园学 ... 全文03附链接 https://www.xnw.com/blog/147649


2012-12-31 08:07 2012的最后一天。宁静的早晨。忙碌的我。


2012-12-30 22:23 石景山//原文03 附链接 https://www.xnw.com/blog/147619 有图

照片 http://pic1.kaixin001.com.cn/pic/app/51/0/1018_177510045_records-b**rotate4.jpeg


2012-12-30 22:17 晴朗的冬日,永定河//原文03 附链接 https://www.xnw.com/blog/147616 有图

照片 http://pic1.kaixin001.com.cn/pic/app/50/96/1018_177509699_records-b**rotate4.jpeg


2012-12-30 21:16 我问小帅台湾的首都是哪里?小帅说台湾没有首都,它和中国是一块儿的。注:[狼奶]


2012-12-30 21:15 人们只有在做自己想做的事情时才会才华四溢。

…more

我的开心网记录-2011

2011-12-31 20:52 读完了《一路走来一路读》。


2011-12-31 19:33 上午一直在读《一路走来一路读》,下午整理自己过去一年发表的微博。几个小时下来,也才整理到3月份。2011年是整理不完了,就先到这里吧。昨天上线的统计程序,早上发现有个地方还可改进,趁2012还在路上,这就去把它改完。


2011-12-31 19:30 标题:2011读书译书不完全记录. 翻译及审稿 审校《Java script DOM编程艺术》第二版译稿,我的名字第一次以印刷体出现在书的封面上。 翻译并出版了《深入PHP与jQuery编程》 翻译《The Smashing jQuery》60% 阅读 一月: 《晨间日记的奇迹》 《简约至//全文>>


2011-12-31 19:26 标题:我的微博自选集(2011年3月). "明哲保身", "事不关己,高高挂起", "各人自扫门前雪,谁管他人瓦上霜" 这些思想害了中国5000年, 到今天仍然有很大的市场. 请大家记住, 只求自保, 一定保不住自己. 普及公民教育, 唤醒公民意识, 刻不容缓. 3月1日13:23 来自新浪//全文>>


2011-12-31 18:50 把电视关了。受不了元旦晚会的弱智节目。


…more

我的开心网记录-2010

2010-12-31 19:45 360这下祸可闯大了,要知道, google 是有快照可以用的哦!快联系 google 删记录吧


2010-12-31 08:37 来自陈文龙先生的邮件,谢谢! Life can be charming, If you have a dream. Life can be sad, When New Year comes without a dream. Dream defines who you are, So try to follow your dream.


2010-12-31 07:33 2010的最后一天。这一年里读到了好几本指导如何掌控生活的特别好的书,认识了一堆新朋友,这要感谢新浪微博。别了,2010!


2010-12-30 07:30 刚刚读完《小强升职记》,我决定尽快买一本纸版。


2010-12-29 12:47 养成一个好习惯很难,养成坏习惯就极其容易。


2010-12-29 12:43 人生就象是在雪地里行走,向后看,是自己的足迹,向前看,白茫茫一片。不要问“该往哪走?“ 只要答“想往哪走“


…more