日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

天气预报+老婆私人订制服务

發(fā)布時(shí)間:2024/3/26 编程问答 76 豆豆
生活随笔 收集整理的這篇文章主要介紹了 天气预报+老婆私人订制服务 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

服務(wù)器免費(fèi)但是有期限,騰訊云現(xiàn)在免費(fèi)3個(gè)月對(duì)新用戶

我在其他地方也發(fā)表了這個(gè)文章供大家參考。

每天一份天氣預(yù)報(bào),加上老婆喜歡的宋詞,花語(yǔ),唐詩(shī)都可以自己選擇,
開(kāi)發(fā)簡(jiǎn)單同時(shí)還能聯(lián)系下"騰訊云serverless函數(shù)"的使用。
1.Server醬??,關(guān)注公眾號(hào)每天給你提醒的路徑,親測(cè)安全。注冊(cè)PC路徑:https://sct.ftqq.com/login
? ? 按照提示手機(jī)關(guān)注下公眾號(hào),記錄下頁(yè)面顯示的ID。后期“SendKey"需要使用。
2.和風(fēng)天氣API,需要微信和郵箱激活下就可以使用??,注冊(cè)PC路徑:? ??https://console.qweather.com/#/console
? ? 按照提示激活獲取ID,記錄下頁(yè)面顯示的ID。后期“Weather_key"需要使用。
3.天行數(shù)據(jù),使用郵箱注冊(cè)激活就好,這里面的數(shù)據(jù)很豐富,宋詞、詩(shī)歌、花語(yǔ)等等太多了你自己選擇,注冊(cè)PC路徑???https://www.tianapi.com/console/
? ? 按照提示注冊(cè)獲取apiID,記錄下頁(yè)面顯示的ID。后期“ApiKey"需要使用。?需要提醒下不同數(shù)據(jù)個(gè)別需要使用地址或者花名,自己先測(cè)試下額外參數(shù)。
4.騰訊云serverless注冊(cè),這個(gè)是服務(wù)器函數(shù)使用地址,可以免費(fèi)3個(gè)月哦,續(xù)費(fèi)也很便宜,注冊(cè)PC地址:https://console.cloud.tencent.com/
1)函數(shù)服務(wù)—新建
2)新建—從頭開(kāi)始—事件函數(shù)—運(yùn)行環(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'
# 接收消息的用戶,存在多個(gè)以 | 隔開(kāi)
userid = ''


adminUserId = '';


def get_time():
? ? nowTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime());??# 獲取當(dāng)前時(shí)間
? ? # 由于騰訊云時(shí)間不準(zhǔn)確,暫時(shí)使用年月日
? ? nowTime_1 = time.strftime("%Y-%m-%d", time.localtime());??# 獲取當(dāng)前時(shí)間
? ? # 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)天天氣,不過(guò)沒(méi)有最高溫度,最低溫度
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)的未來(lá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='沈陽(yáng)'
? ? 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__("沈陽(yáng)"):
? ?? ?? ?? ? #print(Weather_city)
? ?? ?? ?? ? city_id = Weather_city["id"]
? ?? ?? ?? ? #print(city_id)
? ?? ?? ?? ? return city_id;


? ? else:
? ?? ?print("訪問(wèn)獲取地區(qū)接口失敗!")
? ?? ?return "訪問(wèn)獲取地區(qū)接口失敗!";


#拼接消息
def get_desp():


? ? #wyy_content = get_wyy()[0];??# 評(píng)論內(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"] #實(shí)時(shí)獲取當(dāng)前溫度
? ? Weather_feelsLike = Weather["feelsLike"] #獲取當(dāng)前體感溫度


? ? Weather_3d_day = get_3d_Weather(1)??# 獲取當(dāng)天的預(yù)報(bào)天氣
? ? Weather_3d_day_tempMax = Weather_3d_day["tempMax"] #獲取當(dāng)天預(yù)報(bào)最高溫度
? ? Weather_3d_day_tempMin = Weather_3d_day["tempMin"] #獲取當(dāng)天預(yù)報(bào)最低溫度
? ? 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ù)報(bào)最高溫度
? ? Weather_3d_tomorrow_tempMin = Weather_3d_day["tempMin"]??# 獲取第二天預(yù)報(bào)最低溫度
? ? 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 = "";


? ? # 晚上八點(diǎn) 查詢第二天天氣情況,然后根據(jù)當(dāng)前時(shí)間,定制化推送當(dāng)前提醒
? ? #if (local_time >= 20 and local_time <= 23):
? ? if (True):
? ?? ???# 如果有雨,不校驗(yàn)最高溫度
? ?? ???# 判斷天氣
? ?? ???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 + ",傻瓜,記得帶傘,記得早點(diǎn)出門,晚上早點(diǎn)睡\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 + ",傻瓜,記得帶傘,記得早點(diǎn)出門,晚上早點(diǎn)睡\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 + ",記得帶傘,請(qǐng)注意天氣\n\n"
? ?? ???# 沒(méi)雨的情況下,根據(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 = ",天氣有點(diǎn)熱喔!\n\n"
? ?? ?? ?? ?else:
? ?? ?? ?? ?? ? desp_Weather_temp_text = "。\n\n";
? ?? ???else:
? ?? ?? ?? ?desp_Weather_temp_text = "。\n\n";


? ?? ???# 判斷紫外線等級(jí)
? ?? ???if (8 <= local_time < 16):
? ?? ?? ?? ?if (4 < int(Weather_3d_day_uvIndex) <= 6):
? ?? ?? ?? ?? ? desp_Weather_3d_day_uvIndex = "\n\n今天紫外線等級(jí)" + Weather_3d_day_uvIndex + ",適合戶外運(yùn)動(dòng)。\n\n"
? ?? ?? ?? ?elif (6 < int(Weather_3d_day_uvIndex) <= 9):
? ?? ?? ?? ?? ? desp_Weather_3d_day_uvIndex = "\n\n今天紫外線等級(jí)" + Weather_3d_day_uvIndex + ",請(qǐng)注意防曬。\n\n"
? ?? ?? ?? ?elif (9 < int(Weather_3d_day_uvIndex)):
? ?? ?? ?? ?? ? desp_Weather_3d_day_uvIndex = "\n\n今天紫外線等級(jí)" + 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 + ",請(qǐng)注意天氣\n\n"
? ?? ???else:
? ?? ?? ?? ?desp_Weather_text = "\n\n";
? ?? ?? ?? ?# 判斷當(dāng)天最高溫度
? ?? ?? ?? ?if (int(Weather_3d_day_tempMax) >= 33):
? ?? ?? ?? ?? ? # 下午16點(diǎn)以后不用提示
? ?? ?? ?? ?? ? 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點(diǎn)后不提示
? ?? ?? ?? ?# "風(fēng)力等級(jí):" + Weather["windScale"] + "\n\n\n"
? ?? ???);
? ? ##以下是通知內(nèi)容!
? ? desp = "沈陽(yáng)市\(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("錯(cuò)誤日志推送成功 !")
def main_handler(event, context):
? ?main()
? ?
??4)高級(jí)部分—設(shè)置內(nèi)存1024
??5)提交
??6)部署文件,部署的使用回發(fā)現(xiàn)requests,pytz兩個(gè)庫(kù)文件沒(méi)有,控制臺(tái)使用命令:
? ???安裝requests庫(kù)文件的使用注意要同目錄,第一個(gè)命令:cd src
? ???安裝的命令,? ?? ?? ?? ?? ???第二個(gè)命令pip3 install requests -t .? ? 一定要注意點(diǎn)的時(shí)候
??7)測(cè)試,測(cè)試通過(guò)成功回微信上收到公眾號(hào)給的提示消息,失敗給出原因查到對(duì)應(yīng)行。
??8)創(chuàng)建定時(shí)任務(wù)進(jìn)行運(yùn)行,就大功告成了,祝你成功。

總結(jié)

以上是生活随笔為你收集整理的天气预报+老婆私人订制服务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。