Posted By

berkes on 07/03/08


Tagged

hack ruby xml Net GettingThingsDone GTD


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


Rough Vitalist to Rememberthemilk conversion


 / Published in: Ruby
 

URL: http://rememberthemilk.com

Hacked up Ruby script that downloads all the tasks from vitalist and mails them as new tasks to rememberthemilk.

NOTE: you need to change some values in the source to make this work. You also need to comment and uncomment some parts if you want to store stuff locally first, or use this as a one-time script.

  1. require 'net/http'
  2. require 'net/smtp'
  3. require 'rexml/document'
  4.  
  5. def send_email(from, from_alias, to, to_alias, subject, message)
  6. msg = <<END_OF_MESSAGE
  7. From: #{from_alias} <#{from}>
  8. To: #{to_alias} <#{to}>
  9. Subject: #{subject}
  10.  
  11. #{message}
  12. END_OF_MESSAGE
  13.  
  14. #Obviously change this into your own smtp server.
  15. Net::SMTP.start('smtp.chello.nl') do |smtp|
  16. smtp.send_message msg, from, to
  17. end
  18. end
  19.  
  20. def send_task_mail(body, projects, contexts , tags)
  21. from = '' #Where are we sending from, emailaddy?
  22. from_alias = '' #Our name
  23. #to = '[email protected]' #debugging
  24. #to_alias = 'Bèr Kessels'
  25. to = '' #Your RTM *single item* Inbox e-mailadress. Batchmails don't seem to accept tags etc.
  26. to_alias = 'RTM inbox'
  27.  
  28. projects.each { |project|
  29. tags << "+" + project.strip unless project.strip.empty? #Add a +-sign to the project. That way we have nice RTM tags for projects
  30. }
  31.  
  32. contexts.each { |context|
  33. tags << '@'+context.strip unless context.strip.empty? #Add a @-sign to the project. That way we have nice RTM tags for contexts
  34. }
  35.  
  36. #Now lets make a string from the tags.
  37. tags = tags.join(', ')
  38.  
  39. message = <<END_OF_MESSAGE
  40. Tags: #{tags}
  41. END_OF_MESSAGE
  42.  
  43. send_email(from, from_alias, to, to_alias, body, message)
  44. end
  45.  
  46.  
  47. # #Your Vitalist API key, find it a vitalist.com
  48. # key = ''
  49. #
  50. # #Web url for the XML
  51. # url = "http://my.vitalist.com/services/api/export/#{key}"
  52. #
  53. # # get the XML data as a string
  54. # xml_data = Net::HTTP.get_response(URI.parse(url)).body
  55. #
  56. # Because of debugging, I store the vitalist XML in a local file. Saves Bandwith and time.
  57. # my_file = File.new("/home/ber/tmp/vitalistdump.xml", APPEND)
  58. # my_file.puts xml_data
  59.  
  60. # In case we Don't store the data in a temporary file locally, then we use this to turn the XML string into a REMXL object.
  61. #doc = REXML::Document.new xml_data
  62.  
  63. # In case we read from a local file (see above) we turn that file into a REMXL object
  64. doc = REXML::Document.new File.new("/home/ber/tmp/vitalistdump.xml")
  65.  
  66. doc.elements.each("response/items/item") { |item|
  67. #First we see if this is not an already finished item
  68. archived = FALSE
  69. if (! item.elements["complete_date"].text.nil? ) then
  70. complete_date = item.elements["complete_date"].text.split('-')
  71. completed_at = Time.local(complete_date[0], complete_date[1], complete_date[2])
  72. now = Time.now
  73. if (completed_at < now) then
  74. archived = TRUE
  75. puts "skipping archived item #{item.elements['body'].text}, finished at #{completed_at.to_s} (< #{now.to_s})"
  76. end
  77. else if (archived == FALSE) then
  78. contexts = []
  79. body = item.elements['body'].text
  80.  
  81. item.elements.each("contexts/context") { |context|
  82. contexts << context.text
  83. }
  84.  
  85. if ( ! item.elements["project/body"].nil? ) then
  86. project = item.elements["project/body"].text
  87. else
  88. project = ''
  89. end
  90.  
  91. context_str = "\t" + contexts.join(', ')
  92.  
  93. puts send_task_mail(body, project, context_str, [])
  94.  
  95. #We don't want to a) hammer a server and b) be blacklisted as some spambot. So lets keep it nice and wait between 5 and 11 seconds between each mail.
  96. puts sleep(rand + rand(6) + 5)
  97. end end
  98. }

Report this snippet  

You need to login to post a comment.