Twitter bot retweeting same tweet causing it to crash - ruby

I'm trying to write a bot using ruby and the twitter gem.
Here is my code :
#!/usr/bin/env ruby
require 'twitter'
#client = Twitter::REST::Client.new do |config|
config.consumer_key = "xxx"
config.consumer_secret = "xxx"
config.access_token = "xxx"
config.access_token_secret = "xxx"
end
#last_tweet = #client.search("#Hashtag").first
while true
puts "Bot is searching"
puts #last_tweet.text
if #last_tweet.id != #client.search("#Hashtag").first.id
puts #last_tweet.id
puts "bot found another tweet. retweet!"
sleep 1
#client.retweet #last_tweet
#last_tweet = #client.search("#Hashtag").first
puts "the last tweet is now : #{#last_tweet.text}"
end
sleep 5
end
The goal is to simply retweet any tweet with "#hashtag".
Now, the bot is behaving very strangely. For some reasons it's sometimes randomly seem to RT the same tweet twice, causing it to crash.
I tried for hours, I even copied this gist : https://gist.github.com/nilsding/834c2fe8829d29b79e23
Which have the exact same issue.
How can I make it not retweet the same tweet ?
I've already checked this question but can't understand how to apply it to my simple ruby file.

Related

How to Retweet as a Quote with custom message in Ruby

How to Retweet as a Quote with custom message in Ruby
like tweeting as a quote with custom message.
Example: "XYZ Message {Quote Tweet Retweet}"
It is currently simply retweeting the tweet without message
Like in the image example
Here is my code:
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "x"
config.consumer_secret = "x"
config.access_token = "x"
config.access_token_secret = "x"
end
def run(client)
retweetKeyword = "abc"
while true
re = client.search(retweetKeyword).first.id
client.retweet(re);
puts "Retweet: #{re} #{Time.now}";
sleep(30); #Every five minutes
end
end
run(client);
If you look at Hootsuit that how it retweets then you'll see that it just tweet the Tweet Url that you want to Quote and it quotes that.
To Quote a Tweet with a Custom Message, you would do something like this,
"Quote message" Tweet Url like this
https://twitter.com/cowan_rebs/status/983310177343627264
and it would quote the tweet with the custom message.
Hope that answers the question.

Trying to pull from API with Ruby -- how to pass variable into string?

Im trying to practice pulling APIs with Ruby. Im trying to pull videogame news from Steam.
Below is my code.
The idea is, when the program is ran, the the user is prompted to enter a game ID between 200 and 440.
Anything not in between dont exist or the numbers arent continuous.
Anyway, Im trying to pass the gameID variable into the string:
"http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=#{gameID}&count=5&maxlength=300&format=json"
The string is wrapped in a function. When I try to run the program, the error says wrong number of arguments ( 0 for 1 ).
What am i doing wrong, and what am I missing? Many thanks in advance as usual :)
*been doing nothing but asking questions so far, hope to contribute someday once I get better :)
require 'json'
require 'HTTParty'
puts "----------------------------------------------------------"
puts "Welcome to my practice"
puts "The purpose of this exercise is to use the SteamAPI"
puts "to pull videogame news from Steam"
puts "----------------------------------------------------------"
reset = true
while reset
puts "Please enter a game ID between 200 - 440"
gameID = gets.to_i
if gameID < 200
puts "--Invalid input--"
reset = true
elsif gameID > 400
puts "--Invalid input--"
reset= true
else
reset = false
end
end
puts "--------------------Loading API----------------------------"
def get_news( gameID )
string = "http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=#{gameID}&count=5&maxlength=300&format=json"
page = HTTParty.get( string )
browse = page["appnews"]["newsitems"]
browse.map do |content|
{title: content["title"], contents: content["contents"]}
end
end
def display_story( content )
puts "Title: #{content[:title]}"
puts "--------------------"
puts " #{content[:contents]}"
puts "--------------------"
end
get_news.each do |content|
display_story( content )
end
You've defined get_news to take a gameID argument, but you haven't passed it anything. At the end of your file, you need get_news(gameID).each.

How to get user followers [duplicate]

This question already has answers here:
Twitter API - Ruby Twitter Gem
(2 answers)
Closed 7 years ago.
I'm trying to get a list of my followers via Ruby and the Twitter API, however when I use the code you see below, my response in the terminal is:
#<Twitter::Cursor:0x007f8e71d922e8>
How can I turn that into of list with any parameters like usernames and or IDs?
My code:
require 'Twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "..."
config.consumer_secret = "..."
config.access_token = "..."
config.access_token_secret = "..."
end
puts client.followers
as in the duplicate answer link, you can simply use collect
screen_names = client.followers.collect {|f| client.user(f).screen_name }

Twitter REST API and ruby hashtag confusion

so I'm trying to set up a script to grab tweets so I can use them in my app. Currently I'm using Ruby 2.0.0 and the 1.1 Twitter API REST. Currently I'm not worried about storing the tweets in a database or anything I'm just simply trying to get the correct tweets into terminal. Here is my current code.
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "XXXXXXXXXXXXXXXXXX"
config.consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
config.access_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
config.access_token_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
end
topics = ["#BreakingBad", "#Gameofthones"]
client.filter(:track => topics.join(",")) do |tweet|
puts tweet.text
endtag = options[:search]
My problem is that when I run the script I get flooded with just a mass of tweets in all different languages. Any help would be great. Thanks.

Ruby: Script won't grab URL from youtube correctly

I found this script on pastebin that is an IRC bot that will find youtube videos for you. I have not touched it at all (Bar the channel settings), it works well however it won't grab the URL to the video that has been searched. This code is not mine! I jsut would like to get it to work as it would be quite useful!
#!/usr/bin/env ruby
require 'rubygems'
require 'cinch'
require 'nokogiri'
require 'open-uri'
require 'cgi'
bot = Cinch::Bot.new do
configure do |c|
c.server = "irc.freenode.net"
c.nick = "YouTubeBot"
c.channels = ["#test"]
end
helpers do
#Grabs the first result and returns the TITLE,LINK,DESCRIPTION
def youtube(query)
doc = Nokogiri::HTML(open("http://www.youtube.com/results?q=#{CGI.escape(query)}"))
result = doc.css('div#search-results div.result-item-main-content')[0]
title = result.at('h3').text
link = "www.youtube.com"+"#{result.at('a')[:href]}"
desc = result.at('p.description').text
rescue
"No results found"
else
CGI.unescape_html "#{title} - #{desc} - #{link}"
end
end
on :channel, /^!youtube (.+)/ do |m, query|
m.reply youtube(query)
end
on :channel, "polkabot quit" do |m|
m.channel.part("bye")
end
end
bot.start
Currently if i use the command
!youtube asdf
I get this returned:
19:25 < YouTubeBot> asdfmovie - Worldwide Store www.cafepress.com ...
asdfmovie cakebomb tomska
epikkufeiru asdf movie ... tomska ... [Baby Giggling] Man: Got your nose! [Baby ...
- www.youtube.com#
As you can see the URL is just www.youtube.com# not the URL of the video.
Thanks a lot!
This is an xpath issue. It looks like the third 'a' that has the href you want so try:
link = "www.youtube.com#{result.css('a')[2][:href]}"

Resources