Last active
December 7, 2015 22:06
-
-
Save masahiro-mi/69ce7d55a4bac0e0e8d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
変更点
・Slackのtokenに対してstrip()をかけていなかったので、strip()を追加
・Todoistに期限の設定されていないタスクがあると、タスクなしとして表示されるバグを修正
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slack APIの利用にはSlacker、Todoist APIの利用にはpytodoistを利用します。
$pip install Slacker pytodoist
でインストール可能です。