Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- RTM Task emailer
- Emails all current incomplete RTM tasks. It uses a gmail account to
- send the email.
- SETUP:
- Edit RTM_USERNAME, USERNAME, PASSWORD below
- The first time you run the script, run it with the "auth" argument
- $ ./rtm-emailer.py auth
- The script will print a URL, which you must visit to authenticate this
- script. The RTM website will provide you with a frob value, which you
- must paste here.
- Subsequent executions of the script will no longer need the "auth"
- argument and can run non-interactively. Use cron to get daily emails.
- Debug information is printed to stderr, when you are logging to a file,
- redirect stderr to /dev/null.
- $ ./rtm-emailer.py 2>/dev/null
- """
- import rtmapi
- import smtplib
- import sys
- import time
- #rtm api settings
- API_KEY = '88cb5c01a7f2e7a5b6662101437f8d21'
- SHARED_SECRET = '3727dc15923e7a5f'
- RTM_USERNAME = 'rtmusername'
- #gmail settings
- USERNAME = 'name@gmail.com'
- PASSWORD = 'password'
- FROM_ADDR = USERNAME
- TO_ADDR = USERNAME
- HOSTNAME = 'smtp.gmail.com'
- def get_tasks(rtm):
- response = rtm.tasks.getList(filter='status:incomplete')
- # multiple lists can be returned, iterate through each list
- # and then iterate through all the tasks in each list
- tasks = []
- for rtm_list in response.find('tasks').findall('list'):
- for task in rtm_list.findall('taskseries'):
- tasks.append(task.attrib['name'])
- return tasks
- def send_rtm_tasks_email(body):
- server = smtplib.SMTP(HOSTNAME, 587)
- server.set_debuglevel(1)
- server.ehlo()
- server.starttls()
- try:
- server.login(USERNAME, PASSWORD)
- except smtplib.SMTPAuthenticationError:
- log('Gmail authentication problem. Please check username/password.')
- headers = ['From: %s' % FROM_ADDR,
- 'To: %s' % TO_ADDR,
- 'MIME-Version: 1.0',
- 'Content-type: text/plain',
- 'Subject: RTM tasks']
- message = '\r\n'.join(headers).encode('utf-8') + '\r\n' + body
- server.sendmail(FROM_ADDR, TO_ADDR, message)
- server.quit()
- def log(output):
- print time.strftime('[%H:%M] %m/%d/%y: ') + output
- if __name__ == '__main__':
- rtm = rtmapi.RtmAPI(API_KEY, SHARED_SECRET, username=RTM_USERNAME)
- # If "auth" is passed as an argument, set up application access.
- if len(sys.argv) > 1 and sys.argv[1] == 'auth':
- print 'Visit this url and authenticate this script. Then copy the frob token here.'
- print rtm.web_login_url('read')
- frob = raw_input('frob > ')
- #will cache the token on disk
- rtm.get_token(frob)
- try:
- tasks = get_tasks(rtm)
- except rtmapi.exceptions.RtmError:
- log('Authentication error. Please run the script manually with "auth" as an argument.')
- sys.exit()
- send_rtm_tasks_email('\r\n'.join(tasks))
- log('Emailed %d tasks to %s.' % (len(tasks), TO_ADDR))
Add Comment
Please, Sign In to add comment