最近把博客重新捡起来了,但是发现在Typora写完博客之后,上传到typecho时还要把文件单独传一遍,于是写了一个小脚本,可以把本地的文件同步到腾讯云的COS里。
#!/bin/bash
# 百度翻译API,需要自己申请appid和apikey
appid=
apikey=
salt=1435660288
# coscmd的路径
cmd="/usr/local/bin/coscmd"
# 存放图片的目录
cos_base_path="blog/article_img/"
# cos的加速域名 /结尾
base_url="https://example.com/"
q=${1// /}
sign=$(echo -n "${appid}${q}${salt}${apikey}" | md5)
baidu_slug="https://api.fanyi.baidu.com/api/trans/vip/translate?q=${q}&from=zh&to=en&appid=${appid}&salt=${salt}&sign=${sign}"
result=$(curl "${baidu_slug}" | sed -n 's/.*"dst":"\([^"]*\)".*/\1/p')
dir=$(echo "${result}" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
declare -a filenames
for i in "$@"; do
if [ "${1}"x != "${i}"x ]; then
$cmd upload "$i" "${cos_base_path}${dir}/"
if [ $? -eq 0 ]; then
if [[ ${2} =~ /([^/]+)\.([^/]+)$ ]]; then
filenames+=("${base_url}${cos_base_path}${dir}/${BASH_REMATCH[1]}.${BASH_REMATCH[2]}")
fi
fi
fi
done
echo "Upload Success:"
for name in "${filenames[@]}"; do
echo $name
done
将代码放入sh里,然后在typora中配置自定义上传命令,例如:
~/typora-upload.sh "${filename}"
这里要注意一下,${filename}一定要带引号,防止文件名有空格导致脚本g掉
小米8 Explore Edition (透明色)
!/bin/bash
百度翻译API,需要自己申请appid和apikey
appid=
apikey=
salt=1435660288
coscmd的路径
cmd="/usr/local/bin/coscmd"
存放图片的目录
cos_base_path="blog/article_img/"
cos的加速域名 /结尾
base_url="https://example.com/"
q=${1// /}
sign=$(echo -n "${appid}${q}${salt}${apikey}" | md5)
baidu_slug="https://api.fanyi.baidu.com/api/trans/vip/translate?q=${q}&from=zh&to=en&appid=${appid}&salt=${salt}&sign=${sign}"
result=$(curl "${baidu_slug}" | sed -n 's/."dst":"([^"])".*/\1/p')
dir=$(echo "${result}" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
declare -a filenames
for i in "$@"; do
if [ "${1}"x != "${i}"x ]; then
$cmd upload "$i" "${cos_base_path}${dir}/"
if [ $? -eq 0 ]; then
if [[ ${2} =~ /([^/]+).([^/]+)$ ]]; then
filenames+=("${base_url}${cos_base_path}${dir}/${BASH_REMATCH[1]}.${BASH_REMATCH[2]}")
fi
fi
fi
done
echo "Upload Success:"
for name in "${filenames[@]}"; do
echo $name
done
把这个shell改成golang语言
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
const (
// 百度翻译API,需要自己申请appid和apikey
appID = ""
apiKey = ""
baseURL = "https://api.fanyi.baidu.com/api/trans/vip/translate"
// coscmd的路径
coscmdPath = "/usr/local/bin/coscmd"
// 存放图片的目录
cosBasePath = "blog/article_img/"
// cos的加速域名 /结尾
baseURLPrefix = "https://example.com/"
)
func main() {
if len(os.Args) <= 1 {
fmt.Println("Please provide the text to translate.")
return
}
text := strings.TrimSpace(os.Args[1])
text = strings.ReplaceAll(text, " ", "")
salt := fmt.Sprintf("%d", time.Now().Unix())
sign := md5.Sum([]byte(appID + text + salt + apiKey))
query := fmt.Sprintf("%s?q=%s&from=zh&to=en&appid=%s&salt=%s&sign=%s",
baseURL, text, appID, salt, hex.EncodeToString(sign[:]))
resp, err := http.Get(query)
if err != nil {
fmt.Printf("Failed to get translation for %s: %v", text, err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response body for %s: %v", text, err)
return
}
var dst string
fmt.Sscanf(string(body), `{"error_code":0,"error_msg":"SUCCESS","from":"zh","to":"en","trans_result":[{"src":"%s","dst":"%s"}]}`,
&text, &dst)
dir := strings.ToLower(strings.ReplaceAll(dst, " ", "-"))
var filenames []string
for _, arg := range os.Args[1:] {
if arg == os.Args[1] {
continue
}
absPath, err := filepath.Abs(arg)
if err != nil {
fmt.Printf("Failed to get absolute path for %s: %v", arg, err)
continue
}
if err := uploadToCos(absPath, filepath.Join(cosBasePath, dir)); err != nil {
fmt.Printf("Failed to upload %s: %v\n", arg, err)
continue
}
ext := filepath.Ext(arg)
filename := fmt.Sprintf("%s%s%s", baseURLPrefix, cosBasePath, filepath.Join(dir, strings.TrimSuffix(filepath.Base(absPath), ext)+ext))
filenames = append(filenames, filename)
}
fmt.Println("Upload Success:")
for _, name := range filenames {
fmt.Println(name)
}
}
func uploadToCos(localPath string, remoteDir string) error {
cmd := fmt.Sprintf("%s upload %s %s --force", coscmdPath, localPath, remoteDir)
if output, err := execShellCommand(cmd); err != nil {
return fmt.Errorf("%s: %v", output, err)
}
return nil
}
func execShellCommand(cmd string) (string, error) {
resultBytes, err := execShellCommandBytes(cmd)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resultBytes)), nil
}
func execShellCommandBytes(cmd string) ([]byte, error) {
command
@ChatGPT
!/bin/bash
百度翻译API,需要自己申请appid和apikey
appid=
apikey=
salt=1435660288
coscmd的路径
cmd="/usr/local/bin/coscmd"
存放图片的目录
cos_base_path="blog/article_img/"
cos的加速域名 /结尾
base_url="https://example.com/"
q=${1// /}
sign=$(echo -n "${appid}${q}${salt}${apikey}" | md5)
baidu_slug="https://api.fanyi.baidu.com/api/trans/vip/translate?q=${q}&from=zh&to=en&appid=${appid}&salt=${salt}&sign=${sign}"
result=$(curl "${baidu_slug}" | sed -n 's/."dst":"([^"])".*/\1/p')
dir=$(echo "${result}" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
declare -a filenames
for i in "$@"; do
if [ "${1}"x != "${i}"x ]; then
$cmd upload "$i" "${cos_base_path}${dir}/"
if [ $? -eq 0 ]; then
if [[ ${2} =~ /([^/]+).([^/]+)$ ]]; then
filenames+=("${base_url}${cos_base_path}${dir}/${BASH_REMATCH[1]}.${BASH_REMATCH[2]}")
fi
fi
fi
done
echo "Upload Success:"
for name in "${filenames[@]}"; do
echo $name
done
把这个shell改成golang语言