網(wǎng)站首頁 編程語言 正文
在國內(nèi),大部分人都是過農(nóng)歷生日,然后借助日歷工具獲取農(nóng)歷日期對應(yīng)的陽歷日期,以這一天來過生!
這里還有一個(gè)痛點(diǎn),即:每一年的農(nóng)歷生日對應(yīng)的陽歷日期都不一樣
本篇文章將教你利用 Python 制作一個(gè)簡單的生日提醒
1. 實(shí)戰(zhàn)
具體操作步驟如下
1-1??安裝依賴
#?安裝依賴
pip3?install?zhdate
pip3?install?pymysql
其中,zhdate 模塊用于中國農(nóng)歷、陽歷之間的轉(zhuǎn)換,并且支持日期差額計(jì)算
項(xiàng)目地址:
https://github.com/CutePandaSh/zhdate
1-2??創(chuàng)建數(shù)據(jù)表
創(chuàng)建一條數(shù)據(jù)表
create?table?birthday
(
????id????????int?auto_increment
????????primary?key,
????name??????varchar(100)??not?null?comment?'名稱',
????yl_birth??varchar(100)??not?null?comment?'陰歷生日',
????remark????varchar(100)??null?comment?'備注',
????is_delete?int?default?0?null?comment?'0:正常? 1:刪除'
)
????comment?'生日';
然后,將需要提醒用戶的姓名、農(nóng)歷生日等數(shù)據(jù)寫入
PS:這里陰歷生日格式是 mm-dd,比如:10-25
1-3??查詢數(shù)據(jù)
import?pymysql
class?Birth(object):
????def?__init__(self):
????????self.db?=?pymysql.connect(host='**',
??????????????????????????????????user='root',
??????????????????????????????????password='**',
??????????????????????????????????database='xag')
????????self.cursor?=?self.db.cursor()
????def?__get_births(self):
????????#?獲取所有數(shù)據(jù)
????????self.cursor.execute("""
?????????????????????????????select?name,yl_birth,remark?from?birthday?where?is_delete=0;""")
????????datas?=?list(self.cursor.fetchall())
1-4??遍歷,獲取距離今天的天數(shù)
遍歷上面的數(shù)據(jù),將陰歷轉(zhuǎn)為陽歷,然后計(jì)算出距離今天的天數(shù)
from?zhdate?import?ZhDate
...
??def?__get_diff(self,?birth):
????????"""
????????根據(jù)農(nóng)歷生日,獲取當(dāng)前日期距離的時(shí)間(天)
????????:param birth:?農(nóng)歷生日,格式:10-25
????????:return:
????????"""
????????#?1、獲取今日的農(nóng)歷日歷
????????now?=?str(datetime.now().strftime('%Y-%m-%d')).split("-")
????????#?年、月、日
????????year,?month,?day?=?int(now[0]),?int(now[1]),?int(now[2])
????????#?1、獲取陰歷生日,轉(zhuǎn)為陽歷
????????birth_month?=?int(birth.split("-")[0].strip())
????????birth_day?=?int(birth.split("-")[-1].strip())
????????birth_ying?=?ZhDate(year,?birth_month,?birth_day)
????????#?轉(zhuǎn)為陽歷
????????birth_yang?=?birth_ying.to_datetime()
????????#?2、計(jì)算距離當(dāng)前日期的時(shí)間間隔(天)
????????today?=?datetime.now().strftime('%Y-%m-%d')
????????d1?=?datetime.strptime(today,?'%Y-%m-%d')
????????diff_day?=?(birth_yang-d1).days
????????return?diff_day
...
?#?遍歷數(shù)據(jù)
????????for?item?in?datas:
????????????name?=?item[0]
????????????birth?=?item[1]
????????????nickname?=?item[2]
????????????diff?=?self.__get_diff(birth)
...
1-5??組裝數(shù)據(jù)及消息推送
通過時(shí)間間隔,在提前一周、生日當(dāng)天做一個(gè)提醒
最后,將組裝好的消息通過企業(yè)微信機(jī)器人發(fā)送出去
import?requests
import?json
...
???def?send_wechat(self,?msg:?str):
????????"""發(fā)送信息到企業(yè)微信"""
????????#?這里填寫你的機(jī)器人的webhook鏈接
????????url?=?'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key**'
????????headers?=?{"Content-Type":?"text/plain"}
????????data?=?{
????????????"msgtype":?"text",
????????????"text":?{
????????????????"content":?msg
????????????}
????????}
????????#?發(fā)送消息
????????requests.post(url,?headers=headers,?data=json.dumps(data))
...
原文鏈接:https://mp.weixin.qq.com/s/CKscpZObQQKXdhmQFGF1ug
- 上一篇:C語言中的冒泡排序問題_C 語言
- 下一篇:C語言多文件編程問題解析_C 語言
相關(guān)推薦
- 2022-01-30 tortoiseGit推送每次需要輸入密碼,解決方案
- 2022-09-25 服務(wù)器ftp上傳失敗的原因有什么
- 2021-12-10 時(shí)間戳處理的幾種方式
- 2022-09-13 Go語言中map使用和并發(fā)安全詳解_Golang
- 2023-02-12 Python利用物理引擎Pymunk編寫一個(gè)解壓小游戲_python
- 2022-11-23 Pandas?DataFrame操作數(shù)據(jù)增刪查改_python
- 2022-01-06 springmvc中的model和HttpServletRequest的區(qū)別詳細(xì)分析
- 2022-10-18 C/C++函數(shù)指針深入探究_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支