Skip to content

Instantly share code, notes, and snippets.

@s5unty
Last active December 14, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s5unty/5075704 to your computer and use it in GitHub Desktop.
Save s5unty/5075704 to your computer and use it in GitHub Desktop.
todo.txt => pushover
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# inspired on: https://github.com/jkrehm/todotxt-cli-addons
#
# $ crontab -l
# */5 * * * * TZ='Asia/Shanghai' /sun/todo/opt/remind/pushover ${YOUR_TODO_FILE} ${YOUR_API_TOKEN} ${YOUR_USER_KEY}
# */5 * * * * TZ='Asia/Shanghai' /sun/todo/opt/remind/pushover ${ANOTHER_TODO_FILE} ${YOUR_API_TOKEN} ${YOUR_USER_KEY}
#
# 在 pushover.net 申请 App 后得到 ${YOUR_API_TOKEN}
# 在 pushover.net 注册用户后得到 ${YOUR_USER_KEY}
#
# 为什么要导出时区环境变量(TZ)?
# 命令行调试正常,放到 cron 执行时却失败,原因是
# cron 默认使用系统时区(/etc/timezone),有可能不同于当前登录用户环境所采用的时区
import sys, httplib, os, re, urllib
from datetime import datetime, timedelta, date, time
todo_file = sys.argv[1]
api_token = sys.argv[2]
user = sys.argv[3]
today = datetime.today()
todayDt = str(today.date())
todayDttm = today.strftime('%Y-%m-%d %H:%M')
f = open(todo_file)
for line in f:
m = re.search('^[^xX]* [uwt]:([0-9]{4}-[0-9]{2}-[0-9]{2})(\+[0-9]+[dDwW])?!?([0-9]{1,2}:[0-9]{2})?(\+[0-9]+[mMhH])?', line)
if m is None:
continue
# 支持以下这些格式:
# format string | group(1) | group(2) | group(3) | group(4)
# ----------------------------+------------+----------+-----------+---------
# t:2013-03-03 | 2013-03-03 | None | None | None
# u:2013-03-03+3d | 2013-03-03 | +3d | None | None
# u:2013-03-03+1w!14:55 | 2013-03-03 | +1w | 14:55 | None
# w:2013-03-03!09:40 | 2013-03-03 | None | 09:40 | None
# t:2013-03-03!14:55+1h | 2013-03-03 | None | 14:55 | +1h
# t:2013-03-03+1d!14:55+30m | 2013-03-03 | +1d | 14:55 | +1h
if m.group(1) is not None:
ddate = datetime.strptime(m.group(1), '%Y-%m-%d')
dlast = ddate - timedelta(days=0) # 最后一次提醒:任务到期当天凌晨
if m.group(2) is not None:
mm = re.search('\+([0-9]+)([dDwW])', m.group(2))
# format string | group(1) | group(2)
# ----------------+-----------+---------
# +3d | 3 | d
# +1W | 1 | W
# +1day | 1 | d
# +d1 | None | None <== ignore
if mm.group(1).isdigit():
if mm.group(2) == 'd' or mm.group(2) == 'D': ddate = ddate - timedelta(days=int(mm.group(1))) # 日期
elif mm.group(2) == 'w' or mm.group(2) == 'W': ddate = ddate - timedelta(weeks=int(mm.group(1))) # 星期
if m.group(3) is not None:
dtime = datetime.strptime("%s %s" % (ddate.strftime("%Y-%m-%d"), m.group(3)), '%Y-%m-%d %H:%M')
dlast = dtime - timedelta(minutes=5) # 最后一次提醒:任务到期前 5 分钟
if m.group(4) is not None:
mm = re.search('\+([0-9]+)([mMhH])', m.group(4))
# format string | group(1) | group(2)
# ----------------+-----------+---------
# +1m | 1 | m
# +1hour | 1 | h
# +30m | 30 | m
# +h1 | None | None <== ignore
if mm.group(1).isdigit():
if mm.group(2) == 'm' or mm.group(2) == 'M': dtime = dtime - timedelta(minutes=int(mm.group(1))) # 分钟
elif mm.group(2) == 'h' or mm.group(2) == 'H': dtime = dtime - timedelta(hours=int(mm.group(1))) # 小时
if 'dtime' in dir():
dttm = dtime.strftime('%Y-%m-%d %H:%M')
del dtime
else:
dttm = ddate.strftime('%Y-%m-%d 00:00')
last = dlast.strftime('%Y-%m-%d %H:%M')
if dttm == todayDttm or last == todayDttm:
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request('POST', '/1/messages.json',
urllib.urlencode({
'token': api_token,
'user': user,
'message': line,
}), { 'Content-type': 'application/x-www-form-urlencoded' }
)
print conn.getresponse()
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment