Created
April 1, 2010 13:25
-
-
Save thetoine/351796 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# twitter-autofollow.rb | |
# 2010-04-01 Antoine Girard <[email protected]> | |
# This script will search all you follower and add them as a friend | |
# Also post any privately received message to your public timeline. | |
require 'rubygems' | |
require 'twitter' | |
class AutoFollow | |
USER = '' | |
PASSWORD = '' | |
INTERVAL = 60 # Delay between follows in seconds | |
attr_accessor :client | |
def initialize | |
httpauth = Twitter::HTTPAuth.new(USER, PASSWORD) | |
self.client = Twitter::Base.new(httpauth) | |
# init the infinite loop | |
start | |
end | |
def start | |
puts "Friending everyone." | |
friends | |
puts "Checking for private messages and post new ones." | |
post_messages | |
# sleep for an interval and call "start" method again | |
puts "Sleeping for #{INTERVAL} seconds..." | |
sleep(INTERVAL) | |
start | |
end | |
def friends | |
# get friends following | |
friends = self.client.friend_ids | |
followers = self.client.follower_ids | |
followers.each do |user| | |
begin | |
self.client.friendship_create(user) if !friends.include?(user) | |
rescue Twitter => msg | |
puts "Twitter says: #{msg}" | |
rescue Exception => msg | |
puts "Error: #{msg}" | |
end | |
end | |
end | |
def post_messages | |
self.client.direct_messages.each do |message| | |
if !is_logged?(message) | |
# log message to text file | |
log(message) | |
# send message to twitter | |
begin | |
self.client.update("#{message.text} #jeudiconfession") | |
rescue Twitter => msg | |
puts "Twitter says: #{msg}" | |
rescue Exception => msg | |
puts "Error: #{msg}" | |
end | |
else | |
puts "Error: message already logged and posted" | |
end | |
# and destroy twitter message, we keep nothing | |
puts "Deleting message #{message.id}" | |
self.client.direct_message_destroy(message.id) | |
end | |
end | |
def log(message) | |
puts "Logging message: #{message.id}" | |
File.open('messages.txt', 'a+') do |f| | |
# use "\n" for two lines of text | |
f.puts "#{message.id} #{message.text}" | |
end | |
end | |
def is_logged?(message) | |
ids = [] | |
File.open('messages.txt', 'r').each_line do |l| | |
ids << l.split(' ')[0].to_i | |
end | |
false ? true : ids.include?(message.id.to_i) | |
end | |
end | |
# start instance | |
AutoFollow.new() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment