Guest User

Untitled

a guest
Jul 15th, 2018
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. RTM Task emailer
  4.  
  5. Emails all current incomplete RTM tasks. It uses a gmail account to
  6. send the email.
  7.  
  8. SETUP:
  9. Edit RTM_USERNAME, USERNAME, PASSWORD below
  10. The first time you run the script, run it with the "auth" argument
  11.  
  12. $ ./rtm-emailer.py auth
  13.  
  14. The script will print a URL, which you must visit to authenticate this
  15. script. The RTM website will provide you with a frob value, which you
  16. must paste here.
  17.  
  18. Subsequent executions of the script will no longer need the "auth"
  19. argument and can run non-interactively. Use cron to get daily emails.
  20.  
  21. Debug information is printed to stderr, when you are logging to a file,
  22. redirect stderr to /dev/null.
  23.  
  24. $ ./rtm-emailer.py 2>/dev/null
  25. """
  26. import rtmapi
  27. import smtplib
  28. import sys
  29. import time
  30.  
  31. #rtm api settings
  32. API_KEY = '88cb5c01a7f2e7a5b6662101437f8d21'
  33. SHARED_SECRET = '3727dc15923e7a5f'
  34. RTM_USERNAME = 'rtmusername'
  35.  
  36. #gmail settings
  37. USERNAME = 'name@gmail.com'
  38. PASSWORD = 'password'
  39. FROM_ADDR = USERNAME
  40. TO_ADDR = USERNAME
  41. HOSTNAME = 'smtp.gmail.com'
  42.  
  43. def get_tasks(rtm):
  44. response = rtm.tasks.getList(filter='status:incomplete')
  45.  
  46. # multiple lists can be returned, iterate through each list
  47. # and then iterate through all the tasks in each list
  48. tasks = []
  49. for rtm_list in response.find('tasks').findall('list'):
  50. for task in rtm_list.findall('taskseries'):
  51. tasks.append(task.attrib['name'])
  52. return tasks
  53.  
  54. def send_rtm_tasks_email(body):
  55. server = smtplib.SMTP(HOSTNAME, 587)
  56. server.set_debuglevel(1)
  57. server.ehlo()
  58. server.starttls()
  59. try:
  60. server.login(USERNAME, PASSWORD)
  61. except smtplib.SMTPAuthenticationError:
  62. log('Gmail authentication problem. Please check username/password.')
  63.  
  64. headers = ['From: %s' % FROM_ADDR,
  65. 'To: %s' % TO_ADDR,
  66. 'MIME-Version: 1.0',
  67. 'Content-type: text/plain',
  68. 'Subject: RTM tasks']
  69. message = '\r\n'.join(headers).encode('utf-8') + '\r\n' + body
  70. server.sendmail(FROM_ADDR, TO_ADDR, message)
  71. server.quit()
  72.  
  73. def log(output):
  74. print time.strftime('[%H:%M] %m/%d/%y: ') + output
  75.  
  76.  
  77. if __name__ == '__main__':
  78. rtm = rtmapi.RtmAPI(API_KEY, SHARED_SECRET, username=RTM_USERNAME)
  79.  
  80. # If "auth" is passed as an argument, set up application access.
  81. if len(sys.argv) > 1 and sys.argv[1] == 'auth':
  82. print 'Visit this url and authenticate this script. Then copy the frob token here.'
  83. print rtm.web_login_url('read')
  84. frob = raw_input('frob > ')
  85. #will cache the token on disk
  86. rtm.get_token(frob)
  87.  
  88. try:
  89. tasks = get_tasks(rtm)
  90. except rtmapi.exceptions.RtmError:
  91. log('Authentication error. Please run the script manually with "auth" as an argument.')
  92. sys.exit()
  93.  
  94. send_rtm_tasks_email('\r\n'.join(tasks))
  95. log('Emailed %d tasks to %s.' % (len(tasks), TO_ADDR))
Add Comment
Please, Sign In to add comment