Posted By

gtcaz on 03/20/07


Tagged

e e-texteditor twitter


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

vali29
sergeizen
webstic


Check for new twitters in e/TextMate


 / Published in: Ruby
 

Requires the twitter bundle. This is an early attempt at updating this. Pretty crude at the moment, but it works.

  1. #!/usr/bin/env ruby
  2.  
  3. require 'rubygems'
  4. require 'twitter'
  5. require 'net/http'
  6. require 'fileutils'
  7. require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"
  8.  
  9. COCOA_DIALOG = ENV['TM_SUPPORT_PATH'] + "/bin/CocoaDialog"
  10. CACHEDIR = ENV['HOME'] + '/Library/Caches/TwitterMonitor'
  11. FileUtils.mkdir_p CACHEDIR unless File.exist?(CACHEDIR)
  12.  
  13. template = <<EOF
  14. # .twitter
  15. #
  16. # Please fill in fields like this:
  17. #
  18. # email: [email protected] (username also works)
  19. # password: secret
  20. #
  21. email:
  22. password:
  23. EOF
  24.  
  25.  
  26. # ensure config file for twitter gem exists
  27. begin
  28. config = YAML::load open(ENV['HOME'] + "/.twitter")
  29. rescue
  30. open(ENV["HOME"] + '/.twitter','w').write(template)
  31. config = YAML::load open(ENV['HOME'] + "/.twitter")
  32. end
  33.  
  34. # make sure there's actually account information in there
  35. if config == false or config.nil? or config['email'].nil? or config['password'].nil?
  36. res = %x{"#{COCOA_DIALOG}" ok-msgbox --no-newline --float \
  37. --text "Please edit ~/.twitter to include your twitter email and password" \
  38. --informative-text "Press OK to edit your ~/.twitter file in a new tab."}
  39. TextMate.exit_show_tool_tip("Cancelled!") if res == "2"
  40. `cygstart "txmt://open?url=file://#{ENV['HOME']}/.twitter"`
  41. end
  42.  
  43. # attempt to retrieve statuses from friend timeline
  44. begin
  45. statuses = Twitter::Base.new(config['email'], config['password']).timeline
  46. rescue
  47. success = false
  48. end
  49.  
  50. isEven = true
  51. bgStyle = ""
  52. statuses.select do |status|
  53. # ensure messages are suitable for display
  54. next if status.text.nil? || status.user.name.nil?
  55. status.text.gsub!(/"/, "'")
  56. #p status
  57.  
  58. # try to find the most likely icon for this user
  59. icon = Dir.glob("#{CACHEDIR}/#{status.user.screen_name}.{gif,jpeg,jpg,png}").first
  60.  
  61. # download and cache icons if nonexistant or over a month old
  62. unless icon and File.mtime(icon) > Time.now - 2629743.83
  63. image = Net::HTTP.get(URI.parse(status.user.profile_image_url))
  64. icon = CACHEDIR + "/" + status.user.screen_name + "." + status.user.profile_image_url.match(/\.(\w+)\?/)[1]
  65. icf = open(icon, "w")
  66. icf.write(image)
  67. icf.close
  68. end
  69. winicon = `cygpath -w #{icon}` # need to leave this out for TextMate.
  70. (isEven = !isEven) ? bgStyle = "background-color:#EEEEFF;" : bgStyle = ""
  71. puts "<p style='font-family:Helvetica,Arial,sans-serif; #{bgStyle}'><img src='file://#{winicon}' style='float:left' />#{status.user.name} (#{status.user.screen_name}):<br />#{status.text}</p>"
  72. end

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: rohitbhatia2007 on March 27, 2007

hello what up and what going wrong wrong

You need to login to post a comment.