Skip to content

Instantly share code, notes, and snippets.

@tokida
Created January 26, 2017 15: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 tokida/71cc2166f9387f4bd5b68c26bb6537f4 to your computer and use it in GitHub Desktop.
Save tokida/71cc2166f9387f4bd5b68c26bb6537f4 to your computer and use it in GitHub Desktop.
openwhisk action : today todo in todoist 2 slack post
import requests
import datetime
import json
def main(dict):
token = dict.get('todoist_token')
project_id = dict.get('todoist_projectId')
url = dict.get('slack_url')
todos = getList(token, int(project_id))
todos.sort(key=lambda x: (x[0], -x[1]))
if len(todos) != 0:
result = "Success"
postSlack(url, todos)
else:
result = "fail"
# return {'result': result, 'item': todos}
return {'result': result}
def getAllList(token):
q = {
'token': token,
'seq_no': 0,
'seq_no_global': 0,
'resource_types': '["items"]'
}
print token
r = requests.get('https://todoist.com/API/v7/sync', params=q)
r.encoding = r.apparent_encoding
return r.json()["items"]
def getList(token, project_id):
res = getAllList(token)
todos = []
for item in res:
if item['project_id'] is not None and item['project_id'] == project_id:
# print item['project_id']
if item['due_date_utc'] is not None:
if item['date_string'] is not "" or item['date_string'] is not None:
todo = []
tstr = item['due_date_utc'].replace('+0000', 'UTC')
tdatetime = datetime.datetime.strptime(
tstr, '%a %d %b %Y %H:%M:%S %Z')
# tdatetime = dt.strptime(tstr, '%a %d %b %Y 00:00:00 %Z')
if checkDate(tdatetime):
todo.append(tdatetime.strftime('%Y%m%d'))
todo.append(item['priority'])
todo.append(item['date_string'].encode('utf-8'))
todo.append(item['content'].encode('utf-8'))
todos.append(todo)
print todo
return todos
def checkDate(checktarget):
# 引数の時間帯が現時点からプラスマイナス24時間以内であるかをチェック
today = datetime.datetime.now()
yesterday = today + datetime.timedelta(days=-1)
tomorrow = today + datetime.timedelta(days=+1)
if checktarget > yesterday and checktarget < tomorrow:
return True
else:
return False
def postSlack(url, messages):
payload = {"text": "*Let's working today :+1: *", "mrkdwn": "true"}
attach = [] # attachmentsに入れる配列
for message in messages:
text = '%s, %s' % (
message[2], # due date
message[3] # text
)
# sprint messages
priority = message[1]
if priority == 4:
color = "#ff3905"
elif priority == 3:
color = "#ff6b00"
elif priority == 2:
color = "#cbc659"
else:
color = "#238e66"
tmp = {"color": color, "text": text}
attach.append(tmp)
payload["attachments"] = attach
requests.post(url, data=json.dumps(payload))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment