Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import sys
from pytodoist import todoist
from datetime import datetime, timedelta, date
from slacker import Slacker
# 日付を変えて表示するための埋め込み
embedding_date = [
[-365, 0, 'スタックされた'],
[0, 1, '今日の'],
[1, 3, '三日以内の'],
[3, 7, '今週の']
]
# TodoistのAPT Token
todoist_token = None
try:
f = open('.todoist_credentials', 'r')
todoist_token = f.readline()
except:
print('You have no todoist credentials, please input your todoist API token:')
todoist_token = sys.stdin.readline()
try:
todoist_usr = todoist.login_with_api_token(todoist_token.strip())
with open('.todoist_credentials', 'w') as f:
f.write(todoist_token.strip())
except Exception as e:
print('Cannot login with your todoist credentials or API token.')
print(e)
exit()
# slackのAPI Token
slack_token = None
try:
f = open('.slack_credentials', 'r')
slack_token = f.readline()
except:
print('You have no slack credentials, please input your slack API token:')
slack_token = sys.stdin.readline()
try:
slack_usr = Slacker(slack_token.strip())
with open('.slack_credentials', 'w') as f:
f.write(slack_token.strip())
except Exception as e:
print('Cannot login with your slack credentials or API token.')
print(e)
exit()
# 一応のためTodoistにsync
todoist_usr.sync()
# 現在時刻の取得(UTC)
now = datetime.utcnow()
content = []
# 埋め込み日付を参考にしてTodoistのタスクを取得して古い順に並べる
for e in embedding_date:
emb_content = []
for t in sorted( [ x for x in todoist_usr.get_tasks() if x.due_date is not None ], key=lambda t:datetime.strptime(t.due_date, '%a %d %b %Y %H:%M:%S +0000')):
if t.project.name == '<取得したいプロジェクト名>' :
if datetime.strptime(t.due_date, '%a %d %b %Y %H:%M:%S +0000') >= now + timedelta(days=e[0]) and datetime.strptime(t.due_date, '%a %d %b %Y %H:%M:%S +0000') < now + timedelta(days=e[1]):
emb_content.append('> ・'+t.date_string+'\t'+t.content)
if len(emb_content) > 0:
emb_content.insert(0, '>#'+e[2]+'Todoist:')
content.extend(emb_content)
if len(content) == 0:
content.append('>直近1週間でのTodoはありません。\n>Excellent work!!!')
content.append('> Karman:'+str(todoist_usr.karma)+' '+str(todoist_usr.karma_trend))
print(content)
slack_usr.chat.post_message('<投稿したいSlackのチャンネル>', '\n'.join(content))
Owner

masahiro-mi commented Nov 29, 2015

Slack APIの利用にはSlacker、Todoist APIの利用にはpytodoistを利用します。
$pip install Slacker pytodoist
でインストール可能です。

Owner

masahiro-mi commented Dec 7, 2015

変更点
・Slackのtokenに対してstrip()をかけていなかったので、strip()を追加
・Todoistに期限の設定されていないタスクがあると、タスクなしとして表示されるバグを修正

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment