ruby - 使用 GeekTools 在 MacOS 桌面上显示需要 Mechanize 的 ruby​​ 脚本的输出?

标签 ruby macos mechanize geektool

我已经使用 mechanize 编写了一个有效的 ruby​​ 脚本来获取我的

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

并将输出打印到屏幕上。
我首先在终端上运行这个命令:
/Users/cyclotrojan/.rvm/rubies/ruby-1.9.2-p290/bin/ruby /Users/cyclotrojan/Desktop/Snippets/get_so_reputation_and_tw_followers.rb
输出在终端上正确显示如下:
StackOverflow :
    Reputation is : 406
    Total Badges  : 10
Twitter :
    Tweets     : 1,494
    Followers  : 137
现在我希望使用 GeekTools 在我的桌面上显示我的脚本输出并将刷新时间设置为 30 分钟,这样它就会一遍又一遍地运行脚本,从而更新我的统计数据。但是,当我在 geektool shell 上粘贴相同的命令时,没有输出。
这是脚本:
文件 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()
但是,输出根本没有显示在我的桌面上。我知道问题不在于检测 ruby​​,因为我使用另一个文件 test.rb 对其进行了测试
文件test.rb
puts "hello"
此命令将在 GeekTool shell 上运行并正确显示 hello :
/Users/cyclotrojan/.rvm/rubies/ruby-1.9.2-p290/bin/ruby /Users/cyclotrojan/Desktop/Snippets/test.rb;
geektool shell 上的输出:
你好
我做了一些调试,发现如果我删除该行
需要“Mechanize ”
然后脚本正确运行,并且由于异常显示以下输出:
StackOverflow :
    Reputation is : NA
    Total Badges  : NA
Twitter :
    Tweets     : NA
    Followers  : NA
所以问题在于在脚本中包含 Mechanize 。请帮我解决这个问题。谢谢!
这些问题没有帮助:
How to run Ruby scripts in Geektool? ,
Ruby, Mac, Geektool question, file access rights?

最佳答案

如果您没有尝试挽救错误,您将能够判断出来。看起来我们的老 friend 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}"

关于ruby - 使用 GeekTools 在 MacOS 桌面上显示需要 Mechanize 的 ruby​​ 脚本的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11838098/

相关文章:

Python: Mechanize 无法找到所有表单输入

ruby - 无方法错误 : undefined method `q=' for nil:NilClass: Mechanize; google scraping

ruby-on-rails - 使用用户或管理员模型和 Basecamp 样式子域设计登录

ruby - 如何在不使用空格字符的情况下运行 unix 命令,以便执行远程命令?

ruby-on-rails - 验证 ActionCable 连接

ruby - 如何检查模型在 Rails 中是否有现有的模型关联?

linux - nbd-client 无法设置设备

macos - 如何使用 productbuild 构建具有自定义背景的 OS X 安装程序包?

c++ - dyld : Library not loaded:/usr/local/opt/jpeg/lib/libjpeg. 9.dylib OpenCV C++ MacOS

python - libgmail login() 运行时出错