shell脚本定时给手机发送天气预报短信


先上效果图:

img

大概需要准备的:

1、一个公网服务器 或者本地服务器也可以,能通互联网。

2、一个天气API接口

3、一个发短信的API接口

处理逻辑:

1、shell curl 天气API接口,获取结果;

2、解析结果(unicode 转码需要用到native2ascii JDK里面的);

3、组装发短信的报文;

4、调用发短信的API发送报文。

5、可以视情况编写定时任务,crontab。

附代码:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
. ~/.bash_profile
. /etc/profile
cd /home/CYT/
pwd

curl -s "http://api.yytianqi.com/observe?city=37.73565,112.56878&key=你的天气token" | native2ascii -encoding UTF-8 -reverse > tianqishikaung.txt

#sleep 5s

#天气实况
# 城市名称
cityName=`cat tianqishikaung.txt | sed 's/,/\n/g' | grep -w 'cityName'| sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}'`
# 天气现象
tq=`cat tianqishikaung.txt | sed 's/,/\n/g' | grep -w 'tq' | sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}' `
#当前风向
fx=`cat tianqishikaung.txt | sed 's/,/\n/g' | grep -w 'fx' | sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}' `
# 当前风力
fl=`cat tianqishikaung.txt | sed 's/,/\n/g' | grep -w 'fl' | sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}' `
#相对湿度
sd=`cat tianqishikaung.txt | sed 's/,/\n/g' | grep -w 'sd' | sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}'|sed 's/}}/%/g' `
#当前气温
qw=`cat tianqishikaung.txt | sed 's/,/\n/g' | grep -w 'qw' | sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}' `


DQTQ=`echo "$cityName" "$tq,$fx" "$fl,当前气温为" "$qw℃ ,相对湿度为 " "$sd"。`

echo ${DQTQ}


curl -s "http://api.yytianqi.com/forecast7d?city=37.73565,112.56878&key=你的天气token" | native2ascii -encoding UTF-8 -reverse >tianqiyubao.txt
#sleep 5s

date1=`cat tianqiyubao.txt | sed 's/,/\n/g'|grep -w date|head -2|awk 'NR==2'|sed 's/"//g'|sed 's/:/ /g'|awk '{print$2}'|sed 's/}//g'`

#白天天气
tq1=`cat tianqiyubao.txt | sed 's/,/\n/g'|grep -w tq1|head -2|awk 'NR==2'|sed 's/"//g'|sed 's/:/ /g'|awk '{print$2}'|sed 's/}//g'`
#白天气温
qw1=`cat tianqiyubao.txt | sed 's/,/\n/g'|grep -w qw1|head -2|awk 'NR==2'|sed 's/"//g'|sed 's/:/ /g'|awk '{print$2}'|sed 's/}//g'`
#白天风向
fx1=`cat tianqiyubao.txt | sed 's/,/\n/g'|grep -w fx1|head -2|awk 'NR==2'|sed 's/"//g'|sed 's/:/ /g'|awk '{print$2}'|sed 's/}//g'`
#白天风力
fl1=`cat tianqiyubao.txt | sed 's/,/\n/g'|grep -w fl1|head -2|awk 'NR==2'|sed 's/"//g'|sed 's/:/ /g'|awk '{print$2}'|sed 's/}//g'`

#夜间天气
#tq2=`cat tianqiyubao.txt | sed 's/,/\n/g'|grep -w tq2|head -2|awk 'NR==2'|sed 's/"//g'|sed 's/:/ /g'|awk '{print$3}'`
cat tianqiyubao.txt | sed 's/,/\n/g'|grep -w tq2|awk 'NR==2'|sed 's/"/ /g'|sed 's/{//g'|sed 's/://g'|awk '{print$2}'
#夜间气温
qw2=`cat tianqiyubao.txt | sed 's/,/\n/g'|grep -w qw2|head -2|awk 'NR==2'|sed 's/"//g'|sed 's/:/ /g'|awk '{print$2}'|sed 's/}//g'`
#夜间风向
fx2=`cat tianqiyubao.txt | sed 's/,/\n/g'|grep -w fx2|head -2|awk 'NR==2'|sed 's/"//g'|sed 's/:/ /g'|awk '{print$2}'|sed 's/}//g'`
#夜间风力
fl2=`cat tianqiyubao.txt | sed 's/,/\n/g'|grep -w fl2|head -2|awk 'NR==2'|sed 's/"//g'|sed 's/:/ /g'|awk '{print$2}'|sed 's/}//g'`

MTTQ=`echo "明日$date1 白天" "$tq1 ,气温为" "$qw1℃ ,$fx1$fl1,夜间" "$tq2 ,气温为" "$qw2℃ ,$fx2$fl2"。""`

echo ${MTTQ}

#天气指数
curl -X GET 'http://api.yytianqi.com/weatherindex?city=37.73565,112.56878&key=你的天气token' | native2ascii -encoding UTF-8 -reverse >tianqizhishu.txt
#sleep 5s
#穿衣指数
cyzh=`cat tianqizhishu.txt | sed 's/,/\n/g'|sed 's/"//g'|sed 's/://g'|grep 穿|grep longDesc|sed 's/longDesc//g'|sed 's/}//g'`

echo $MTTQ



echo "'"${DQTQ} ${cyzh} ${MTTQ}"'"



mobile="你的手机号"

curl 'http://1111.cn/HXXX/XXX_XXX/XXX0001' \
-H 'content-type: application/json' \
-d '{"txnBodyCom": {"MSG_SMS_MBLPH_NO": "'"${mobile}"'","MSG_SMS_CNTNT": "'" ${DQTQ}${cyzh}\n${MTTQ} "'"}}'



评论
  目录