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

先上效果图:

大概需要准备的:

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

2、一个天气API接口

3、一个发短信的API接口


处理逻辑:

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

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

3、组装发短信的报文;

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

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


附代码:

#!/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/,/
/g' | grep -w 'cityName'| sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}'`	
# 天气现象
tq=`cat tianqishikaung.txt  | sed 's/,/
/g' | grep -w 'tq' | sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}'	`
#当前风向
fx=`cat tianqishikaung.txt  | sed 's/,/
/g' | grep -w 'fx' | sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}' `
# 当前风力
fl=`cat tianqishikaung.txt  | sed 's/,/
/g' | grep -w 'fl' | sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}'   `
#相对湿度
sd=`cat tianqishikaung.txt  | sed 's/,/
/g' | grep -w 'sd' | sed 's/"//g'|sed 's/:/ /g'|awk '{printf$2}'|sed  's/}}/%/g' `
#当前气温
qw=`cat tianqishikaung.txt  | sed 's/,/
/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/,/
/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/,/
/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/,/
/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/,/
/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/,/
/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/,/
/g'|grep -w tq2|head -2|awk 'NR==2'|sed 's/"//g'|sed 's/:/ /g'|awk '{print$3}'`
cat tianqiyubao.txt | sed 's/,/
/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/,/
/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/,/
/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/,/
/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/,/
/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}
${MTTQ} "'"}}'
展开阅读全文

页面更新:2024-03-06

标签:公网   报文   相对湿度   风力   风向   夜间   气温   脚本   天气预报   白天   接口   天气   短信   手机

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top