-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyy
66 lines (46 loc) · 1.98 KB
/
yy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 一言
# 返回普通文本
#`curl -ks https://v1.hitokoto.cn/?encode=text`
url='https://v1.hitokoto.cn'
if [ x$1 != x ]; then
url='https://v1.hitokoto.cn?'$1
fi
# echo $url
# 处理 json 数据
json=`curl -ks $url`
date=`date`
# 写入日志
`echo "[" $date "]" $json >> ${local}/yy.log`
# 查看日志写入列表
# cat yy.log | cut -d":" -f 6 | cut -d'"' -f 2
# json=`cat << EOF
# {"id":6472,"uuid":"96ee1e66-508d-422f-acf1-b7de9de596ae","hitokoto":"I'm nothing without you.","type":"h","from":"The Flash","from_who":"Clifford Devo","creator":"流幻的光","creator_uid":7468,"reviewer":6844,"commit_from":"web","created_at":"1602837770","length":24}
# EOF`
# linux 安裝jq
# yum install epel-release
# yum list jq
# yum install jq
# window 安裝jq
# https://stedolan.github.io/jq/
# content=${json} | jq '.hitokoto' | sed 's/"//g' | tr -d '\n'
# 失败原因:管道无法直接赋值给变量
# 在 Shell 中使用 jq 解析 JSON 格式文本,通过管道读取出 value 后无法存储为 Shell 中的值
# 只需要将读取出的 value 通过 echo 打印出来,再利用``将值写入到变量中即可
random=$[RANDOM%7+31]
# color="\033[1;${random}m%s\033[0m"
echo -e
content=`echo ${json} | jq '.hitokoto' | sed 's/"//g' | tr -d '\n'`
printf "\033[1;${random}m%s\033[0m" "『 " $content " 』"
from_who=`echo ${json} | jq '.from_who' | sed 's/"//g' | tr -d '\n'`
echo -e
if [ "$from_who" == 'null' ]
then
from_who='匿名'
fi
length=`expr ${#content} \* 2`
printf "\033[1;${random}m%${length}s\033[0m" "—— $from_who"
from=`echo ${json} | jq '.from' | sed 's/"//g' | tr -d '\n'`
printf "\033[1;${random}m%s\033[0m" "「" $from "」"
echo -e
# {"id":6175,"uuid":"db2c4ea6-ce7d-4637-8aa8-456f2e4ec874","hitokoto":"知识有两种,一种是你知道的,一种是你知道在哪里能找到的!","type":"k","from":"塞缪尔·约翰逊","from_who":null,"creator":"Mr96","creator_uid":4362,"reviewer":1044,"commit_from":"web","created_at":"1589939352","length":28}
exit 0