Skip to content

Instantly share code, notes, and snippets.

@earlonrails
Forked from sssionggg/gist:168518
Last active August 29, 2015 14:16
Show Gist options
  • Save earlonrails/3fb7b86712f0b9ba2b86 to your computer and use it in GitHub Desktop.
Save earlonrails/3fb7b86712f0b9ba2b86 to your computer and use it in GitHub Desktop.

Revisions

  1. earlonrails renamed this gist Mar 4, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. earlonrails revised this gist Mar 4, 2015. 1 changed file with 0 additions and 70 deletions.
    70 changes: 0 additions & 70 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,7 @@
    #!/usr/bin/env ruby
    # -*- coding: utf-8 -*-

    require 'rubygems'

    require 'rufus/verbs' # gem rufus-verbs
    require 'json' # gem json or json_pure
    require 'growl' # gem growlnotifier (and rubycocoa)

    def get_password key
    result = `/usr/bin/security 2>&1 >/dev/null find-internet-password -gs #{key}`
    if result =~ /password: "(.*)"/
    $1
    else
    raise "Unknown password"
    end
    end

    # get password from keychain
    USER = 'user_name'
    PASS = get_password 'twitter.com'

    GROWL = Growl::Notifier.sharedInstance
    GROWL.register('twatch', ['twitter_update', 'issue'])

    class ObjectStash
    def self.store obj, file_name
    marshal_dump = Marshal.dump(obj)
    @@ -42,52 +21,3 @@ def self.load file_name
    end
    end
    end

    begin
    $last_seen_id = ObjectStash.load "#{ENV['HOME']}/growl.stash"
    rescue
    $last_seen_id = ''
    end

    def fetch_tweets
    if $last_seen_id == ''
    res = Rufus::Verbs.get(
    "https://twitter.com/statuses/friends_timeline.json",
    :hba => [ USER, PASS ]
    )
    else
    res = Rufus::Verbs.get(
    "https://twitter.com/statuses/friends_timeline.json?since_id=#{$last_seen_id}",
    :hba => [ USER, PASS ]
    )
    end

    raise "#{res.code} != 200" if res.code.to_i != 200

    json = JSON.parse(res.body)

    json.each_with_index do |message, i|
    # store the last id
    $last_seen_id = message['id'] if i == 0

    user = message['user']
    u = user['profile_image_url']
    u = OSX::NSURL.alloc.initWithString(u)
    i = OSX::NSImage.alloc.initWithContentsOfURL(u)

    GROWL.notify(
    'twitter_update',
    "#{user['name']} (#{user['screen_name']})",
    message['text'],
    :icon => i
    )
    end
    end

    begin
    fetch_tweets
    rescue Exception => e
    GROWL.notify('issue', 'twatch issue', e.to_s)
    end

    ObjectStash.store $last_seen_id, "#{ENV['HOME']}/growl.stash"
  3. siong1987 renamed this gist Aug 16, 2009. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. siong1987 revised this gist Aug 16, 2009. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -91,5 +91,3 @@ rescue Exception => e
    end

    ObjectStash.store $last_seen_id, "#{ENV['HOME']}/growl.stash"


  5. siong1987 created this gist Aug 16, 2009.
    95 changes: 95 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    #!/usr/bin/env ruby
    # -*- coding: utf-8 -*-

    require 'rubygems'

    require 'rufus/verbs' # gem rufus-verbs
    require 'json' # gem json or json_pure
    require 'growl' # gem growlnotifier (and rubycocoa)

    def get_password key
    result = `/usr/bin/security 2>&1 >/dev/null find-internet-password -gs #{key}`
    if result =~ /password: "(.*)"/
    $1
    else
    raise "Unknown password"
    end
    end

    # get password from keychain
    USER = 'user_name'
    PASS = get_password 'twitter.com'

    GROWL = Growl::Notifier.sharedInstance
    GROWL.register('twatch', ['twitter_update', 'issue'])

    class ObjectStash
    def self.store obj, file_name
    marshal_dump = Marshal.dump(obj)
    file = File.new(file_name,'w')
    file.write marshal_dump
    file.close
    return obj
    end

    def self.load file_name
    begin
    file = File.open(file_name, 'r')
    ensure
    obj = Marshal.load file.read
    file.close
    return obj
    end
    end
    end

    begin
    $last_seen_id = ObjectStash.load "#{ENV['HOME']}/growl.stash"
    rescue
    $last_seen_id = ''
    end

    def fetch_tweets
    if $last_seen_id == ''
    res = Rufus::Verbs.get(
    "https://twitter.com/statuses/friends_timeline.json",
    :hba => [ USER, PASS ]
    )
    else
    res = Rufus::Verbs.get(
    "https://twitter.com/statuses/friends_timeline.json?since_id=#{$last_seen_id}",
    :hba => [ USER, PASS ]
    )
    end

    raise "#{res.code} != 200" if res.code.to_i != 200

    json = JSON.parse(res.body)

    json.each_with_index do |message, i|
    # store the last id
    $last_seen_id = message['id'] if i == 0

    user = message['user']
    u = user['profile_image_url']
    u = OSX::NSURL.alloc.initWithString(u)
    i = OSX::NSImage.alloc.initWithContentsOfURL(u)

    GROWL.notify(
    'twitter_update',
    "#{user['name']} (#{user['screen_name']})",
    message['text'],
    :icon => i
    )
    end
    end

    begin
    fetch_tweets
    rescue Exception => e
    GROWL.notify('issue', 'twatch issue', e.to_s)
    end

    ObjectStash.store $last_seen_id, "#{ENV['HOME']}/growl.stash"