天气预报+老婆私人订制服务
服務(wù)器免費但是有期限,騰訊云現(xiàn)在免費3個月對新用戶
我在其他地方也發(fā)表了這個文章供大家參考。
每天一份天氣預(yù)報,加上老婆喜歡的宋詞,花語,唐詩都可以自己選擇,
開發(fā)簡單同時還能聯(lián)系下"騰訊云serverless函數(shù)"的使用。
1.Server醬??,關(guān)注公眾號每天給你提醒的路徑,親測安全。注冊PC路徑:https://sct.ftqq.com/login
? ? 按照提示手機(jī)關(guān)注下公眾號,記錄下頁面顯示的ID。后期“SendKey"需要使用。
2.和風(fēng)天氣API,需要微信和郵箱激活下就可以使用??,注冊PC路徑:? ??https://console.qweather.com/#/console
? ? 按照提示激活獲取ID,記錄下頁面顯示的ID。后期“Weather_key"需要使用。
3.天行數(shù)據(jù),使用郵箱注冊激活就好,這里面的數(shù)據(jù)很豐富,宋詞、詩歌、花語等等太多了你自己選擇,注冊PC路徑???https://www.tianapi.com/console/
? ? 按照提示注冊獲取apiID,記錄下頁面顯示的ID。后期“ApiKey"需要使用。?需要提醒下不同數(shù)據(jù)個別需要使用地址或者花名,自己先測試下額外參數(shù)。
4.騰訊云serverless注冊,這個是服務(wù)器函數(shù)使用地址,可以免費3個月哦,續(xù)費也很便宜,注冊PC地址:https://console.cloud.tencent.com/
1)函數(shù)服務(wù)—新建
2)新建—從頭開始—事件函數(shù)—運行環(huán)境python3.7
3)函數(shù)代碼—在線編輯
import requests
import datetime
import time
import json
import pytz
import ssl,hashlib,string
from urllib import request,parse,error;
from urllib.parse import quote
SendKey = 'SCT176676Td47137409817519470198'? ?
Weather_key = '4482782952850942385-85-2945-28'
Tianapi_key = ''
Storeapi_key = ''
ApiKey = '9b5f549279023707547250752479'
# 接收消息的用戶,存在多個以 | 隔開
userid = ''
adminUserId = '';
def get_time():
? ? nowTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime());??# 獲取當(dāng)前時間
? ? # 由于騰訊云時間不準(zhǔn)確,暫時使用年月日
? ? nowTime_1 = time.strftime("%Y-%m-%d", time.localtime());??# 獲取當(dāng)前時間
? ? # print(nowTime_1)
? ? return nowTime_1;
def new_songci():
? ? url="http://api.tianapi.com/zmsc/index?key="+ApiKey;
? ? new_songci_json = requests.get(url).json()
? ? new_songci_code = new_songci_json["code"]
? ? new_songci_newslist = new_songci_json["newslist"]
? ? if(new_songci_code.__eq__(200)):
? ?? ???return new_songci_newslist[0]["content"]+"\n\n作者:"+new_songci_newslist[0]["source"]
#獲取當(dāng)天天氣,不過沒有最高溫度,最低溫度
def get_Weather():
? ? city_id = get_location();
? ? Weather_url = f'https://devapi.qweather.com/v7/weather/now?key={Weather_key}&location={city_id}';
? ? Weather_json = requests.get(Weather_url).json()
? ? #print(Weather_url)
? ? Weather_code = Weather_json["code"]
? ? Weather_now = Weather_json["now"]
? ? if(Weather_code.__eq__("200")):
? ?? ?#print(Weather_now)
? ?? ?return Weather_now;
#獲取包括今天在內(nèi)的未來三天天氣
def get_3d_Weather(num):
? ? date = get_time();
? ? date_tomorrow = (datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d");
? ? city_id = get_location();
? ? Weather_url = f'https://devapi.qweather.com/v7/weather/3d?key={Weather_key}&location={city_id}';
? ? #print(Weather_url)
? ? Weather_json = requests.get(Weather_url).json();
? ? Weather_3d = Weather_json["daily"]
? ? for weather in Weather_3d:
? ?? ???#print("6666666"+weather)
? ?? ???if(num.__eq__(1)):
? ?? ?? ?? ?if(weather["fxDate"].__eq__(date)):
? ?? ?? ?? ?? ? #print("當(dāng)天天氣獲取成功")
? ?? ?? ?? ?? ? return weather;
? ?? ?? ?? ?else:
? ?? ?? ?? ?? ? print("查詢的不是今天日期的天氣")
? ?? ?? ?? ?? ? return "查詢的不是今天日期的天氣"
? ?? ?? ?? ?? ? break;
? ?? ???elif(num.__eq__(0)):
? ?? ?? ?? ?if (weather["fxDate"].__eq__(date_tomorrow)):
? ?? ?? ?? ?? ? #print(weather);
? ?? ?? ?? ?? ? return weather;
? ?? ???else:
? ?? ?? ?? ?print("非法參數(shù)!")
? ?? ?? ?? ?return "非法參數(shù)!"
#獲取當(dāng)前天氣地區(qū)id
def get_location():
? ? # 調(diào)整所在的區(qū)域
? ? location='沈陽'
? ? Weather_city_url = f'https://geoapi.qweather.com/v2/city/lookup?key={Weather_key}&location={location}'
? ? city_json =requests.get(Weather_city_url).json();
? ? city_code = city_json["code"]
? ? if city_code.__eq__("200"):
? ???#print(Weather_city_url)
? ???for Weather_city in city_json["location"]:
? ?? ?? ?Weather_city_name = Weather_city["name"]
? ?? ?? ?if Weather_city_name.__eq__("沈陽"):
? ?? ?? ?? ? #print(Weather_city)
? ?? ?? ?? ? city_id = Weather_city["id"]
? ?? ?? ?? ? #print(city_id)
? ?? ?? ?? ? return city_id;
? ? else:
? ?? ?print("訪問獲取地區(qū)接口失敗!")
? ?? ?return "訪問獲取地區(qū)接口失敗!";
#拼接消息
def get_desp():
? ? #wyy_content = get_wyy()[0];??# 評論內(nèi)容
? ? #wyy_source = get_wyy()[1];??# 歌名
? ? date_tomorrow = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d");
? ? Weather = get_Weather() #獲取當(dāng)天天氣信息
? ? Weather_text = Weather["text"] #獲取當(dāng)天天氣文字
? ? Weather_temp = Weather["temp"] #實時獲取當(dāng)前溫度
? ? Weather_feelsLike = Weather["feelsLike"] #獲取當(dāng)前體感溫度
? ? Weather_3d_day = get_3d_Weather(1)??# 獲取當(dāng)天的預(yù)報天氣
? ? Weather_3d_day_tempMax = Weather_3d_day["tempMax"] #獲取當(dāng)天預(yù)報最高溫度
? ? Weather_3d_day_tempMin = Weather_3d_day["tempMin"] #獲取當(dāng)天預(yù)報最低溫度
? ? Weather_3d_day_uvIndex = Weather_3d_day["uvIndex"] #獲取當(dāng)天紫外線強(qiáng)度
? ? Weather_3d_tomorrow = get_3d_Weather(0)??# 獲取明天天氣
? ? Weather_3d_tomorrow_textDay = Weather_3d_day["textDay"]??# 獲取第二天天氣文字
? ? Weather_3d_tomorrow_tempMax = Weather_3d_day["tempMax"]??# 獲取第二天預(yù)報最高溫度
? ? Weather_3d_tomorrow_tempMin = Weather_3d_day["tempMin"]??# 獲取第二天預(yù)報最低溫度
? ? Weather_3d_tomorrow_uvIndex = Weather_3d_day["uvIndex"]??# 獲取第二天紫外線強(qiáng)度
? ? cur_time = time.time()
? ? #print(cur_time)
? ? local_tz = pytz.timezone("Asia/Shanghai")
? ? local_time = datetime.datetime.fromtimestamp(cur_time, tz=local_tz).hour
? ? # print(local_time)
? ? songci=new_songci();#獲得宋詞
? ? desp_Weather_temp_text = "";
? ? desp_Weather_text = "";
? ? desp_Weather_3d_tomorrow_textDay_text = "";
? ? desp_Weather_3d_day_tempMax_text = "";
? ? desp_Weather_3d_tomorrow_tempMax_text = "";
? ? desp_day = "";
? ? desp_tomorrow = "";
? ? desp_Weather_3d_day_uvIndex = "";
? ? # 晚上八點 查詢第二天天氣情況,然后根據(jù)當(dāng)前時間,定制化推送當(dāng)前提醒
? ? #if (local_time >= 20 and local_time <= 23):
? ? if (True):
? ?? ???# 如果有雨,不校驗最高溫度
? ?? ???# 判斷天氣
? ?? ???if ("雨" in Weather_3d_tomorrow_textDay):
? ?? ?? ?? ?if Weather_3d_tomorrow_textDay.__eq__("大雨"):
? ?? ?? ?? ?? ? desp_Weather_3d_tomorrow_tempMax_text = "\n\n";
? ?? ?? ?? ?? ? desp_Weather_3d_tomorrow_textDay_text = "\n\n明天"+ Weather_3d_tomorrow_textDay + ",傻瓜,記得帶傘,記得早點出門,晚上早點睡\n\n"
? ?? ?? ?? ?elif Weather_3d_tomorrow_textDay.__eq__("中雨"):
? ?? ?? ?? ?? ? desp_Weather_3d_tomorrow_tempMax_text = "\n\n";
? ?? ?? ?? ?? ? desp_Weather_3d_tomorrow_textDay_text = "\n\n明天" + Weather_3d_tomorrow_textDay + ",傻瓜,記得帶傘,記得早點出門,晚上早點睡\n\n"
? ?? ?? ?? ?elif Weather_3d_tomorrow_textDay.__eq__("雷陣雨"):
? ?? ?? ?? ?? ? desp_Weather_3d_tomorrow_tempMax_text = "\n\n";
? ?? ?? ?? ?? ? desp_Weather_3d_tomorrow_textDay_text = "\n\n明天" + Weather_3d_tomorrow_textDay + ",小心打雷,抱抱傻瓜,不怕不怕\n\n"
? ?? ?? ?? ?else:
? ?? ?? ?? ?? ? desp_Weather_3d_tomorrow_tempMax_text = "\n\n";
? ?? ?? ?? ?? ? desp_Weather_3d_tomorrow_textDay_text = "\n\n明天" + Weather_3d_tomorrow_textDay + ",記得帶傘,請注意天氣\n\n"
? ?? ???# 沒雨的情況下,根據(jù)溫度提醒明天天氣
? ?? ???else:
? ?? ?? ?? ?desp_Weather_3d_tomorrow_textDay_text = "\n\n";
? ?? ?? ?? ?if (int(Weather_3d_tomorrow_tempMax) >= 33):
? ?? ?? ?? ?? ? desp_Weather_3d_tomorrow_tempMax_text = "\n\n明天天氣較熱,注意防曬!\n\n"
? ?? ?? ?? ?else:
? ?? ?? ?? ?? ? desp_Weather_3d_tomorrow_tempMax_text = "\n\n";
? ?? ???desp_tomorrow = (
? ?? ?? ?? ?? ?? ???"明天 " + date_tomorrow + "\n\n天氣:" + Weather_3d_tomorrow_textDay + desp_Weather_3d_tomorrow_textDay_text +
? ?? ?? ?? ?? ?? ???"明天最高溫度:" + Weather_3d_tomorrow_tempMax + "℃。" + desp_Weather_3d_tomorrow_tempMax_text +
? ?? ?? ?? ?? ?? ???"明天最低溫度:" + Weather_3d_tomorrow_tempMin + "℃。"
? ?? ?? ?? ?? ?? ???);
? ? #elif (local_time >= 0 and local_time < 20):
? ? if (True):
? ?? ???# desp_Weather_3d_tomorrow_textDay_text = "\n\n";
? ?? ???# desp_Weather_3d_tomorrow_tempMax_text = "\n\n";
? ?? ???# 判斷當(dāng)前溫度
? ?? ???if (int(Weather_temp) >= 33):
? ?? ?? ?? ?if (8 < local_time < 16):
? ?? ?? ?? ?? ? desp_Weather_temp_text = ",天氣有點熱喔!\n\n"
? ?? ?? ?? ?else:
? ?? ?? ?? ?? ? desp_Weather_temp_text = "。\n\n";
? ?? ???else:
? ?? ?? ?? ?desp_Weather_temp_text = "。\n\n";
? ?? ???# 判斷紫外線等級
? ?? ???if (8 <= local_time < 16):
? ?? ?? ?? ?if (4 < int(Weather_3d_day_uvIndex) <= 6):
? ?? ?? ?? ?? ? desp_Weather_3d_day_uvIndex = "\n\n今天紫外線等級" + Weather_3d_day_uvIndex + ",適合戶外運動。\n\n"
? ?? ?? ?? ?elif (6 < int(Weather_3d_day_uvIndex) <= 9):
? ?? ?? ?? ?? ? desp_Weather_3d_day_uvIndex = "\n\n今天紫外線等級" + Weather_3d_day_uvIndex + ",請注意防曬。\n\n"
? ?? ?? ?? ?elif (9 < int(Weather_3d_day_uvIndex)):
? ?? ?? ?? ?? ? desp_Weather_3d_day_uvIndex = "\n\n今天紫外線等級" + Weather_3d_day_uvIndex + ",寶,外邊太曬了,能不出去就不要出去了。\n\n"
? ?? ?? ?? ?else:
? ?? ?? ?? ?? ? desp_Weather_3d_day_uvIndex = "\n\n";
? ?? ???else:
? ?? ?? ?? ?desp_Weather_3d_day_uvIndex = "\n\n";
? ?? ?? ?? ?# 判斷當(dāng)天是否有雨,有雨不判斷當(dāng)天最高溫度
? ?? ?? ?? ?Weather_text = "晴"
? ?? ???if ("雨" in Weather_text):
? ?? ?? ?? ?if Weather_text.__eq__("大雨"):
? ?? ?? ?? ?? ? desp_Weather_3d_day_tempMax_text = "\n\n";
? ?? ?? ?? ?? ? desp_Weather_text = "\n\n當(dāng)前有" + Weather_text + ",傻瓜,趕緊避雨,不要再外出啦\n\n"
? ?? ?? ?? ?elif Weather_text.__eq__("中雨"):
? ?? ?? ?? ?? ? desp_Weather_3d_day_tempMax_text = "\n\n"
? ?? ?? ?? ?? ? desp_Weather_text = "\n\n當(dāng)前有" + Weather_text + ",傻瓜,趕緊避雨,不要再外出啦\n\n"
? ?? ?? ?? ?elif Weather_text.__eq__("雷陣雨"):
? ?? ?? ?? ?? ? desp_Weather_3d_day_tempMax_text = "\n\n"
? ?? ?? ?? ?? ? desp_Weather_text = "\n\n當(dāng)前有" + Weather_text + ",小心打雷,抱抱傻瓜,不怕不怕\n\n"
? ?? ?? ?? ?else:
? ?? ?? ?? ?? ? desp_Weather_3d_day_tempMax_text = "\n\n"
? ?? ?? ?? ?? ? desp_Weather_text = "\n\n當(dāng)前有" + Weather_text + ",請注意天氣\n\n"
? ?? ???else:
? ?? ?? ?? ?desp_Weather_text = "\n\n";
? ?? ?? ?? ?# 判斷當(dāng)天最高溫度
? ?? ?? ?? ?if (int(Weather_3d_day_tempMax) >= 33):
? ?? ?? ?? ?? ? # 下午16點以后不用提示
? ?? ?? ?? ?? ? if (local_time > 16):
? ?? ?? ?? ?? ?? ???desp_Weather_3d_day_tempMax_text = "\n\n";
? ?? ?? ?? ?? ? else:
? ?? ?? ?? ?? ?? ???desp_Weather_3d_day_tempMax_text = "\n\n今天天氣較熱,注意防曬!\n\n"
? ?? ?? ?? ?else:
? ?? ?? ?? ?? ? desp_Weather_3d_day_tempMax_text = "\n\n";
? ?? ???desp_day = (
? ?? ?? ?? ?? ? "當(dāng)前天氣:" + Weather_text + desp_Weather_text +
? ?? ?? ?? ?? ? "當(dāng)前溫度:" + Weather_temp + "℃" + desp_Weather_temp_text +
? ?? ?? ?? ?? ? "今天最高溫度:" + Weather_3d_day_tempMax + "℃。" + desp_Weather_3d_day_tempMax_text +
? ?? ?? ?? ?? ? "最低溫度:" + Weather_3d_day_tempMin + "℃。" + desp_Weather_3d_day_uvIndex??# 紫外線 16點后不提示
? ?? ?? ?? ?# "風(fēng)力等級:" + Weather["windScale"] + "\n\n\n"
? ?? ???);
? ? ##以下是通知內(nèi)容!
? ? desp = "沈陽市\(zhòng)n\n " + desp_day + desp_tomorrow+"\n\n"+songci;
? ? return desp;
# 程序入口,消息推送
def main():
? ? title = '查收天氣情況';
? ? desp = get_desp();
? ? url = f"https://sctapi.ftqq.com/{SendKey}.send?title={title}&desp={desp}";
? ? res = requests.post(url);
? ? if res.status_code == 200:
? ?? ???print("成功!")
? ? else:
? ?? ? title = "天氣推送失敗!"
? ?? ? print(res.status_code)
? ?? ? print("錯誤日志推送成功 !")
def main_handler(event, context):
? ?main()
? ?
??4)高級部分—設(shè)置內(nèi)存1024
??5)提交
??6)部署文件,部署的使用回發(fā)現(xiàn)requests,pytz兩個庫文件沒有,控制臺使用命令:
? ???安裝requests庫文件的使用注意要同目錄,第一個命令:cd src
? ???安裝的命令,? ?? ?? ?? ?? ???第二個命令pip3 install requests -t .? ? 一定要注意點的時候
??7)測試,測試通過成功回微信上收到公眾號給的提示消息,失敗給出原因查到對應(yīng)行。
??8)創(chuàng)建定時任務(wù)進(jìn)行運行,就大功告成了,祝你成功。
總結(jié)
以上是生活随笔為你收集整理的天气预报+老婆私人订制服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 互相关函数实现(c语言完整工程,利用cm
- 下一篇: css 科技 边框_CSS内嵌边框