0

Linux 下的图床自动化实践

 1 year ago
source link: https://blog.diqigan.cn/posts/picgo-linux-script.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

因为 PicGo 在 Linux 下的 GUI 体验并不是很好,索性自己通过 Shell 脚本和快捷键的方式实现了较好的用户体验,记录下操作流程,希望能帮到各位。

笔者测试的相关软件环境如下:

安装 picgo

  1. 安装 Node.js

    yay -S nodejs
  2. 安装 picgo

    npm install picgo -g

    # or

    yarn global add picgo
  3. 安装 picgo 插件

    这里我使用了两个插件:

    • [可选] picgo-plugin-web-uploader

      用来实现自定义 web 图床,如果不需要自定义 web 图床,可以忽略这个插件。

      picgo install web-uploader
    • picgo-plugin-autocopy

      图片上传完毕后自动拷贝链接到剪切板。

      picgo install autocopy

[可选] 配置 picgo-plugin-web-uploader

修改 picgo 的配置文件 ~/.picgo/config.json,设置自定义 web 图床,具体可以参考 官方文档
这里是一份 美日小站 的自定义图床配置:

"picBed": {
"current": "web-uploader",
"web-uploader": {
"customBody": null,
"customHeader": null,
"url": "https://www.ladydaily.com/tools/upload/${token}",
"paramName": "file",
"jsonPath": "data.o_url"
}
}

插件配置完成之后,可以使用 picgo upload 命令验证配置是否正确。

编写 shell 脚本

  • 借助 xclip 实现了剪切板相关操作,manjaro 下安装 xclip 的命令为 sudo pacman -Sy xclip
  • [可选] 借助 espeak 实现了语音提示功能,manjaro 下安装 xclip 的命令为 yay -S espeak。如果不需要语音提示,删除脚本中的相关代码即可。

我这里把脚本写在了 ~/.script/app/picog-upload.sh 文件中,内容如下:

#!/bin/zsh
source $HOME/.zshrc
picgo_upload_result=`picgo upload`

if [ $? -ne 0 ]; then
# 系统提示图片上传失败并显示失败原因
notify-send -u normal -t 5000 -a picgo 'picture upload failed' $picgo_upload_result
# 语音提示图片上传失败
# espeak 'picture upload failed'
exit
else
# 系统提示图片上传成功
notify-send -u normal -t 5000 -a picgo 'picture puload succees' `xclip -o -selection clipboard`
# 删除 xclip 自动添加的换行符
xclip -o -selection clipboard | sed '/^[[:blank:]]*$/d' | xclip -selection clipboard -r
# 语音提示图片上传成功
# espeak 'picture upload succeed'
fi

给脚本添加可执行权限:

chmod +x ~/.script/app/picgo-upload.sh

现在,可以使用命令 ~/.script/app/picgo-upload.sh 测试脚本是否正常。

配置快捷键

  1. 打开 manjaro 系统设置,选择 “工作区”-“快捷键”-“自定义快捷键”- 点击鼠标右键 - 选择 “新建组”- 创建一个 “custom” 分组:

  2. 鼠标右键 “custom” 分组,选择 “新建”-“全局快捷键”-“命令 / URL:”,创建一个名为 “picgo-upload” 的快捷键:

  3. 点击 “触发器”,设置自己喜欢的快捷键,我这里设置的是 Ctrl+Alt+U

  4. 点击 “操作”,在 “命令 / URL:” 输入框中填写脚本路径 ~/.script/app/picgo-upload.sh
    勾选 “custom” 分组和 “picgo-upload” 快捷键,点击 “应用” 使配置生效:

到这里,快捷键就配置完成了,拷贝一张图片,然后使用按下 Ctrl+ Alt+U 测试快捷键,不出意外的话,图片会上传成功并且在右上角弹窗提示:

同时,图片的 url 会自动复制到你的剪切板中。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK