1

I have written a working ruby script using mechanize for fetching my

  1. Stackoverflow reputation and badges
  2. Twitter tweets and followers

and printing the output to screen.

I run this command on the terminal first :

/Users/cyclotrojan/.rvm/rubies/ruby-1.9.2-p290/bin/ruby /Users/cyclotrojan/Desktop/Snippets/get_so_reputation_and_tw_followers.rb

The output is correctly displayed like this on the terminal:

StackOverflow :
    Reputation is : 406
    Total Badges  : 10
Twitter :
    Tweets     : 1,494
    Followers  : 137

Now I wish to display the output of my script on my desktop using GeekTools and set refresh time to 30 minutes, so it keeps running the script over and over thus updating my stats. However when I paste the same command on a geektool shell, no output comes.

Here is the script :

File get_so_reputation_and_tw_followers.rb

require 'mechanize'

def get_stackoverflow_data

  url_so = 'https://stackoverflow.com/users/898346/cyclotrojan'

  begin
    so_page = Mechanize.new.get(url_so)
    so_reputation = so_page.link_with(:href => "/users/898346/cyclotrojan?tab=reputation").to_s
    so_badges = so_page.search(".badgecount").first

    print "StackOverflow :" + "\n"
    print "    Reputation is : " + so_reputation + "\n"
    print "    Total Badges  : " + so_badges + "\n"
  rescue
    print "StackOverflow :" + "\n"
    print "\tReputation is : " + "NA" + "\n"
    print "\tTotal Badges  : " +  "NA" + "\n"
  end

end


def get_twitter_data

  url_tw = 'https://twitter.com/cyclotrojan'

  begin
    tw_page =  Mechanize.new.get(url_tw)
    tw_tweets = tw_page.link_with( :href => "/" + url_tw.split("/").last).to_s.split(" ").first
    tw_followers = tw_page.link_with( :href => "/#!/" + url_tw.split("/").last + "/followers").to_s.split(" ").first

    print "Twitter :" + "\n"
    print "\tTweets     : " + tw_tweets + "\n"
    print "\tFollowers  : " + tw_followers + "\n"
  rescue
    print "Twitter :" + "\n"
    print "\tTweets     : " + "NA" + "\n"
    print "\tFollowers  : " + "NA" + "\n"
  end

end # end function


  get_stackoverflow_data()
  get_twitter_data()

However, the output simply doesnt show up on my desktop. I know that the problem is not with detecting ruby, since I tested it out using another file test.rb

File test.rb

puts "hello"

This command will work on GeekTool shell and display hello correctly :

/Users/cyclotrojan/.rvm/rubies/ruby-1.9.2-p290/bin/ruby /Users/cyclotrojan/Desktop/Snippets/test.rb;

Output on geektool shell: hello

I have done some debugging and found that if I remove the line require 'mechanize' then the script runs correctly and due to exception the following output is displayed :

StackOverflow :
    Reputation is : NA
    Total Badges  : NA
Twitter :
    Tweets     : NA
    Followers  : NA

So the issue is with including mechanize in the script. Please help me in resolving this. Thnx !

These questions didnt help : How to run Ruby scripts in Geektool? , Ruby, Mac, Geektool question, file access rights?

4

1 回答 1

0

您将能够判断您是否没有尝试挽救错误。看起来我们的老朋友 ssl 验证错误,所以试试这个:

@agent = Mechanize.new
@agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
tw_page = @agent.get url_tw

我应该补充一点,您可能想确保您了解 OpenSSL::SSL::VERIFY_NONE 的后果

这里还有一个提示:

tweets = tw_page.at('a[@data-element-term="tweet_stats"] strong').text rescue 'N/A'
followers = tw_page.at('a[@data-element-term="follower_stats"] strong').text rescue 'N/A'
puts "Twitter :" + "\n"
puts "\tTweets     : #{tweets}"
puts "\tFollowers  : #{followers}"
于 2012-08-08T11:52:24.460 回答