LoginSignup
17
18

More than 5 years have passed since last update.

TodoistのAPIを使い任意のプロジェクトを作成してみる

Last updated at Posted at 2014-06-06

Todoistを最近使ってます。

UIがいい感じ、素敵

(最近awsの障害影響でダウンしてましたが・・・)

自分の仕事上での使い方としては、

毎日年月日のプロジェクトを作って、そこでタスク管理しています。

20140531

20140601

20140602

みたいな・3・

でも毎日プロジェクト作成するのめんどくさい。

って思ってたらTodoistのAPIの存在を知ったので、APIを使って自分用のバッチファイルをPHPで作りました。

Todoist.php
<?php

require_once 'HTTP/Request.php';
require_once 'TodoistException.php';

class Todoist
{
    const API_URL = 'https://todoist.com/API/';
    const MAIL_ADDRESS = '';
    const PASSWORD = '';
    private $token;

    public function __construct()
    {
        $this->login(Todoist::MAIL_ADDRESS, Todoist::PASSWORD);
    }

    private function login($email, $password)
    {
        $params = array(
            'email' => $email,
            'password' => $password
        );

        $url = $this->createUrl('login', $params);
        $res = $this->remote($url, array());

        if ($res === 'LOGIN_ERROR')
        {
            throw new TodoistException('login error');
        }
        $this->token = $res->token;
    }

    public function createProject($projectName)
    {
        $params = array(
            'name' => $projectName,
            'token' => $this->token
        );

        $url = $this->createUrl('addProject', $params);
        $res = $this->remote($url, array());
        if ($res === 'ERROR_NAME_IS_EMPTY') {
            throw new TodoistException('project name error');
        }
    }

    private function remote($url, $option)
    {
        $http = new HTTP_Request($url, $option);
        if (!PEAR::isError($http->sendRequest())) {
            $res = $http->getResponseBody();
            return json_decode($res);
        } else {
            throw new TodoistException('remote error');
        }
    }

    private function createUrl($action, $params)
    {
        return Todoist::API_URL.$action.'?'.http_build_query($params, NULL, '&');
    }

}
TodoistException.php
<?php

class TodoistException extends Exception
{
    public function __construct($message, $code = 0, Exception $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }
    public function __toString()
    {
        return __CLASS__. " : [{$this->message}]\n";
    }
}
todoist_batch.php
<?php

require_once 'Todoist.php';
try {
    $todoist = new Todoist();
    $todoist->createProject(date('Ymd'));
} catch (Exception $e) {
    echo $e;
}

みたいなバッチファイルを作成して、cronに登録して毎日実行させてます。


30 00 * * * /usr/bin/php /var/phpwork/batch/todoist_batch.php >> /var/log/batch.log 2>&1

17
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
18