This script fails to compile, but why? - ruby

This ruby script does not run. It responds with
/Users/superhappyfuntime/Desktop/twt.rb:25: undefined local variable or method `xsFEEyGKDPcnhJ5JoPJKg' for main:Object (NameError)
from /Library/Ruby/Gems/1.8/gems/twitter-1.7.2/lib/twitter/configuration.rb:80:in `configure'
from /Users/superhappyfuntime/Desktop/twt.rb:24
This is after the user inputs.
How do I fix it? Also, if it helps, here is the script:
#!/usr/bin/ruby
require "rubygems"
require "twitter"
puts "Welcome! TwitNIX (A.K.A. 'clt') posts to Twitter from the command line!"
puts "your token, please:"
please = gets.chomp
puts "...aaand your secret_token:"
secret_token = gets.chomp
puts "You're Done!"
Twitter.configure do |config|
config.consumer_key = xsFEEyGKDPcnhJ5JoPJKg
config.consumer_secret = **CENSORED**
config.oauth_token = please
config.oauth_token_secret = secret_token
end
client = Twitter::Client.new
puts "Now, post an update:"
update = gets
client.update("update")
puts "Now, post an update:"
update = gets
client.update("update")
Once this is fixed, my pogram should be done. PLEASE HELP!!
Update: Now it shows:
/Library/Ruby/Gems/1.8/gems/twitter-1.7.2/lib/faraday/response/raise_http_4xx.rb:12:in `on_complete': POST https://api.twitter.com/1/statuses/update.json: 401: Read-only application cannot POST (Twitter::Unauthorized)
from /Library/Ruby/Gems/1.8/gems/faraday-0.7.5/lib/faraday/response.rb:9:in `call'
from /Library/Ruby/Gems/1.8/gems/faraday-0.7.5/lib/faraday/response.rb:62:in `on_complete'
from /Library/Ruby/Gems/1.8/gems/faraday-0.7.5/lib/faraday/response.rb:8:in `call'
from /Library/Ruby/Gems/1.8/gems/faraday-0.7.5/lib/faraday/request/url_encoded.rb:14:in `call'
from /Library/Ruby/Gems/1.8/gems/faraday-0.7.5/lib/faraday/request/multipart.rb:13:in `call'
from /Library/Ruby/Gems/1.8/gems/twitter-1.7.2/lib/faraday/request/twitter_oauth.rb:17:in `call'
from /Library/Ruby/Gems/1.8/gems/twitter-1.7.2/lib/faraday/request/multipart_with_file.rb:18:in `call'
from /Library/Ruby/Gems/1.8/gems/faraday-0.7.5/lib/faraday/connection.rb:207:in `run_request'
from /Library/Ruby/Gems/1.8/gems/faraday-0.7.5/lib/faraday/connection.rb:94:in `post'
from /Library/Ruby/Gems/1.8/gems/twitter-1.7.2/lib/twitter/request.rb:27:in `send'
from /Library/Ruby/Gems/1.8/gems/twitter-1.7.2/lib/twitter/request.rb:27:in `request'
from /Library/Ruby/Gems/1.8/gems/twitter-1.7.2/lib/twitter/request.rb:10:in `post'
from /Library/Ruby/Gems/1.8/gems/twitter-1.7.2/lib/twitter/client/tweets.rb:45:in `update'
from /Users/superhappyfuntime/Desktop/twt.rb:40

add '' single quotes to strings
Twitter.configure do |config|
config.consumer_key = 'xsFEEyGKDPcnhJ5JoPJKg'
config.consumer_secret = '**CENSORED**'

Better you can reset your twitter consumer key and secret.
www.ning.com/help/?p=5551

Related

Tmdb::InvalidApiKeyError (Tmdb::InvalidApiKeyError)

When I am using Kiba ELT, I have followed the tutorials in YouTube as well as the tutorials provided by the owner. Yet, I am getting this error:
bitlasoft#Bitlasoft-TS-22:~/test01$ bundle exec kiba movies.etl
{
"title: Blade Runner" => "title: Minority Report"
}
/home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/themoviedb-1.0.1/lib/themoviedb/api.rb:37:in `set_response': Tmdb::InvalidApiKeyError (Tmdb::InvalidApiKeyError)
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/themoviedb-1.0.1/lib/themoviedb/search.rb:75:in `fetch_response'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/themoviedb-1.0.1/lib/themoviedb/search.rb:60:in `fetch'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/themoviedb-1.0.1/lib/themoviedb/resource.rb:41:in `search'
from /home/bitlasoft/test01/common.rb:30:in `process'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/kiba-0.6.1/lib/kiba/runner.rb:35:in `block (3 levels) in process_rows'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/kiba-0.6.1/lib/kiba/runner.rb:34:in `each'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/kiba-0.6.1/lib/kiba/runner.rb:34:in `block (2 levels) in process_rows'
from /home/bitlasoft/test01/common.rb:12:in `block in each'
from /home/bitlasoft/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/csv.rb:1739:in `each'
from /home/bitlasoft/test01/common.rb:11:in `each'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/kiba-0.6.1/lib/kiba/runner.rb:33:in `block in process_rows'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/kiba-0.6.1/lib/kiba/runner.rb:32:in `each'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/kiba-0.6.1/lib/kiba/runner.rb:32:in `process_rows'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/kiba-0.6.1/lib/kiba/runner.rb:13:in `run'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/kiba-0.6.1/lib/kiba/cli.rb:13:in `run'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/gems/kiba-0.6.1/bin/kiba:5:in `<top (required)>'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/bin/kiba:23:in `load'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/bin/kiba:23:in `<main>'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
from /home/bitlasoft/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'
bitlasoft#Bitlasoft-TS-22:~/test01$
Here is my movies.etl and common.rb config:
require_relative 'common'
api_key = IO.read('.themoviedb')
source CSVSource, filename: 'movies.csv'
limit ENV['LIMIT']
show_me!
transform MovieDBlookup,
api_key: api_key,
title_field: 'title'
show_me!
require 'csv'
require 'awesome_print'
class CSVSource
def initialize(filename:)
#filename = filename
end
def each
csv = CSV.open(#filename, headers: true)
csv.each do |row|
yield(row.to_hash)
end
csv.close
end
end
require 'themoviedb'
class MovieDBlookup
def initialize(api_key:, title_field:)
#title_field = title_field
Tmdb::Api.key(api_key)
end
def process(row)
movie = Tmdb::Movie.find(row[#title_field]).first
row[:vote_average] = movie.vote_average
row[:vote_count] = movie.vote_count
row
end
end
def show_me!
transform do |row|
ap row
row
end
end
def limit(x)
x = Integer(x || -1)
return if x == -1
transform do |row|
#counter ||= 0
#counter += 1
abort("stopping....")if #counter >= x
row
end
end
(Kiba owner here) - the error you get isn't Kiba specific ; it looks like the API key you provided for themoviedb is invalid. Did you sign-up for the movie db here, and is the API key provided copy-pasted into the file you are loading (.themoviedb)?
One problem I could think of would be that you'd have a line ending character in that file (carriage return / line feed), in which case maybe just calling api_key = IO.read('.themoviedb').strip could help.
Again, this isn't Kiba specific, but hope this helps!

Ruby NoMethodError // Telegram Bot

I'm using this Ruby code for running Telegram messenger bot. After code is runned with /start command, it crashes with the following error:
./bot.rb:26:in `block (3 levels) in <main>': undefined method `[]' for nil:NilClass (NoMethodError)
from ./bot.rb:20:in `loop'
from ./bot.rb:20:in `block (2 levels) in <main>'
from /var/lib/gems/2.1.0/gems/telegram-bot-ruby-0.3.11/lib/telegram/bot/client.rb:41:in `block in fetch_updates'
from /var/lib/gems/2.1.0/gems/telegram-bot-ruby-0.3.11/lib/telegram/bot/client.rb:37:in `each'
from /var/lib/gems/2.1.0/gems/telegram-bot-ruby-0.3.11/lib/telegram/bot/client.rb:37:in `fetch_updates'
from /var/lib/gems/2.1.0/gems/telegram-bot-ruby-0.3.11/lib/telegram/bot/client.rb:29:in `listen'
from ./bot.rb:17:in `block in <main>'
from /var/lib/gems/2.1.0/gems/telegram-bot-ruby-0.3.11/lib/telegram/bot/client.rb:22:in `run'
from /var/lib/gems/2.1.0/gems/telegram-bot-ruby-0.3.11/lib/telegram/bot/client.rb:10:in `run'
from ./bot.rb:16:in `<main>'
Bot code is here:
require 'telegram/bot'
token = '...'
require 'net/http'
url = URI.parse('http://api.wotblitz.ru/wotb/clans/info/?application_id=...&clan_id=8')
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
require 'json'
str = JSON.parse(res.body)
ids = str["data"]["8"]["members_ids"]
n = 0
m = ids.size - 1
players = ''
Telegram::Bot::Client.run(token) do |bot|
bot.listen do |message|
case message.text
when '/start'
loop do
url = URI.parse('http://api.wotblitz.ru/wotb/account/info/?application_id=...&account_id=' + ids[n].to_s)
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req)
}
str1 = JSON.parse(res.body)
nick = str1["data"][ids[n].to_s]["nickname"]
players = players + nick + "\n"
n += 1
break if n == m
end
bot.api.sendMessage(chat_id: message.chat.id, text:"#{players}")
end
end
end
If anyone is facing this same issue, I found a hacky fix for it. When running this code from my macOS terminal, I get the undefined method 'run' message. When I run it inside my vscode terminal on the other hand, it works correctly. I had this same issue on a Linux machine aswell. It probably has something to do with how my environment is setup.
Hope this helps someone out.

`block in non_options': file not found: (ArgumentError)

I'm trying to open browser url based on argument passed to script. Hence I wrote following ruby code:
require 'selenium-webdriver'
require 'test/unit'
class TestTitle < Test::Unit::TestCase
def setup
$driver = Selenium::WebDriver.for :firefox
if ARGV[0] == 'google'
$driver.get 'http://www.google.com'
elsif ARGV[0] == 'twitter'
$driver.get 'http://www.twitter.com'
end
end
def test_title
puts $driver.title
end
def teardown
$driver.quit
end
end
When I passed argument: ruby test.rb 'google', it results into following error:
c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: google (ArgumentError)
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
from c:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
from c:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:21:in `run'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
Please help me understand what I'm doing wrong.
It appears that test-unit (as of 1.9.1) grabs command line options in its GlobOptions module. You are using ARGV[0] to pass browser name, but it thinks you're passing a file name. A workaround is to capture the value of ARGV[0] and then clear it before your test case runs:
browser = ARGV[0]
ARGV[0] = nil

Storing tweets using tweetstream in MongoDB fails with following error

I have just started to learn ruby and concept of mongodb. This is the script that I am trying to run
require 'rubygems'
require 'tweetstream'
require 'mongo'
TweetStream.configure do |config|
config.consumer_key = '<key>'
config.consumer_secret = '<secret>'
config.oauth_token = '<token>'
config.oauth_token_secret = '<token_secret'
config.auth_method = :oauth
end
connection = Mongo::Connection.new
db = connection.db("tweetsDB")
tweets = db.collection("tweets")
client = TweetStream::Client.new
client.on_error do |message|
puts message
end
client.follow(<user_id>,<user_id>) do |status|
id = tweets.insert(status, :safe => true)
end
NOTE: I have removed all the static private values with in the script above for this post.
Version of Mongo, bson, bson_ext - 1.7.0
error message
NoMethodError: undefined method `has_key?' for #<Twitter::Tweet:0x7f21cd14cf08>
/var/lib/gems/1.8/gems/bson-1.7.0/lib/bson/types/object_id.rb:93:in `create_pk'
/var/lib/gems/1.8/gems/mongo-1.7.0/lib/mongo/collection.rb:360:in `insert'
/var/lib/gems/1.8/gems/mongo-1.7.0/lib/mongo/collection.rb:360:in `collect!'
/var/lib/gems/1.8/gems/mongo-1.7.0/lib/mongo/collection.rb:360:in `insert'
tracker.rb:28
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:525:in `call'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:525:in `invoke_callback'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:533:in `yield_message_to'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:471:in `respond_to'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:411:in `connect'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:296:in `call'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:296:in `invoke_callback'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:143:in `handle_stream'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:193:in `on_body'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:192:in `each'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:192:in `on_body'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:74:in `<<'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:74:in `receive_data'
/var/lib/gems/1.8/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
/var/lib/gems/1.8/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:385:in `start'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:128:in `filter'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:106:in `follow'
tracker.rb:27
If you want to blindly dump everything into MongoDB then you can just do a :
client.follow(<user_id>,<user_id>) do |status|
data = status.to_hash
id = tweets.insert(data)
end
If you want to be a bit more selective then you can try something like :
# only add the following fields to the database
ONLY = %w{created_at text geo coordinate id_str}
client.follow(<user_id>,<user_id>) do |status|
data = status.to_hash.select{|k,v| ONLY.include?(k.to_s)}
id = tweets.insert(data)
end
Or :
# add everything except the following fields to the database
EXCEPT = %w{entities}
client.follow(<user_id>,<user_id>) do |status|
data = status.to_hash.reject{|k,v| EXCEPT.include?(k.to_s)}
id = tweets.insert(data)
end
It seems looking at the docs that Client.follow just returns a generic ruby object. You have to take the relevant fields from the object and pull them out into json (or, more advisable, a mongoid object) to send to the database.
has_key? is a method available in the Hash superclass. What it's trying to do is make a json string to pass to Mongo. Except that you are passing a string, has_key? is not part of the String class. Just convert it to a hash and you should be good to go.
client.follow(<user_id>,<user_id>) do |status|
id = tweets.insert({:status => status.text}, :safe => true)
end

Ruby Net::HTTP time out

I'm trying to write my first Ruby program, but have a problem. The code has to download 32 MP3 files over HTTP. It actually downloads a few, then times-out.
I tried setting a timeout period, but it makes no difference. Running the code under Windows, Cygwin and Mac OS X has the same result.
This is the code:
require 'rubygems'
require 'open-uri'
require 'nokogiri'
require 'set'
require 'net/http'
require 'uri'
puts "\n Up and running!\n\n"
links_set = {}
pages = ['http://www.vimeo.com/siai/videos/sort:oldest',
'http://www.vimeo.com/siai/videos/page:2/sort:oldest',
'http://www.vimeo.com/siai/videos/page:3/sort:oldest']
pages.each do |page|
doc = Nokogiri::HTML(open(page))
doc.search('//*[#href]').each do |m|
video_id = m[:href]
if video_id.match(/^\/(\d+)$/i)
links_set[video_id[/\d+/]] = m.children[0].to_s.split(" at ")[0].split(" -- ")[0]
end
end
end
links = links_set.to_a
p links
cookie = ''
file_name = ''
open("http://www.tubeminator.com") {|f|
cookie = f.meta['set-cookie'].split(';')[0]
}
links.each do |link|
open("http://www.tubeminator.com/ajax.php?function=downloadvideo&url=http%3A%2F%2Fwww.vimeo.com%2F" + link[0],
"Cookie" => cookie) {|f|
puts f.read
}
open("http://www.tubeminator.com/ajax.php?function=convertvideo&start=0&duration=1120&size=0&format=mp3&vq=high&aq=high",
"Cookie" => cookie) {|f|
file_name = f.read
}
puts file_name
Net::HTTP.start("www.tubeminator.com") { |http|
#http.read_timeout = 3600 # 1 hour
resp = http.get("/download-video-" + file_name)
open(link[1] + ".mp3", "wb") { |file|
file.write(resp.body)
}
}
end
puts "\n Yay!!"
And this is the exception:
/Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/protocol.rb:140:in `rescue in rbuf_fill': Timeout::Error (Timeout::Error)
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/protocol.rb:134:in `rbuf_fill'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/protocol.rb:126:in `readline'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/http.rb:2138:in `read_status_line'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/http.rb:2127:in `read_new'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/http.rb:1120:in `transport_request'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/http.rb:1106:in `request'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:312:in `block in open_http'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/http.rb:564:in `start'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:306:in `open_http'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:767:in `buffer_open'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:201:in `catch'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:669:in `open'
from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:33:in `open'
from test.rb:38:in `block in <main>'
from test.rb:37:in `each'
from test.rb:37:in `<main>'
I'd also appreciate your comments on the rest of the code.
For Ruby 1.8 I used this to solve my time-out issues. Extending the Net::HTTP class in my code and re-initialized with default parameters including an initialization of my own read_timeout should keep things sane I think.
require 'net/http'
# Lengthen timeout in Net::HTTP
module Net
class HTTP
alias old_initialize initialize
def initialize(*args)
old_initialize(*args)
#read_timeout = 5*60 # 5 minutes
end
end
end
Your timeout isn't in the code you set the timeout for. It's here, where you use open-uri:
open("http://www.tubeminator.com/ajax.php?function=downloadvideo&url=http%3A%2F%2Fwww.vimeo.com%2F" + link[0],
You can set a read timeout for open-uri like so:
#!/usr/bin/ruby1.9
require 'open-uri'
open('http://stackoverflow.com', 'r', :read_timeout=>0.01) do |http|
http.read
end
# => /usr/lib/ruby/1.9.0/net/protocol.rb:135:in `sysread': \
# => execution expired (Timeout::Error)
# => ...
# => from /tmp/foo.rb:5:in `<main>'
:read_timeout is new for Ruby 1.9 (it's not in Ruby 1.8). 0 or nil means "no timeout."

Resources