python, 实现朋友家人年历生日自动提醒

为了方便提醒自己,今天有哪位朋友过生日(年历生日)。

测试环境:fedora25桌面版。

 

建立一个生日配置文件,注意:按日期排好序

vi /etc/birthday.txt

朋友A 1-4
朋友C 2-3
朋友B 3-8

 

创建脚本文件

创建脚本文件 /usr/bin/check_birthday, 脚本内容如下。

#!/usr/bin/python
# -*- coding: utf-8 -*-

from lunardate import LunarDate
import os
import sys

def get_month_day(datestring):
    array = datestring.split("-")

    if len(array) == 3:
        (y, m, d) = array
        return int(m), int(d)
    else:
        (m, d) = array
        return int(m), int(d)


def is_birthday_today(date, birthday):
    m, d = get_month_day(birthday)
    return m == date.month and d == date.day


def distance_birthday_today(date, birthday):
    m, d = get_month_day(birthday)
    return (m - date.month) * 30 + d - date.day


def showtip(line):
    os.system('zenity --info --title "title" --text "' + line + '" --width=300 --height=200')


filename='/etc/birthday.txt'
lines = open(filename).readlines()

lines = map(lambda x: x.strip(), lines)
lines = filter(lambda x: x != "", lines)


if len(lines) == 0 or lines[0].strip() == "":
  print("配置文件%s中没有数据" % filename)
  sys.exit(1)

today = LunarDate.today()

exist_someone_birthday = False

# 查找谁今天过生日
for line in lines:
    array = line.split()
    if is_birthday_today(today, array[1]):
        exist_someone_birthday = True
        showtip("今天生日, " + line)
        sys.exit(0)

all_finish_this_year = True
# 提醒谁快要过生日
if not exist_someone_birthday:
    for line in lines:
        array = line.split()
        if distance_birthday_today(today, array[1]) > 0:
	    all_finish_this_year = False
            showtip("今天%d-%d, %s" % (today.month, today.day, line))
            sys.exit(0)

if all_finish_this_year:
  showtip("今天%d-%d, %s" % (today.month, today.day, lines[0]))


 

设置脚本执行权限

chmod 777 /usr/bin/check_birthday

 

下载LunarDate模块

pip install LunarDate

提示:如果系统中没有安装pip,先安装pip(yum install -y pip)

 

检查今天是否有人过生日

check_birthday

 

可以设置每天定时检查

通过crontab -e进行设置

# 每天12,20点检查一次

0 12,20 * * * /usr/bin/check_birthday

 

转载于:https://my.oschina.net/u/1263909/blog/880855

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值