mekk.rtm / sample / sample_client.py

# -*- coding: utf-8 -*-

"""
Example updates
"""

import webbrowser
#import xml.etree.ElementTree as ElementTree
import keyring
import getpass
from mekk.rtmimport import RtmConnector, RtmClient
import logging

logging.basicConfig(level = logging.DEBUG)

APP_NAME = "mekk.rtmimport sample"
API_LABEL = "api-key"
SEC_LABEL = "sec-code"
TOKEN_LABEL = "default-user-token"

def create_client():
    api_key = keyring.get_password(APP_NAME, API_LABEL)
    if not api_key:
        api_key = getpass.getpass("Enter API Key: ")
        keyring.set_password(APP_NAME, API_LABEL, api_key)

    api_sec = keyring.get_password(APP_NAME, SEC_LABEL)
    if not api_sec:
        api_sec = getpass.getpass("Enter shared secret: ")
        keyring.set_password(APP_NAME, SEC_LABEL, api_sec)

    token = keyring.get_password(APP_NAME, TOKEN_LABEL)

    connector = RtmConnector(api_key, api_sec, "write", token)
    
    if not connector.token_valid():
        url, frob = connector.authenticate_desktop()
        print "Opening browser window to authenticate the script"
        webbrowser.open(url)
        raw_input("Press Enter once you authenticated script to access your RememberTheMilk account.")
        if connector.retrieve_token(frob):
            keyring.set_password(APP_NAME, TOKEN_LABEL, connector.token)
            print "Access token saved"
        else:
            raise Exception("Failed to grab access token")

    client = RtmClient(connector)
    return client

def run():
    client = create_client()

    ######################################################################
    # Lists
    ######################################################################

    print "Known lists: "
    for l in client.known_lists():
        print l

    print "Known smart lists: "
    for l in client.known_smart_lists():
        print l

    test_list = client.find_or_create_list(u"Zażółć gęślą jaźń")
    print "Test list: ", test_list

    test_list = client.archive_list(test_list.id)
    print "After archiving: ", test_list

    test_list = client.unarchive_list(test_list.id)
    print "After unarchiving: ", test_list

    ######################################################################
    # Tasks
    ######################################################################
    
    task = client.create_task(
        u"Bardzo poważne zadanie",
        list_id = test_list.id,
        tags = ["testy", "@dom"],
        priority = 3,
        due_date = "2010-11-30",
        estimate = "1 day 10 hours",
        repeat = "after 3 days",
        url = "http://www.google.com",
        completed = True,
        notes = [ (u"Brzęczy", u"mucha na pianinie\ni zupełnie nic nie ginie"),
                  (u"Strzela", u"krokodyl zielony\nbawi się z tej owej strony") ])
    print "Created task", task

    print "Tasks on test list"
    for t in client.find_tasks(test_list.id):
        print t

    print "All incomplete repeating tasks"
    for t in client.find_tasks(test_list.id, filter = "status:incomplete and isRepeating:true"):
        print t

    print "All tasks with notes"
    for t in client.find_tasks(test_list.id, filter = "status:incomplete and hasNotes:true"):
        print t

    ######################################################################
    # Cleanup
    ######################################################################

    test_list = client.delete_list(test_list.id)
    print "After deletion", test_list


if __name__ == "__main__":
    run()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.