Created
November 12, 2012 19:03
-
-
Save ChewingPencils/4061198 to your computer and use it in GitHub Desktop.
checkvist_inbox.py
Send Task to Checkvist (Fork of MacDrifter) #macdrifter #python #pythonista #api
This file contains hidden or 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
#!/usr/bin/env python | |
# | |
# checkvist_inbox.py | |
# Sean Korzdorfer | |
# Mon Nov 12 2012 | |
# | |
# A Modified version of Gabe Weatherhead's pyhton script which uses checkvist's | |
# import api parameter. | |
# http://www.macdrifter.com/2012/11/the-checkvist-inbox-and-some-api-scripts.html | |
# | |
# | |
# This fork creates a single task with the options of adding a note and tags. | |
# | |
# Form: | |
# this is the text for the new task -n this is the note text -t tag1 tag-2 tag_3 | |
# | |
# Options: | |
# -n, --note : Note Text | |
# -t, --tag : Task tags. Tags are seperated by spaces. | |
# -v, --version : Script version | |
# -h, --help : script help | |
# | |
# | |
#### Configuration #### | |
# | |
# Please add the following values below | |
# user_id : Your email address | |
# api_key : Found on your profile page: http://checkvist.com/auth/profile#status | |
# inbox_list_id : The list you want to add tasks to. Look at the URL for the id | |
# http://checkvist.com/checklists/<this is the id number> | |
# | |
#### Example Launch Center Pro #### | |
# | |
# You can make everything one prompt and use the CLI | |
# | |
# pythonista://inbox_list?action=run&args=[prompt] | |
# | |
# You can make multiple prompts: | |
# | |
# pythonista://inbox_list?action=run&args=[prompt]%20-n%20[prompt]%20-t%20[prompt] | |
# pythonista://inbox_list?action=run&args=[prompt]%20-n%20[clipboard]%20-t%20[prompt] | |
#### Change Log #### | |
# Version 1 - 2012-11-12_112332 | |
import argparse, requests, sys, json | |
def main(args): | |
user_id = '' # Add value | |
api_key = '' # Add value | |
inbox_list_id = '' # Add value | |
auth_url = 'https://checkvist.com/auth/login.json' | |
print 'Command Line Parsed As:\n\t' | |
print args | |
args['list_id'] = inbox_list_id | |
r = requests.post(auth_url, {'username':user_id, 'remote_key':api_key}) | |
# Get token and strip quotes | |
args['auth_token'] = r.text.replace('"', '').strip() | |
task_res = sendTask(args) | |
print '\nHTTP Response From Task Post Request:\n\t%s' % task_res | |
if 'task_comment' in args: | |
sendComment(args, task_res) | |
def sendTask(args): | |
task_url = 'http://checkvist.com/checklists/' + args['list_id'] + '/tasks.json' | |
print task_url | |
if 'task_tag' in args: | |
payload = {'token': args['auth_token'], 'task[content]': args['task_text'], 'task[tags]': args['task_tag']} | |
else: | |
payload = {'token': args['auth_token'], 'task[content]': args['task_text']} | |
# Make post Request to Checkvist API | |
r = requests.post(task_url, data=payload) | |
print r.text | |
return r.text | |
def sendComment(args, response): | |
parsed_json = json.loads(response) | |
task_id = parsed_json['id'] | |
# If I use https, requests sends as GET. Odd. | |
comment_url = 'http://checkvist.com/checklists/' + args['list_id'] + '/tasks/' + str(task_id) + '/comments.json' | |
payload = {'token': args['auth_token'], 'comment[comment]': args['task_comment']} | |
r = requests.post(comment_url, data=payload) | |
print '\nHTTP Response From Task Note Request:\n\t%s' % r.text | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('task_text', metavar='N', nargs='+', | |
help='The text that will appear in the task field') | |
parser.add_argument('-n', '--note', action='store', dest='task_comment', nargs='+', | |
help='The text which will appear in the task note') | |
parser.add_argument('-t', '--tag', action='store', dest='task_tag', nargs='+', | |
help='Task tags are separated by spaces. This means they must be one word (hyphens and underscores allowed)') | |
parser.add_argument('-v','--version', action='version', version='%(prog)s 1.') | |
results = parser.parse_args() | |
# Will raise traceback error if missing | |
if results.task_text > 0: | |
args = {'task_text': ' '.join(results.task_text)} | |
# There is a task note, add it to the dictionary | |
if results.task_comment: | |
args['task_comment'] = ' '.join(results.task_comment) | |
# If there is a task tag, add it to the dictionary | |
if results.task_tag: | |
args['task_tag'] = ','.join(results.task_tag) | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment