Beispiel #1
0
def retrieve_words(request):
    frob = request.GET.get('frob')

    api = Rtm(settings.RTM_API_KEY, settings.RTM_API_SECRET, "read", None)
    api.retrieve_token(frob)

    if not api.token_valid():
        raise ValueError("not a valid token")

    result = api.rtm.tasks.getList(list_id=settings.RTM_WORD_LIST_ID)
    flashcards = []
    quotes = []
    for tasklist in result.tasks:
        for taskseries in tasklist:
            parts = taskseries.name.split(':')
            if len(parts) == 1:
                quotes.append(''.join(parts))
            if len(parts) > 1:
                flashcards.append((parts[0], ':'.join(parts[1:])))
    context = {
        "rtw_data": json.dumps({"flashcards": flashcards, "quotes": quotes}),
        "api_token": api.token,
    }

    return render(request, 'rtw.html', context=context)
Beispiel #2
0
def connect(api_key=None, shared_secret=None, token=None, frob=None):
    api_key = api_key or os.environ.get("RTM_KEY")
    shared_secret = shared_secret or os.environ.get("RTM_SECRET")
    token = token or os.environ.get("RTM_TOKEN")

    if not api_key or not shared_secret or not (token or frob):
        raise ValueError(
            "Expected an API key, app secret, and either a token or frob.")

    api = Rtm(api_key, shared_secret, "delete", token)

    if frob:
        api.retrieve_token(frob)
        print("New token: ", api.token)
    else:
        # authenication block, see http://www.rememberthemilk.com/services/api/authentication.rtm
        # check for valid token
        if not api.token_valid():
            md5 = hashlib.md5()
            md5.update("{secret}api_key{key}permsdelete".format(
                secret=shared_secret, key=api_key))
            api_sig = md5.hexdigest()
            generate_frob_url = _generate_frob_url(api_key, shared_secret)
            raise ValueError(
                "An invalid token was provided. Try generating a new frob in the browser by visiting {}"
                .format(generate_frob_url))

    return api
Beispiel #3
0
def retrieve_words(request):
    frob = request.GET.get('frob')

    api = Rtm(settings.RTM_API_KEY, settings.RTM_API_SECRET, "read", None)
    api.retrieve_token(frob)

    if not api.token_valid():
        raise ValueError("not a valid token")

    result = api.rtm.tasks.getList(list_id=settings.RTM_WORD_LIST_ID)
    flashcards = []
    quotes = []
    for tasklist in result.tasks:
        for taskseries in tasklist:
            parts = taskseries.name.split(':')
            if len(parts) == 1:
                quotes.append(''.join(parts))
            if len(parts) > 1:
                flashcards.append((parts[0], ':'.join(parts[1:])))
    context = {
        "rtw_data": json.dumps({
            "flashcards": flashcards,
            "quotes": quotes
        }),
        "api_token": api.token,
    }

    return render(request, 'rtw.html', context=context)
Beispiel #4
0
def main():
    api_key = "910f700cc0ff23ed2462312c250bc560"
    shared_secret = "b7be4d2a824b42b1"

    token = sys.argv[2] if len(sys.argv) == 3 else None
    task_name = sys.argv[1]

    api = Rtm(api_key, shared_secret, "delete", token)

    if not api.token_valid():
        url, frob = api.authenticate_desktop()

        webbrowser.open(url)
        input("Continue?")

        # Get the token for the frob
        assert (api.retrieve_token(frob))

        print("New token: {}".format(api.token))

    result = api.rtm.timelines.create()
    timeline = result.timeline.value

    # Add task - default is Inbox
    result = api.rtm.tasks.add(timeline=timeline, parse="1", name=task_name)

    task_id = result.list.taskseries.id
    print("Create task, id: {}".format(task_id))
Beispiel #5
0
import sys
import webbrowser
from rtmapi import Rtm

if __name__ == '__main__':
    # call the program as `listtasks.py api_key shared_secret [optional: token]`
    # get those parameters from http://www.rememberthemilk.com/services/api/keys.rtm
    api_key, shared_secret = sys.argv[1:3]
    token = sys.argv[3] if len(sys.argv) >= 4 else None
    api = Rtm(api_key, shared_secret, "delete", token)

    # authenication block, see http://www.rememberthemilk.com/services/api/authentication.rtm
    # check for valid token
    if not api.token_valid():
        # use desktop-type authentication
        url, frob = api.authenticate_desktop()
        # open webbrowser, wait until user authorized application
        webbrowser.open(url)
        raw_input("Continue?")
        # get the token for the frob
        api.retrieve_token(frob)
        # print out new token, should be used to initialize the Rtm object next time
        # (a real application should store the token somewhere)
        print "New token: %s" % api.token

    # get all open tasks, see http://www.rememberthemilk.com/services/api/methods/rtm.tasks.getList.rtm
    result = api.rtm.tasks.getList(filter="status:incomplete")
    for tasklist in result.tasks:
        for taskseries in tasklist:
            print taskseries.task.due, taskseries.name
def workhorse():
    signal.signal(signal.SIGINT, quitter)

    # put the api token and secret in the credentials file (chmod 600)
    # get those parameters from http://www.rememberthemilk.com/services/api/keys.rtm

    debug = True if (len(sys.argv) > 1 and sys.argv[1] == 'debug') else False
    colourtest = True if (len(sys.argv) > 1 and sys.argv[1] == 'colourtest') else False

    config = configparser.ConfigParser()
    # test for file presence
    # if not there, exit telling user to rename template to file and get api key
    ## TODO: implement previous comment
    config.read_file(open('rtmstatus.conf'))
    # test for config items existing
    if (not config.has_option('main', 'api_key') or not config.has_option('main', 'shared_secret')):
        # without api_key or shared_secret, I cannot do anything, so exit and tell user to get an API key
        print ('You need to get a RTM API key and shared secret to use this script.')
        # exiting with an error code because the script cannot run
        sys.exit(1)
    # if token isn't there then launch API without it so we can get one and store it, else launch anyway.
    if config.has_option('main','token'):
        api = Rtm(config.get('main', 'api_key'), config.get('main', 'shared_secret'), 'read', config.get('main', 'token'))
    else:
        api = Rtm(config.get('main', 'api_key'), config.get('main', 'shared_secret'), 'read', None)

    # authentication block, see http://www.rememberthemilk.com/services/api/authentication.rtm
    # check for valid token

    if not api.token_valid():
        # use desktop-type authentication
        url, frob = api.authenticate_desktop()
        # open webbrowser, wait until user authorized application
        webbrowser.open(url)
        input("Continue?")
        # get the token for the frob
        api.retrieve_token(frob)
        # If the token turns out to be invalid, we get a valid one and store it.
        config.set('main','token',api.token)
        with open('rtmstatus.conf', 'w') as configfile:
            config.write(configfile) # TODO test that this worked
        # reinitialize the Rtm object
        del api
        api = Rtm(config.get('main', 'api_key'), config.get('main', 'shared_secret'), 'read', config.get('main', 'token'))

    display.connect()
    display.display_on()

    print('Press Ctrl+C to quit')

    while True:
        backlight = config.get('main', 'defaultcolour')
        count = {}
        sections = config.sections()
        sections.sort(reverse=True)

        for section in sections:

            if section != 'main':

                count[section] = taskcounter(config.get(section, 'filter'), api)

        display.clear()

        for section in sections:

            if section != 'main':

                display.set_cursor_position(int(config.get(section,'x')),int(config.get(section,'y')))
                if int(count[section]) < 10:
                    display.write(config.get(section,'label') + ': ' + str(count[section]))
                else:
                    display.write(config.get(section,'label') + ':' + str(count[section]))

                if (int(count[section]) > int(config.get(section,'threshold'))):
                    backlight = config.get(section, 'colour')

        rgb = backlight.split(',')
        display.set_backlight_rgb(int(rgb[0]),int(rgb[1]),int(rgb[2]))
        time.sleep(float(config.get('main','polling_delay')))
Beispiel #7
0
from Adafruit_Thermal import *

if __name__ == '__main__':
    api = Rtm(rtm_const.api_key, rtm_const.shared_secret, "read", rtm_const.token)

    # authenication block, see http://www.rememberthemilk.com/services/api/authentication.rtm
    # check for valid token
    if not api.token_valid():
        # use desktop-type authentication
        url, frob = api.authenticate_desktop()
        # open webbrowser, wait until user authorized application
        print ("URL you need to open: %s" % url)
        webbrowser.open(url)
        raw_input("Continue?")
        # get the token for the frob
        api.retrieve_token(frob)
        # print out new token, should be used to initialize the Rtm object next time
        print ("token: %s" % api.token)
        f1=open('./rtm_const', 'w+')
        f1.write("token: %s\n" % api.token)
        print ("You're going to need to remove the duplicate token line\n")
        f1.close

    printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)

    # get all open tasks, see http://www.rememberthemilk.com/services/api/methods/rtm.tasks.getList.rtm
    result = api.rtm.tasks.getList(filter="due:today")

    box     = chr(0xaf) # Degree symbol on thermal printer
    printer.println("------------------------------")
    for tasklist in result.tasks:
Beispiel #8
0
def main(raw_args=None):
    # Check the arguments passed to this script
    parser = argparse.ArgumentParser(
        description='Get a random TO DO from Remember the Milk!',
        prefix_chars='-/',
        epilog=
        'Note that multiple flags are ANDed together making your search more specific. See docs for more info.'
    )
    parser.add_argument('--loglevel',
                        dest='loglevel',
                        metavar='',
                        choices=['debug'],
                        type=str.lower,
                        help="[optional] Set the log level (e.g. debug, etc.)")
    parser.add_argument(
        '-l',
        '--list',
        metavar='',
        help=
        "[optional] Select a specific list to search in. Use quotes if your list name has spaces in it."
    )
    parser.add_argument(
        '-t',
        '--tag',
        metavar='',
        help="[optional] Select a specific tag to add to your search filter.")
    parser.add_argument(
        '-p',
        '--priority',
        metavar='',
        choices=['1', '2', '3', 'N'],
        help=
        "[optional] Select a specific priority to add to your search filter.")
    args = parser.parse_args(raw_args)

    # Set loglevel if --loglevel argument is used, otherwise set to INFO
    if args.loglevel is None:
        logging_num_level = 20
    else:
        logging_num_level = getattr(logging, args.loglevel.upper())

    LOG_FORMAT = "\n %(levelname)s: %(message)s"
    logging.basicConfig(level=logging_num_level, format=LOG_FORMAT)

    logging.debug("Args passed in are: " + str(args))

    rtm_list = args.list
    rtm_tag = args.tag
    rtm_priority = args.priority

    # Look for a token in ~/.rtm_auth_token first
    user_home_dir = os.path.expanduser("~")
    rtm_auth_file = os.path.join(user_home_dir, '.rtm_auth_token')
    if os.path.exists(rtm_auth_file):
        with open(rtm_auth_file, "r") as f:
            token = f.readline()
    else:
        token = None

    # Create a class instance using the RtmAPI module
    api = Rtm(api_key, shared_secret, "read", token, api_version=2)

    # Authenication block, see http://www.rememberthemilk.com/services/api/authentication.rtm
    # Check for valid token. If none, open a browser so the user can authenticate.
    if not api.token_valid():
        # use desktop-type authentication
        url, frob = api.authenticate_desktop()
        # open webbrowser, wait until user authorized application
        webbrowser.open(url)
        raw_input("Continue?")
        # get the token for the frob
        api.retrieve_token(frob)
        # print out new token, should be used to initialize the Rtm object next time
        # (a real application should store the token somewhere)
        logging.debug("New token: %s" % api.token)

        # Write out the token to the user's home directory
        f = open(rtm_auth_file, "w")
        f.write(api.token)
        f.close()

    # Get all incomplete tasks based on the constructed filter.
    # RTM filters: https://www.rememberthemilk.com/help/?ctx=basics.search.advanced
    filter = 'status:incomplete isSubtask:false'
    if rtm_list:
        filter = filter + ' list:"%s"' % rtm_list
    if rtm_tag:
        filter = filter + ' tag:"%s"' % rtm_tag
    if rtm_priority:
        filter = filter + ' priority:"%s"' % rtm_priority

    logging.debug("filter is now: " + filter)
    result = api.rtm.tasks.getList(filter="%s" % filter)

    list_of_tasks = []

    # Use the RtmAPI tasks iter to put the filtered set of tasks into a list
    for tasklist in result.tasks:
        for taskseries in tasklist:
            list_of_tasks.append(taskseries.name)

    # If the total # of retrieved tasks is zero, print mesg & exit
    if not list_of_tasks:
        print "\n\tSorry, but your filter didn't find any to dos."
        print "\tPerhaps re-check your spelling and try again.\n"
        exit(0)

    # Pick out a random task name
    random_task_name = random.choice(list_of_tasks)
    logging.debug("Random task name is: " + random_task_name)

    # Use the random task's name to retrieve its full info
    result = ""
    result = api.rtm.tasks.getList(filter='name:"%s"' % random_task_name)
    logging.debug("results.tasks has a type of: " + str(type(result.tasks)))

    # Use the RtmAPI iterators to drill down to the taskseries & task info.
    # To better understand what's going on here, you'll need to read the
    # RtmAPI docs as well as how Remember The Milk's API returns queries.
    first_tasklist = iter(result.tasks).next()
    logging.debug("tasklist has a type of: " + str(type(tasklist)))

    first_taskseries = iter(first_tasklist).next()
    logging.debug("first_taskseries  type is: " + str(type(first_taskseries)))

    spinner()  # Cosmetic only ;-)

    print "\nTask Name: \t", COLORAMA_STYLE, COLORAMA_BG, COLORAMA_FG, first_taskseries.name, Style.RESET_ALL

    if "N" in first_taskseries.task.priority:
        print 'Priority: \t', COLORAMA_STYLE, COLORAMA_BG, COLORAMA_FG, 'None', Style.RESET_ALL
    else:
        print 'Priority: \t', COLORAMA_STYLE, COLORAMA_BG, COLORAMA_FG, first_taskseries.task.priority, Style.RESET_ALL

    if first_taskseries.task.due == '':
        print 'Due: \t\t -'
    else:
        formatted_date = strftime(
            strptime(first_taskseries.task.due, "%Y-%m-%dT%H:%M:%SZ"),
            "%d %b %Y")
        print 'Due: \t\t', COLORAMA_STYLE, COLORAMA_BG, COLORAMA_FG, formatted_date, Style.RESET_ALL

    # As a bonus, print the # of tasks in the user's search filter
    print "\nPS: The total # of tasks with your search filter is: ", COLORAMA_STYLE, \
        COLORAMA_BG, COLORAMA_FG, len(list_of_tasks), Style.RESET_ALL, "\n"