Return to Snippet

Revision: 2651
at April 7, 2007 23:53 by gtcaz


Updated Code
#!/usr/bin/env ruby

require 'rubygems'
require 'twitter'
require 'net/http'
require 'fileutils'
require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"

COCOA_DIALOG = ENV['TM_SUPPORT_PATH'] + "/bin/CocoaDialog"
CACHEDIR = ENV['HOME'] + '/Library/Caches/TwitterMonitor'
FileUtils.mkdir_p CACHEDIR unless File.exist?(CACHEDIR)

template = <<EOF
# .twitter
# 
# Please fill in fields like this:
#
#  email: [email protected] (username also works)
#  password: secret
#
email: 
password: 
EOF


# ensure config file for twitter gem exists
begin
  config = YAML::load open(ENV['HOME'] + "/.twitter")
rescue
  open(ENV["HOME"] + '/.twitter','w').write(template)
  config = YAML::load open(ENV['HOME'] + "/.twitter")
end

# make sure there's actually account information in there
if config == false or config.nil? or config['email'].nil? or config['password'].nil?
  res = %x{"#{COCOA_DIALOG}" ok-msgbox --no-newline --float \
  --text "Please edit ~/.twitter to include your twitter email and password" \
  --informative-text "Press OK to edit your ~/.twitter file in a new tab."}
  TextMate.exit_show_tool_tip("Cancelled!") if res == "2"
  `cygstart "txmt://open?url=file://#{ENV['HOME']}/.twitter"`
end

# attempt to retrieve statuses from friend timeline
begin
  statuses = Twitter::Base.new(config['email'], config['password']).timeline
rescue
  success = false
end

isEven = true
bgStyle = ""
statuses.select do |status|
  # ensure messages are suitable for display
  next if status.text.nil? || status.user.name.nil?
  status.text.gsub!(/"/, "'")
  #p status
  
  # try to find the most likely icon for this user
  icon = Dir.glob("#{CACHEDIR}/#{status.user.screen_name}.{gif,jpeg,jpg,png}").first
  
  # download and cache icons if nonexistant or over a month old
  unless icon and File.mtime(icon) > Time.now - 2629743.83
    image = Net::HTTP.get(URI.parse(status.user.profile_image_url))
    icon = CACHEDIR + "/" + status.user.screen_name + "." + status.user.profile_image_url.match(/\.(\w+)\?/)[1]
    icf = open(icon, "w")
    icf.write(image)
    icf.close
  end
  winicon = `cygpath -w #{icon}` # need to leave this out for TextMate.
  (isEven = !isEven) ? bgStyle = "background-color:#EEEEFF;" : bgStyle = ""
  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>"
end

Revision: 2650
at April 7, 2007 00:40 by gtcaz


Updated Code
#!/usr/bin/env ruby

require 'rubygems'
require 'twitter'
require 'net/http'
require 'fileutils'
require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"

COCOA_DIALOG = ENV['TM_SUPPORT_PATH'] + "/bin/CocoaDialog"
CACHEDIR = ENV['HOME'] + '/Library/Caches/TwitterMonitor'
FileUtils.mkdir_p CACHEDIR unless File.exist?(CACHEDIR)

template = <<EOF
# .twitter
# 
# Please fill in fields like this:
#
#  email: [email protected] (username also works)
#  password: secret
#
email: 
password: 
EOF


# ensure config file for twitter gem exists
begin
  config = YAML::load open(ENV['HOME'] + "/.twitter")
rescue
  open(ENV["HOME"] + '/.twitter','w').write(template)
  config = YAML::load open(ENV['HOME'] + "/.twitter")
end

# make sure there's actually account information in there
if config == false or config.nil? or config['email'].nil? or config['password'].nil?
  res = %x{"#{COCOA_DIALOG}" ok-msgbox --no-newline --float \
  --text "Please edit ~/.twitter to include your twitter email and password" \
  --informative-text "Press OK to edit your ~/.twitter file in a new tab."}
  TextMate.exit_show_tool_tip("Cancelled!") if res == "2"
  `cygstart "txmt://open?url=file://#{ENV['HOME']}/.twitter"`
end

# attempt to retrieve statuses from friend timeline
begin
  statuses = Twitter::Base.new(config['email'], config['password']).timeline
rescue
  success = false
end

isEven = true
bgStyle = ""
statuses.select do |status|
  # ensure messages are suitable for display
  next if status.text.nil? || status.user.name.nil?
  status.text.gsub!(/"/, "'")
  #p status
  
  # try to find the most likely icon for this user
  icon = Dir.glob("#{CACHEDIR}/#{status.user.screen_name}.{gif,jpeg,jpg,png}").first
  
  # download and cache icons if nonexistant or over a month old
  unless icon and File.mtime(icon) > Time.now - 2629743.83
    image = Net::HTTP.get(URI.parse(status.user.profile_image_url))
    icon = CACHEDIR + "/" + status.user.screen_name + "." + status.user.profile_image_url.match(/\.(\w+)\?/)[1]
    icf = open(icon, "w")
    icf.write(image)
    icf.close
  end
  winicon = `cygpath -w #{icon}` # need to leave this out for TextMate.
  (isEven = !isEven) ? bgStyle = "background-color:#EEEEFF;" : bgStyle = ""
  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>"
end

Revision: 2649
at March 20, 2007 01:11 by gtcaz


Initial Code
#!/usr/bin/env ruby
# requires JSON ruby gem ('gem install json')
require 'rubygems'
require 'json'
require 'open-uri'
require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"

user = "username"
password = "password"
timefile = ENV["HOME"]+"/.twittercheck-date"
url = 'http://twitter.com/statuses/friends_timeline.json'

begin
    last = Time.parse(open(timefile).read)
rescue
    last = Time.now
end

output = []
data = JSON.parse(open(url, :http_basic_authentication=>[user, password]).read)
for twitter in data.select { |d| Time.parse(d["created_at"]) > last } do
    title = twitter['user']['name']
    msg = twitter['text']
    output.push("<i>#{title}:</i> #{msg}")
end

open(timefile,"w").write(Time.now.to_s)

TextMate.exit_show_tool_tip("No new twitters found.") if output.empty?
puts "<p>"
output.each {|line| puts "  #{line}<br />"}
puts "</p>"
TextMate.exit_show_html

Initial URL


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

Initial Title
Check for new twitters in e/TextMate

Initial Tags
twitter

Initial Language
Ruby